为什么80%的码农都做不了架构师?>>>
问题来源: http://www.cnblogs.com/del/archive/2009/03/12/1409708.html#1475240
本例效果图:
自定义的类(TMyButton):
unit Unit2;interfaceusesWindows, Messages, Classes, Graphics, StdCtrls;typeTMyButton = class(TButton)privateFBit1,FBit2: TBitmap;protectedprocedure WMPaint(var Message: TWMPaint); message WM_PAINT;publicconstructor Create(AOwner: TComponent); override;destructor Destroy; override;procedure Click; override;end;implementation{ TMyButton }procedure TMyButton.Click;
begininherited;MessageBox(0, 'MyButton', 'Hi', MB_OK);
end;constructor TMyButton.Create(AOwner: TComponent);
varwh: Integer;
begininherited;FBit1 := TBitmap.Create;FBit2 := TBitmap.Create;//在此可以载入图片, 为了测试方便, 我随便画两个矩形吧wh := Height - 8;FBit1.SetSize(wh, wh);FBit2.SetSize(wh, wh);FBit1.Canvas.Brush.Color := clRed;FBit1.Canvas.Rectangle(0, 0, wh, wh);FBit2.Canvas.Brush.Color := clLime;FBit2.Canvas.Rectangle(0, 0, wh, wh);
end;destructor TMyButton.Destroy;
beginFBit1.Free;FBit2.Free;inherited;
end;procedure TMyButton.WMPaint(var Message: TWMPaint);
varcvs: TCanvas;DC: HDC;
beginInherited;DC := GetDC(Handle);cvs := TCanvas.Create;cvs.Handle := DC;cvs.Draw(4, (Height - FBit2.Height) div 2, FBit1);cvs.Draw(Width - FBit2.Width - 4, (Height - FBit2.Height) div 2, FBit2);cvs.Free;ReleaseDC(Handle, DC);
end;end.
测试代码:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}uses Unit2;procedure TForm1.Button1Click(Sender: TObject);
beginwith TMyButton.Create(Self) do beginParent := Self;Left := Random(Self.ClientWidth - Width);Top := Random(Self.ClientHeight - Height);end;
end;end.