当鼠标不在鼠标上方时,如何防止焦点控件滚动? [英] How to prevent focused control from scrolling when the mouse isn't over it?

查看:100
本文介绍了当鼠标不在鼠标上方时,如何防止焦点控件滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅此先前相关的问题.虽然答案确实有效,但是当涉及某些类型的控件(例如TDBGrid)时,我还有其他问题.如果TDBGrid当前具有焦点,但是将鼠标指向另一个控件进行滚动,则TDBGrid仍将滚动,从而导致两个不同的控件同时滚动.到目前为止,我发现的每个解决方案都与在鼠标下方滚动控件有关.

Refer to this prior related question. While the answers there do work, I have further issues when it comes to certain types of controls such as a TDBGrid. If the TDBGrid currently has focus, but the mouse is pointed over another control to scroll, the TDBGrid scrolls anyway, thus resulting in two different controls scrolling at the same time. This happens in every single solution which I've found so far related to scrolling the control underneath the mouse.

如何防止这种行为,并确保鼠标下的控件滚动,而没有其他设置?

How can I prevent this behavior and ensure that only the control under the mouse scrolls, and nothing else?

推荐答案

此代码对我来说很好.

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;

这篇关于当鼠标不在鼠标上方时,如何防止焦点控件滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆