鼠标滚轮水平滚动

时间:2012-10-23 16:16:50

标签: c# focus panel mousewheel

我有一个AutoScroll = true;

的小组

我可以使用滚动条滚动面板。

我还通过书写找到鼠标滚轮“垂直滚动”和“鼠标”滚轮:

void panelInner_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
    panelInner.Focus();
}

但是,我想通过“转动鼠标+移位”水平滚动。

我需要做些什么才能实现?

1 个答案:

答案 0 :(得分:2)

在您的设计器文件中,您需要手动添加MouseWheel事件委托。

this.panelInner.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panelInner_MouseWheel);

然后,在您的代码中,您可以添加以下内容。

    private const int WM_SCROLL = 276; // Horizontal scroll 
    private const int SB_LINELEFT = 0; // Scrolls one cell left 
    private const int SB_LINERIGHT = 1; // Scrolls one line right

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); 

    private void panelInner_MouseWheel(object sender, MouseEventArgs e)
    {
        if (ModifierKeys == Keys.Shift)
        {
            var direction = e.Delta > 0 ? SB_LINELEFT : SB_LINERIGHT;

            SendMessage(this.panelInner.Handle, WM_SCROLL, (IntPtr)direction, IntPtr.Zero);
        }
    }

参考文献:

  1. Shift + mouse wheel horizontal scrolling
  2. Mouse tilt wheel horizontal scrolling in C#