这是TWinControl中的WM_EraseBkgnd消息,你可以改变FBrush的Color、Style等,也可以改变FillRect函数中的Rect等. procedure TWinControl.WMEraseBkgnd(var Message: TWMEraseBkgnd); begin { Only erase background if we're not doublebuffering or painting to memory. } if not FDoubleBuffered or (TMessage(Message).wParam = TMessage(Message).lParam) then FillRect(Message.DC, ClientRect, FBrush.Handle); Message.Result := 1; end;
procedure TWinControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
{ Only erase background if we're not doublebuffering or painting to memory. }
if not FDoubleBuffered or
(TMessage(Message).wParam = TMessage(Message).lParam) then
FillRect(Message.DC, ClientRect, FBrush.Handle);
Message.Result := 1;
end;
所以对从TCustomControl或TGraphicControl继承的直接用Canvas属性就行啦,而对从TGraphicControl继承的,你需要创建一个新的类,在这个类中相应WM_PAINT或WM_ERASEBKGND消息来画窗体.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, DatetimeDialog, ExtCtrls, Db, Grids, DBGrids,
DBTables;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FDefWndProc: TFarProc; procedure ButtonWndProc(var Message: TMessage);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ButtonWndProc(var Message: TMessage);
var
dc: HDC;
rct: TRect;
ps: TPaintStruct;
font: HFONT;
begin
if Message.Msg=WM_ERASEBKGND then
begin
FillRect( TWMEraseBkgnd(Message).DC, Button1.ClientRect, Button1.Brush.Handle );
Message.Result := 1;
end
else
if Message.Msg=WM_PAINT then
begin
if( GetUpdateRect( Button1.Handle, rct, True ) )then
begin
BeginPaint( Button1.Handle, ps );
try
dc := ps.hdc;
rct := Button1.ClientRect;
DrawFrameControl( dc, rct, DFC_BUTTON, DFCS_BUTTONPUSH or DFCS_ADJUSTRECT);
FillRect( dc, rct, Button1.Brush.Handle );
if Button1.Focused then
begin
InflateRect( rct, -2, -2 );
DrawFocusRect( dc, rct );
end;
font := SelectObject( dc, Button1.Font.Handle );
SetTextColor( dc, ColorToRGB(clWhite) );
SetBkMode( dc, TRANSPARENT );
DrawText( dc, Pchar(Button1.Caption), Length(Button1.Caption), rct, DT_CENTER or DT_VCENTER or DT_SINGLELINE );
SelectObject( dc, font );
finally
EndPaint( Button1.Handle, ps );
end;
end;
end
else
with Message do
Result := CallWindowProc(FDefWndProc, Button1.Handle, Msg, wParam, lParam);
end;procedure TForm1.FormCreate(Sender: TObject);
var
FButtonInstance: TFarProc;
begin
Button1.Brush.Style := bsSolid;
Button1.Brush.Color := clBlue;
FButtonInstance := MakeObjectInstance(ButtonWndProc);
FDefWndProc := Pointer(GetWindowLong(Button1.Handle, GWL_WNDPROC));
SetWindowLong(Button1.Handle, GWL_WNDPROC, Longint(FButtonInstance));
end;end.