简体   繁体   中英

Disable/Enable mouse wheel scroll for a panel

In my WinForm there a is Panel with some grids, grids have scroll bar too. I wanted to scroll each grid using mouse wheel and scroll the panel using 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; in Initialization.

This is disabling the scroll but not enabling it back. I mean: If I do Shift+scroll first, scroll works on panel. Just do Scroll, it is disabling the panel scroll so, I can scroll the grid. But if I do Shift+scroll again then scroll in panel is not working.

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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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