我发现在delphi中,用
  if dlgColor1.Execute then
  begin
    pnl33.Color := dlgColor1.Color;
    btn4.Caption := ColorToString(dlgColor1.Color);
  end;
可以获得颜色的字符串形式,用
pnl24.Color := StrToInt64(btn4.Caption);
可以将字符串再转行为颜色,但是如果获取颜色时候获取的值是clRed,clBlue这类的值,再将string转换为color就不能转换了,只能转换$00FF0080这类的值,怎么解决clRed这种情况?
或者说怎么获取颜色不是clRed而是一个6位16进制的值?

解决方案 »

  1.   

    uses Graphicsfunction IdentToColor(const Ident: string; var Color: Longint): Boolean
    function ColorToIdent(Color: Longint; var Ident: string): Boolean;
      

  2.   

    var
      s : string;
      c : Longint;
    begin
      //引用Graphics单元
      //把颜色值转到字符串
      if ColorToIdent(clRed, s) then
        Caption := s;
      //把字符串转到颜色值
      if IdentToColor('clRed', c) then
      begin
         Canvas.Brush.Color := c;
         Canvas.FillRect(Rect(0,0,100,100));
      end;
    end;
      

  3.   

    uses
      Graphics;字符串转为颜色:
    function TfrmMain.strToColor(s: string): TColor;
    var
      clo: Integer;
      sClo: string;
    begin
      {将字符串转为颜色}
      try
        if IdentToColor(s,clo)then
        begin
          sClo := IntToStr(clo);
          Result := StrToInt64(sClo);
        end
        else
        begin
          Result := StrToInt64(btn4.Caption) ;
        end;
      except
        Result := $000000;
      end;
    end;