问题描述
使用Shift +滚轮的横向滚动相当普遍。
The use of shift + scroll wheel is fairly common for horizontal scrolling.
。这都是很容易捕捉。我可以使用鼠标滚轮事件标志由在KeyDown,的KeyUp事件设置跟踪时Shift键是pressed的。
Both of those are fairly easy to capture. I can use the MouseWheel event with a flag set by the KeyDown, KeyUp events to keep track of when the shift key is pressed.
不过,我怎么居然触发水平滚动?我知道WM_MOUSEHWHEEL的,可以是用来触发事件?
However, how do I actually trigger the horizontal scrolling? I am aware of WM_MOUSEHWHEEL, can that be used to trigger the event?
更新:对于 System.Windows.Form
有一个 HorizontalScroll
属性,它是类型的 HScrollProperties
。你可以操纵这些对象的值
属性来更改水平滚动条的位置。然而,到目前为止,我还没有发现任何其他控件上的对象是可用的。
Update:For a System.Windows.Form
there is a HorizontalScroll
property that is of type HScrollProperties
. You can manipulate the Value
attribute on that object to change the horizontal scrollbar's position. However, so far I haven't spotted any other controls on which that object is available.
推荐答案
在你的设计师文件,你需要手动添加鼠标滚轮事件委托。
In your designer file, you'll need to manually add a MouseWheel event delegate.
this.richTextBox.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.RichTextBox_MouseWheel);
然后,在你的背后code,你可以添加以下内容。
Then, in your code behind, you can add the following.
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 RichTextBox_MouseWheel(object sender, MouseEventArgs e)
{
if (ModifierKeys == Keys.Shift)
{
var direction = e.Delta > 0 ? SB_LINELEFT : SB_LINERIGHT;
SendMessage(this.richTextBox.Handle, WM_SCROLL, (IntPtr)direction, IntPtr.Zero);
}
}
有关的常量值的详细信息,请参阅下面的这样:How我以编程方式滚动的WinForms控制?
For more information on the const values, see the following SO: How do I programmatically scroll a winforms control?
使用Alvin's溶液的如果可能的。它的的更好的方式的
Use Alvin's solution if possible. It's way better.
这篇关于按住Shift +鼠标滚轮水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!