从鼠标滚轮事件获取滚动条位置 [英] Getting Scrollbar position from Mouse wheel event
问题描述
我在winform中有一个小组。我想捕获面板的滚动和鼠标滚轮事件。对于这两种情况,我想检查滚动条位置。
当滚动条位于底部时,控件应该触发事件。
我为Panel.Scroll做了这样的事情:
I have a panel in winform. I want to capture both scroll and mouse wheel event for the panel. For both scenario I want to check the scroll bar position.
When scroll bar is at bottom the control should fire the event.
I have done this for Panel.Scroll like this:
private void Panel1_Scroll(object sender, ScrollEventArgs e)
{
if (e.NewValue == Panel1.VerticalScroll.Maximum-Panel1.VerticalScroll.LargeChange+1)
{
//do some operation
}
}
但是对于MouseEventArgs,没有值(e.newvalue)表示滚动条位置。
如何从鼠标滚轮事件中获取滚动条位置?
另外根据我的要求,两个事件调用都有相同的逻辑实现,所以我想写逻辑一次。
我怎样才能实现这个目标?
But for MouseEventArgs there is no value (e.newvalue) to indicate scrollbar position.
How can I get the Scrollbar position from mouse wheel event ?
Also as per my requirement both event call have same logic implementation, so I want to write the logic once.
How can I achieve this ?
推荐答案
在你的方法中你可以检查panel1.VerticalScroll.Value
- 它相当于e.NewValue
如果你想只写一次逻辑然后引入例程,例如
Within your method you can examinepanel1.VerticalScroll.Value
- it's the equivalent ofe.NewValue
If you want to just write the logic once then introduce a routine e.g.
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1_scrollcheck(e.NewValue);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
panel1_scrollcheck(panel1.VerticalScroll.Value);
}
private void panel1_scrollcheck(int currPos)
{
if (currPos == panel1.VerticalScroll.Maximum - panel1.VerticalScroll.LargeChange + 1)
{
//do some operation
}
}
假设你有一个Panel,'panel1,其滚动条在运行时有效 - 时间和一个TextBox,'textBox1:
Assuming you have a Panel, 'panel1, which has scrollbars active at run-time, and a TextBox, 'textBox1:
// in Form Load event or form contructor
panel1.MouseWheel += Panel1OnMouseWheel;
private void Panel1OnMouseWheel(object sender, MouseEventArgs e)
{
textBox1.Text = string.Format("mouse: {0} wheel-delta: {1} panel scroll: {2}", e.Location.ToString(), e.Delta, panel1.VerticalScroll.Value);
}
你尝试过这样的事吗?发生了什么事?
典型的e.Delta值为#120:车轮向前移动,#-120车轮向后移动。
Have you tried working with something like this ? What happened ?
Typical e.Delta values are #120: wheel moved forward, and #-120 wheel moved back.
这篇关于从鼠标滚轮事件获取滚动条位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!