zoukankan      html  css  js  c++  java
  • TGraphicControl如何捕获鼠标滚轮事件(WMMouseWheel)

    TGraphicControl默认情况下无法捕获鼠标滚轮事件,如果需要处理鼠标滚轮事件可以采用以下方式处理:

    1、在CMMouseenter事件中捕获鼠标,以获得后续的鼠标事件处理权

    procedure TTestControl.CMMouseenter(var Message: TMessage);
    begin
      FPrevFocusHwnd := SetFocus(Parent.Handle);
      MouseCapture := True;
      inherited;
    end;
    

    2、在鼠标离开控件区域时释放鼠标

    procedure TTestControl.WMMouseMove(var Message: TWMMouseMove);
    begin
      if MouseCapture and not PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
      begin
        MouseCapture := False;
        SetFocus(FPrevFocusHwnd);
      end;
      inherited;
    end;

    经过上述方式处理后就可以在TGraphicControl中响应鼠标滚轮事件了(DoMouseWheel、DoMouseWheelDown、DoMouseWheelUp)。

    以下是Delphi帮助文件中对属性MouseCapture的说明:

    Specifies whether the control has "captured" mouse events.

    Use MouseCapture to determine whether a control has captured the mouse. When a control captures the mouse,
    all subsequent mouse events go to that control until the user releases the mouse button.

    A control captures the mouse when the user drags an item from it. In addition, if the control has the csCaptureMouse flag set in its ControlStyle property,
    it captures the mouse when the user presses the left mouse button over it, until the user releases the mouse button.

    大意是说:

    MouseCapture指定控件是否“捕获”了鼠标事件。
    使用MouseCapture确定控件是否捕获了鼠标。当控件捕获鼠标时,所有后续的鼠标事件都将转到该控件,直到用户释放鼠标按钮为止。
    控件在用户从中拖动项目时捕获鼠标。此外,如果控件在其ControlStyle属性中设置了csCaptureMouse标志,则当用户在其上按鼠标左键时,
    它将捕获鼠标,直到用户释放鼠标按钮为止。

  • 相关阅读:
    如何学习一项新技术呢?
    开发人员如何构建自己的学习笔记系统
    Stream流之List、Integer[]、int[]相互转化
    图解LinkedHashMap原理
    java 手动实现单链表(尾插法和头插法)
    为什么你学不会递归?刷题几个月,告别递归,谈谈我的经验
    谈谈限流算法的几种实现
    使用LinkedHashMap实现一个简易的LRU缓存
    Java深拷贝和浅拷贝
    Excel两列求差集和并集的实现
  • 原文地址:https://www.cnblogs.com/huixch/p/14442209.html
Copyright © 2011-2022 走看看