论坛上看到能人提这个问题,就试着做了做,重载了TBitBtn
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure MyOnMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
{ Private declarations }
public
{ Public declarations }
end;
type
MyTBitBtn = class(TBitBtn)
private
DownStat: Boolean;
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
end;
var
Form1: TForm1;
NewTBitBtn: MyTBitBtn;
implementation
{$R *.dfm}
procedure TForm1.MyOnMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then MyTBitBtn(Sender).DownStat := not MyTBitBtn(Sender).DownStat;
SendMessage(MyTBitBtn(Sender).Handle, CM_INVALIDATE, 0, 0); //让按钮重画
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
NewTBitBtn := MyTBitBtn.Create(self);
NewTBitBtn.Parent := self;
NewTBitBtn.Top := 100;
NewTBitBtn.Left := 100;
NewTBitBtn.DownStat := false;
NewTBitBtn.OnMouseUp := MyOnMouseUp;
end;
{ MyTBitBtn }
procedure MyTBitBtn.CNDrawItem(var Message: TWMDrawItem);
begin
if DownStat then Message.DrawItemStruct.itemState := 17;
inherited;
end;
end.