var cvs:tcanvas; begin cvs:=tcanvas.create; cvs.handle:=getdc(button1.handle); //cvs.brush.color:=rgb(0,128,128); //cvs.FillRect(rect(2,2,button1.width-2,button1.height-2)); //画背景 cvs.brush.style:=bsclear; cvs.font:=button1.font; cvs.font.color:=rgb(0,0,255); cvs.TextOut((button1.width-cvs.textwidth(button1.caption)) div 2,(button1.height-cvs.textheight(button1.caption)) div 2,button1.caption); end;
begin
cvs:=tcanvas.create;
cvs.handle:=getdc(button1.handle);
//cvs.brush.color:=rgb(0,128,128);
//cvs.FillRect(rect(2,2,button1.width-2,button1.height-2)); //画背景
cvs.brush.style:=bsclear;
cvs.font:=button1.font;
cvs.font.color:=rgb(0,0,255);
cvs.TextOut((button1.width-cvs.textwidth(button1.caption)) div 2,(button1.height-cvs.textheight(button1.caption)) div 2,button1.caption);
end;
只是这样只能改变按钮表面的颜色,但单击按钮时还会成为原来按钮的
颜色
关注中!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TButtonMy=class(TButton)
private
procedure demo(var Message: TMessage); message WM_Paint;
public
procedure Click; override;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TButtonMy.demo(var Message: TMessage);
var cvs:tcanvas;
begin
inherited;
cvs:=tcanvas.create;
cvs.handle:=getdc(self.handle);
cvs.brush.color:=rgb(0,128,128);
cvs.FillRect(rect(2,2,self.width-2,self.height-2)); //画背景
cvs.brush.style:=bsclear;
cvs.font:=self.font;
cvs.font.color:=rgb(0,0,255);
cvs.TextOut((self.width-cvs.textwidth(self.caption)) div 2,(self.height-cvs.textheight(self.caption)) div 2,self.caption);
end;
procedure TButtonMy.Click;
var
Form: TCustomForm;
begin
Form := GetParentForm(Self);
if Form <> nil then Form.ModalResult := ModalResult;
inherited Click;
SendMessage(self.Handle,wm_paint,0,0);
end;
procedure TForm1.FormCreate(Sender: TObject);
var but:TButtonMy;
begin
but:=TButtonMy.Create(nil);
but.ParentWindow :=form1.Handle ;
but.Show ;
end;end.