delphi TColorToHex 与 HexToTColor


TColorToHex 与 HexToTColor
function TColorToHex(Color: TColor): string;
begin
Result :=
IntToHex(GetRValue(Color), 2) +
IntToHex(GetGValue(Color), 2) +
IntToHex(GetBValue(Color), 2);
end;

function HexToTColor(sColor: string): TColor;
begin
Result :=
RGB(
StrToInt(#36 + Copy(sColor, 1, 2)),
StrToInt(#36 + Copy(sColor, 3, 2)),
StrToInt(#36 + Copy(sColor, 5, 2))
);
end;

//测试:
procedure TForm1.Button1Click(Sender: TObject);
var
c: TColor;
s: string;
begin
c := clRed;
s := TColorToHex(c);
ShowMessage(s); //FF0000 (相当于 HTML 中的 #FF0000)

s := 'FF0000';
c := HexToTColor(s);
ShowMessage(BoolToStr(c=clRed)); //-1
Self.Color := c; //窗体将变红色
end;

联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678