delphi 制作放两个小图片的按钮


本文整理自网络,侵删。

 本例效果图:


自定义的类(TMyButton):
--------------------------------------------------------------------------------

unit Unit2;

interface

uses
Windows, Messages, Classes, Graphics, StdCtrls;

type
TMyButton = class(TButton)
private
FBit1,FBit2: TBitmap;
protected
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Click; override;
end;

implementation

{ TMyButton }

procedure TMyButton.Click;
begin
inherited;
MessageBox(0, 'MyButton', 'Hi', MB_OK);
end;

constructor TMyButton.Create(AOwner: TComponent);
var
wh: Integer;
begin
inherited;
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;
begin
FBit1.Free;
FBit2.Free;
inherited;
end;

procedure TMyButton.WMPaint(var Message: TWMPaint);
var
cvs: TCanvas;
DC: HDC;
begin
Inherited;
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;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
with TMyButton.Create(Self) do begin
Parent := Self;
Left := Random(Self.ClientWidth - Width);
Top := Random(Self.ClientHeight - Height);
end;
end;

end.

相关阅读 >>

Delphi一个非常完整的取windows os 版本信息的函数

Delphi 计算一个路径相对于另一路径的相对路径

Delphi 进程程序多开调用单元

tmsweb core 组件居中显示

Delphi getexplorerpid获取系统explorer.exe进程id

Delphi 控制台程序获取系统信息

Delphi 怎样判断spinedit在微调时值是增加还是减少了

Delphi 获取当前鼠标指针位置文本

Delphi 动态更改webbrowser数据流内容

Delphi 开发中遇到的dll问题思考及解决方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...