简体   繁体   English

禁用/启用面板的鼠标滚轮滚动

[英]Disable/Enable mouse wheel scroll for a panel

In my WinForm there a is Panel with some grids, grids have scroll bar too. 在我的WinForm中,有一些网格的Panel,网格也有滚动条。 I wanted to scroll each grid using mouse wheel and scroll the panel using Shift+scroll. 我想使用鼠标滚轮滚动每个网格,并使用Shift + scroll滚动面板。 Tried this: 试过这个:

private void sitePnlGrid_MouseWheel(object sender, MouseEventArgs e)
    {
                if (Control.ModifierKeys == Keys.Shift)
                   this.sitePnlGrid.DisableScroll = false;
                else
                   this.sitePnlGrid.DisableScroll = true;
    }

And this: 和这个:

public class CustomScrollPanel : Panel
    {
        public bool DisableScroll { get; set; }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x20a && DisableScroll==true) return; 
            base.WndProc(ref m);
        }
    }

Set this.sitePnlGrid.DisableScroll = false; 设置this.sitePnlGrid.DisableScroll = false; in Initialization. 在初始化中。

This is disabling the scroll but not enabling it back. 这将禁用滚动,但不会将其重新启用。 I mean: If I do Shift+scroll first, scroll works on panel. 我的意思是:如果我先进行Shift + scroll,则滚动会在面板上起作用。 Just do Scroll, it is disabling the panel scroll so, I can scroll the grid. 只要执行Scroll,它就会禁用面板滚动,因此,我可以滚动网格。 But if I do Shift+scroll again then scroll in panel is not working. 但是,如果我再次执行Shift + scroll,则“在面板中滚动”不起作用。

How to enable the panel scroll back once it is disabled? 禁用后如何启用面板回滚?

[EDIT] Ok, I leave my code here. [编辑]好的,我将代码留在这里。 But the fact is : it is a standard behavior that pressing the shift key during a mouse scroll will have effect on the parent panel. 但是事实是:这是一种标准行为,即在鼠标滚动期间按Shift键会在父面板上生效。 There is nothing more to do. 没什么可做的了。

Here something that should work. 在这里应该起作用。

The lack is that you have to do this modification for all the types of components that you should want to put in your panel. 缺少的是您必须对应该放入面板中的所有类型的组件进行此修改。

class MyDataGridView : DataGridView
{
    protected override void WndProc(ref Message m)
    {
        // If the message is for this component, is about mouse wheel
        // and if the shift key is pressed,
        // we transmit it to the parent control.
        // Otherwise, we handle it normally.
        if ((m.HWnd == Handle) && (m.Msg == WM_MOUSEWHEEL) && (ModifierKeys == Keys.Shift))
        {
            PostMessage(Parent.Handle, m.Msg, m.WParam, m.LParam);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    #region User32.dll
    [DllImport("User32.dll")]
    private static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    private const int WM_MOUSEWHEEL = 0x020A;
    #endregion
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM