【问题标题】:How to prevent focused control from scrolling when the mouse isn't over it?当鼠标不在焦点上时,如何防止焦点控件滚动?
【发布时间】:2015-12-08 00:29:58
【问题描述】:

请参阅this prior related question。虽然那里的答案确实有效,但在涉及某些类型的控件(例如TDBGrid)时,我还有其他问题。如果TDBGrid 当前有焦点,但鼠标指向另一个控件以滚动,则TDBGrid 无论如何都会滚动,从而导致两个不同的控件同时滚动。到目前为止,我发现的每一个与滚动鼠标下方的控件相关的解决方案都会发生这种情况。

我怎样才能防止这种行为,并确保只有鼠标下的控件滚动,而没有其他?

【问题讨论】:

  • 其他滚动控件的所有事件处理程序是否在所有情况下都设置了 Handled:=true?如果不是,那是消息处理系统寻找“其他人”来完成工作的自然行为,即集中控制。事实上,所有控件是否都有您编写的 MouseWheel 事件处理程序,或者有些应该像 TMemo 或 TListView 一样自行工作?其中一些有实现,他们必须专注于对鼠标滚轮事件做出反应。
  • 做一些调试。找出两个控件处理鼠标滚动消息的原因。

标签: delphi scroll mousewheel


【解决方案1】:

这段代码对我来说很好用。

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
  Control: TWinControl;
begin

  if Msg.message = WM_MOUSEWHEEL then
  begin
    // Find control at mouse cursor
    Control := FindVCLWindow(Msg.pt);

    if Assigned(Control) then
    begin
      // Try to scroll
      if Control.Perform(CM_MOUSEWHEEL, Msg.wParam, Msg.lParam) <> 0 then
        Handled := True
      else
      // If no scroll was performed by control,
      // then detrmine if message control is at mouse cursor.
      // If not, then supress message
      if Control.Handle <> Msg.hwnd then
        Handled := True;
    end;
  end;

end;

【讨论】:

  • 稍微解释一下会提高你的回答质量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2020-07-02
相关资源
最近更新 更多