В книгах Калверта, Свана и других авторов можно найти похожий текст. Смысл текста — "Изменить цвет кнопок Button, BitBt нельзя, т.к. их рисует WINDOWS". Если нельзя, но ОЧЕНЬ НУЖНО, то можно.
Небольшой компонент ColorBtn, дает возможность использовать в кнопках цвет. Кроме того, представлено новое свойство — Frame3D, позволяющее получить более реалистичный вид нажатой кнопки. В отличие от API, при изменении значения свойства Frame3D, не требуется переоткрытие компонента.
unitColorBtn;
interface
usesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;
typeTColorBtn = class(TButton)
private
{ Private declarations }
IsFocused: boolean;
FCanvas: TCanvas;
F3DFrame: boolean;
FButtonColor: TColor;
procedureSet3DFrame(Value: boolean);
procedureSetButtonColor(Value: TColor);
procedureCNDrawItem( var Message: TWMDrawItem); messageCN_DRAWITEM;
procedureWMLButtonDblClk( var Message: TWMLButtonDblClk); messageWM_LBUTTONDBLCLK;
procedureDrawButtonText( constCaption: string; TRC: TRect; State: TButtonState; BiDiFlags: Longint);
procedureCalcuateTextPosition( constCaption: string; varTRC: TRect; BiDiFlags: Longint);
protected
{ Protected declarations }
procedureCreateParams( varParams: TCreateParams); override;
procedureSetButtonStyle(ADefault: boolean); override;
public
{ Public declarations }
constructorCreate(AOwner: TComponent); override;
destructorDestroy; override;
published
{ Published declarations }
propertyButtonColor: TColor readFButtonColor writeSetButtonColor defaultclBtnFace;
propertyFrame3D: boolean readF3DFrame writeSet3DFrame defaultFalse;
end;
procedure Register;
implementation
{ TColorBtn }
constructorTColorBtn.Create(AOwner: TComponent);
begin
InheritedCreate(AOwner);
FCanvas:= TCanvas.Create;
FButtonColor:= clBtnFace;
F3DFrame:= False;
end;
destructorTColorBtn.Destroy;
begin
FCanvas.Free;
InheritedDestroy;
end;
procedureTColorBtn.CreateParams( varParams: TCreateParams);
begin
InheritedCreateParams(Params);
withParams doStyle:= Style orBS_OWNERDRAW;
end;
procedureTColorBtn.Set3DFrame(Value: boolean);
begin
ifF3DFrame <> Value thenF3DFrame:= Value;
end;
procedureTColorBtn.SetButtonColor(Value: TColor);
begin
ifFButtonColor <> Value then begin
FButtonColor:= Value;
Invalidate;
end;
end;
procedureTColorBtn.WMLButtonDblClk( var Message: TWMLButtonDblClk);
begin
Perform(WM_LBUTTONDOWN, Message.Keys, Longint(Message.Pos));
end;
procedureTColorBtn.SetButtonStyle(ADefault: Boolean);
begin
ifIsFocused <> ADefault thenIsFocused:= ADefault;
end;
procedureTColorBtn.CNDrawItem( var Message: TWMDrawItem);
var
RC: TRect;Flags: Longint;
State: TButtonState;
IsDown, IsDefault: Boolean;
DrawItemStruct: TDrawItemStruct;
begin
DrawItemStruct:= Message.DrawItemStruct^;
FCanvas.Handle:= DrawItemStruct.HDC;
RC:= ClientRect;
withDrawItemStruct do begin
IsDown:= ItemState andODS_SELECTED <> 0;
IsDefault:= ItemState andODS_FOCUS <> 0;
if notEnabled thenState:= bsDisabled
else ifIsDown thenState:= bsDown
elseState:= bsUp;
end;
Flags:= DFCS_BUTTONPUSH orDFCS_ADJUSTRECT;
ifIsDown thenFlags:= Flags orDFCS_PUSHED;
ifDrawItemStruct.ItemState andODS_DISABLED <> 0 thenFlags:= Flags orDFCS_INACTIVE;
ifIsFocused orIsDefault then begin
FCanvas.Pen.Color:= clWindowFrame;
FCanvas.Pen.Width:= 1;
FCanvas.Brush.Style:= bsClear;
FCanvas.Rectangle(RC.Left, RC.Top, RC.Right, RC.Bottom);
InflateRect(RC, -1, -1);
end;
ifIsDown then begin
FCanvas.Pen.Color:= clBtnShadow;
FCanvas.Pen.Width:= 1;
FCanvas.Rectangle(RC.Left, RC.Top, RC.Right, RC.Bottom);
InflateRect(RC, -1, -1);
ifF3DFrame then begin
FCanvas.Pen.Color:= FButtonColor;
FCanvas.Pen.Width:= 1;
DrawFrameControl(DrawItemStruct.HDC, RC, DFC_BUTTON, Flags);
end;
end elseDrawFrameControl(DrawItemStruct.HDC, RC, DFC_BUTTON, Flags);
Читать дальше