Hi,
anstatt WM_PAINT könntest du auch CN_DRAWITEM nehmen.
Noch ein bisschen unfertiger Code, aber zur Demonstration reicht es:
Delphi-Quellcode:
type
TChBox =
class(TCheckBox)
protected
procedure CNDrawItem(
var Message: TWMDrawItem);
message CN_DRAWITEM;
procedure CreateWnd;
override;
end;
procedure TChBox.CNDrawItem(
var Message: TWMDrawItem);
var
Canv: TCanvas;
CheckRect: TRect;
begin
CheckRect :=
Message.DrawItemStruct.rcItem;
CheckRect.Right := CheckRect.Bottom;
Canv := TCanvas.Create;
try
Canv.Handle :=
Message.DrawItemStruct.hDC;
//if Message.DrawItemStruct.ItemState and ODS_SELECTED <> 0 then
if Self.Checked
then
Canv.Brush.Color := clRed
else
Canv.Brush.Color := clWhite;
Canv.Rectangle(CheckRect);
Canv.Brush.Style := bsClear;
Canv.TextOut(CheckRect.Right + 2,(CheckRect.Bottom - Canv.TextHeight(Caption))
div 2, Caption);
finally
Canv.Free;
end;
end;
procedure TChBox.CreateWnd;
begin
inherited CreateWnd;
if HandleAllocated
then
SendMessage(
Handle, BM_SETSTYLE, BS_CHECKBOX
or BS_OWNERDRAW, 1);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Cb: TChBox;
begin
Cb:= TChBox.Create(self);
Cb.Parent:= self;
Cb.Caption:= '
Test';
Cb.Left:= 20;
Cb.Top:= 50;
end;