【问题标题】:Disable/Enable mouse wheel scroll for a panel禁用/启用面板的鼠标滚轮滚动
【发布时间】:2015-02-05 08:38:10
【问题描述】:

在我的 WinForm 中有一个带有一些网格的面板,网格也有滚动条。我想使用鼠标滚轮滚动每个网格并使用 Shift+scroll 滚动面板。 试过这个:

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

还有这个:

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);
        }
    }

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

这是禁用滚动但不启用它回来。我的意思是:如果我先执行 Shift+scroll,则可以在面板上滚动。只需滚动,它会禁用面板滚动,所以我可以滚动网格。但是如果我再次使用 Shift+scroll 则在面板中滚动将不起作用。

禁用后如何启用面板回滚?

【问题讨论】:

    标签: c# winforms scroll mousewheel


    【解决方案1】:

    [编辑] 好的,我把代码留在这里。但事实是:在鼠标滚动期间按 shift 键会对父面板产生影响是一种标准行为。没有什么可做的了。

    这里应该可以工作。

    不足之处在于您必须对您应该放入面板中的所有类型的组件进行此修改。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多