我自己写了一个控件,从Timage继承.用来捕捉鼠标移进移出图片以及鼠标左键按下和弹起时的消息,然后对图片进行处理,可是发现能响应左键按下和弹起,但竟然不能响应CLICK事件了.
我刚开始写的时候是只捕捉了WM_MOUSEENTER和WM_MOUSELEAVE.那个时候还是可以响应CLICK事件的.可是当我再捕捉WM_LBUTTONDOWN和WM_LBUTTONUP时.就发现不能响应CLICK事件了.这是怎么会事呢?
我刚开始写的时候是只捕捉了WM_MOUSEENTER和WM_MOUSELEAVE.那个时候还是可以响应CLICK事件的.可是当我再捕捉WM_LBUTTONDOWN和WM_LBUTTONUP时.就发现不能响应CLICK事件了.这是怎么会事呢?
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;
DoMouseUp(Message, mbLeft);
end;OnClick也是通过WM_LBUTTONUP消息通知的
所以你处理的时候要调用inherited;让父类也处理该消息
我现在加了Inherited了.现在能响应CLICK了.可是发现要是CLICK事件和WM_LBUTTONDOWN WM_LBUTTONUP中都写上代码的话就又不响应CLICK了.
SysUtils, Classes, Controls, ExtCtrls,messages;type
TYsgImage = class(TImage)
private
{ Private declarations }
FOnMouseEnter:TNotifyEvent;
FOnMouseLeave:TNotifyEvent;
FOnLButtonDown:TNotifyEvent;
FOnLButtonUp:TNotifyEvent;
protected
{ Protected declarations }
procedure DoMouseEnter;virtual;
Procedure DoMouseLeave;virtual;
procedure CMMouseEnter(Var Msg:TMessage);message CM_MOUSEENTER;
procedure CMMouseLeave(Var Msg:TMessage);message CM_MOUSELEAVE;
procedure DoLButtonDown;virtual;
procedure DoLButtonUp;virtual;
procedure WMLButtonDown(var Message: TWMLButtonDown);message WM_LBUTTONDOWN;
procedure WMLButtonUp(var Message: TWMLButtonUp);message WM_LBUTTONUP;
public
{ Public declarations }
published
{ Published declarations }
property OnMouseEnter:TNotifyEvent Read FOnMouseEnter Write FOnMouseEnter;
property OnMouseLeave:TNotifyEvent Read FOnMouseLeave Write FOnMouseLeave;
property OnLButtonDown:TNotifyEvent Read FOnLButtonDown Write FOnLButtonDown;
property OnLButtonUp:TNotifyEvent Read FOnLButtonUp Write FOnLButtonUp;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Coffee', [TYsgImage]);
end;{ YsgImage }procedure TYsgImage.CMMouseEnter(Var Msg: TMessage);
begin
DoMouseEnter;
end;procedure TYsgImage.CMMouseLeave(Var Msg: TMessage);
begin
DoMouseLeave;
end;procedure TYsgImage.DoLButtonDown;
begin
if assigned(FOnLButtonDown) then
FOnLButtonDown(self);
end;procedure TYsgImage.DoLButtonUp;
begin
if assigned(FOnLButtonUp) then
FOnLButtonUp(self);
end;procedure TYsgImage.DoMouseEnter;
begin
if assigned(FOnMouseEnter) then
FOnMouseEnter(self);
end;procedure TYsgImage.DoMouseLeave;
begin
if assigned(FOnMouseLeave) then
FOnMouseLeave(self);
end;procedure TYsgImage.WMLButtonDown(var Message: TWMLButtonDown);
begin
inherited;
DoLButtonDown;
end;procedure TYsgImage.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited;
DoLButtonUp;
end;end.
非常感谢zswang(伴水清清)(专家门诊清洁工) 我在事件代码里用的是SHOWMESSAGE函数。结果导致CLICK事件不能正确执行。
今天算是对这是事件有了更深的了解了