[英]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.