TButton类型不能响应鼠标双击事件,即使用了显式声明,如:
TxyButton = class(TButton)
published
property OnDBlClick;
end;
查了一下源代码:
procedure TControl.WMLButtonDblClk(var Message: TWMLButtonDblClk);
begin
SendCancelMode(Self);
inherited;
if csCaptureMouse in ControlStyle then MouseCapture := True;
if csClickEvents in ControlStyle then DblClick; //注意这里的csClickEvents
DoMouseDown(Message, mbLeft, [ssDouble]);
end
实际上TControl的子类默认的ControlStyle都带有csClickEvents的:
constructor TControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowProc := WndProc;
FControlStyle := [csCaptureMouse, csClickEvents, csSetCaption, csDoubleClicks];
FFont := TFont.Create;
...
但是TButton类就没那么好运了:
constructor TButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csSetCaption, csDoubleClicks];
//注意这里没有csClickEvents
Width := 75;
Height := 25;
TabStop := True;
end;
解决方法有两种:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)一、拦截WM_LButtonDBLCLK
二、在TButton的声明中显式加入csClickEvents
constructor TxyButton.Create;
begin
inherited Create(Form1);
ControlStyle := ControlStyle + [csClickEvents];
OnDBLClick := OnDBClick;
end;
procedure TxyButton.OnDBClick(Sender: TObject);
begin
ShowMessage('DBClick');
end;