从网上找到的,原来的代码画框(Rectangle),改进为画块(FillRect),并保持画块前的画布属性。
function GetSelectedColor(const AIndex: Integer): TColor;
begin
case AIndex of
0: Result := clYellow;
1: Result := clRed;
2: Result := clGreen;
3: Result := clBlue;
4: Result := clNavy;
5: Result := clMaroon;
6: Result := clOlive;
7: Result := clPurple;
8: Result := clTeal;
9: Result := clLime;
10:Result := clFuchsia;
11:Result := clAqua;
else
Result := clYellow;
end;
end;
cbSelColor.Style := csOwnerDrawFixed;
procedure cbSelColorDrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
OBC, OFC, SelColor: TColor;
CurrRect: TRect;
begin
SelColor := GetSelectedColor(Index);
with cbSelColor.Canvas do
begin
FillRect(Rect);
OBC := Brush.Color; // my enhancement
OFC := Font.Color; // my enhancement
Brush.Color := SelColor;
Font.Color := clBlack;
CurrRect.Left := Rect.Left + 1;
CurrRect.Top := Rect.Top + 1;
CurrRect.Right := Rect.Left + 14;
CurrRect.Bottom := Rect.Top + 14;
FillRect(CurrRect); // my enhancement
Brush.Color := OBC; // my enhancement
Font.Color := OFC; // my enhancement
TextOut(CurrRect.Right + 10, CurrRect.Top, cbSelColor.Items[Index]);
end;
end;
另外几个小技巧:
procedure BitmapToIcon(ABitmap: TBitmap; ATransColor: TColor): TIcon;
begin
with TImageList.CreateSize(ABitmap.Width, ABitmap.Height) do
begin
try
AllocBy := 1;
AddMasked(ABitmap, ATransColor);
Result := TIcon.Create;
try
GetIcon(0, Result);
except
Ico.Free;
raise;
end;
finally
Free;
end;
end;
end;
function RGBToColor(R, G, B:Byte): TColor;
begin
Result := B Shl 16 or
G Shl 8 or
R;
end;
procedure GetColorRGB(const AColor: TColor; var R, G, B: Integer);
begin
R := AColor and $FF;
G := (AColor and $FF00) shr 8;
B := (AColor and $FF0000) shr 16;
end;