我自己写了一个控件,从Timage继承.用来捕捉鼠标移进移出图片以及鼠标左键按下和弹起时的消息,然后对图片进行处理,可是发现能响应左键按下和弹起,但竟然不能响应CLICK事件了.
我刚开始写的时候是只捕捉了WM_MOUSEENTER和WM_MOUSELEAVE.那个时候还是可以响应CLICK事件的.可是当我再捕捉WM_LBUTTONDOWN和WM_LBUTTONUP时.就发现不能响应CLICK事件了.这是怎么会事呢?

解决方案 »

  1.   

    procedure TControl.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;
      DoMouseUp(Message, mbLeft);
    end;OnClick也是通过WM_LBUTTONUP消息通知的
    所以你处理的时候要调用inherited;让父类也处理该消息
      

  2.   

    刚查了下,好象是因为我捕捉了WM_LBUTTONDOWN和WM_LBUTTONUP后,覆盖了TCONTROL的两个方法,而CLICK是在这两个方法中调用的.我自己的方法中又没处理所以没有触发CLICK事件.不过我刚看了下VCL代码.发现驱动CLICK事件的代码很难懂.好多状态和条件,我看不太懂,我想捕捉WM_LBUTTONDOWN和WM_LBUTTONUP,又要能响应CLICK事件,不知道有没有简单的方法实现.
      

  3.   


    我现在加了Inherited了.现在能响应CLICK了.可是发现要是CLICK事件和WM_LBUTTONDOWN WM_LBUTTONUP中都写上代码的话就又不响应CLICK了.
      

  4.   

    你最好给出能调试的代码方便大家分析,你可以直接Click;触发OnClick
      

  5.   

    unit MouseImage;interfaceuses
      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.
      

  6.   

    我刚发现TIMAGE本身就带了MOUSEDOWN和MOUSEUP。可是我做了个实现发现要时同时在CLICK和MOUSEDOWN 以及MOUSEUP中同时写上代码。CLICK事件就不能响应了。不知道是我做的不对还是怎么回事?
      

  7.   

    问题解决了!是我测试的不对。
    非常感谢zswang(伴水清清)(专家门诊清洁工) 我在事件代码里用的是SHOWMESSAGE函数。结果导致CLICK事件不能正确执行。
    今天算是对这是事件有了更深的了解了