簡體   English   中英

禁用/啟用面板的鼠標滾輪滾動

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

在我的WinForm中,有一些網格的Panel,網格也有滾動條。 我想使用鼠標滾輪滾動每個網格,並使用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,則滾動會在面板上起作用。 只要執行Scroll,它就會禁用面板滾動,因此,我可以滾動網格。 但是,如果我再次執行Shift + scroll,則“在面板中滾動”不起作用。

禁用后如何啟用面板回滾?

[編輯]好的,我將代碼留在這里。 但是事實是:這是一種標准行為,即在鼠標滾動期間按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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM