2012-10-23 278 views
1

我有一个面板,其AutoScroll = true;鼠标滚轮滚动水平

余可使用滚动条滚动面板。

我也写找到滚轮“垂直滚动”和“鼠标”滚轮:

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

不过,我想用“随心所欲鼠标+转移”太水平滚动。

我需要做什么才能发生?

+0

可能重复http://stackoverflow.com/questions/7828121/shift -mouse-wheel-horizo​​ntal-scroll) –

回答

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#
[移+鼠标滚轮水平滚动](的
+0

我在论坛上看到了这个答案,但我无法理解它:S你能解释一下吗? –

+0

此条目“[DllImport(”user32.dll“,CharSet = CharSet.Auto)]”将放置在哪里?还有richTextBox?我们为什么用它 ? –

+0

我问,因为当我写这个:[DllImport(“user32.dll”,CharSet = CharSet.Auto)]此条目不编译 –