光是截取消息恐怕不妥吧,OnClick的触发实际是由WM_LButtonDown和WM_LButtonUp协作完成的,要屏蔽OnMouseUp和OnMouseDown,得彻底改写WM_LButtonDown和WM_LButtonUp的消息处理函数,在TControl默认是这样处理的:
procedure TMyButton.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited;
if csCaptureMouse in ControlStyle then MouseCapture := False;
if csClicked in ControlState then
begin
Exclude(FControlState, csClicked);
if PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
Click(); //触发OnClick
end;
DoMouseUp(Message, mbLeft);//把这句去掉就可以屏蔽OnMouseUp事件
end;你可以从TButton派生自己的Button类,在其中声明一下函数
TMyButton=class(TButton)
private
FOnClick:TNotifyEvent;
procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
protected
procedure Click; dynamic;
public
property OnClick:TMyNotifyEvent read FOnClick write FOnClick;
end;////////
procedure TMyButton.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited;
if csCaptureMouse in ControlStyle then MouseCapture := False;
if csClicked in ControlState then
begin
Exclude(FControlState, csClicked);
if PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
Click();
end;
end;procedure TMyButton.Click;
begin
if Assigned(FOnClick) then
FOnClick(self);
end;
procedure TMyButton.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited;
if csCaptureMouse in ControlStyle then MouseCapture := False;
if csClicked in ControlState then
begin
Exclude(FControlState, csClicked);
if PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
Click(); //触发OnClick
end;
DoMouseUp(Message, mbLeft);//把这句去掉就可以屏蔽OnMouseUp事件
end;你可以从TButton派生自己的Button类,在其中声明一下函数
TMyButton=class(TButton)
private
FOnClick:TNotifyEvent;
procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
protected
procedure Click; dynamic;
public
property OnClick:TMyNotifyEvent read FOnClick write FOnClick;
end;////////
procedure TMyButton.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited;
if csCaptureMouse in ControlStyle then MouseCapture := False;
if csClicked in ControlState then
begin
Exclude(FControlState, csClicked);
if PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
Click();
end;
end;procedure TMyButton.Click;
begin
if Assigned(FOnClick) then
FOnClick(self);
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货