المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : horizontal Panel scrolling - unexpected behaviour



C# Programming
01-15-2010, 07:30 AM
Hi Experts,

I'd like to enable my MyPanel:System.Windows.Forms.Panel to
- scroll vertically on mouse wheel move
- scroll horizontally on [Shift] + mouse wheel move and
- zoom on [Ctrl] + mouse wheel move

Scrolling vertically is Panel's built-in functionality. It's enough to use base.OnMouseWheel(e); for that purpose.


To achieve horizontal scrolling I tried this one:

protected override void OnMouseWheel(MouseEventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
m_iMouseWheelDeltaAccumulator += e.Delta;

int iHorizontalPosition = 0;

while (
m_iMouseWheelDeltaAccumulator < -m_iMouseWheelDeltaThreshold
|| m_iMouseWheelDeltaAccumulator > m_iMouseWheelDeltaThreshold
)
{
iHorizontalPosition = HorizontalScroll.Value;

if (m_iMouseWheelDeltaAccumulator < 0)
{
iHorizontalPosition -= m_iMouseWheelDeltaThreshold;
m_iMouseWheelDeltaAccumulator += m_iMouseWheelDeltaThreshold;
}
else if (m_iMouseWheelDeltaAccumulator > 0)
{
iHorizontalPosition += m_iMouseWheelDeltaThreshold;
m_iMouseWheelDeltaAccumulator -= m_iMouseWheelDeltaThreshold;
}

if (iHorizontalPosition < HorizontalScroll.Minimum)
iHorizontalPosition = HorizontalScroll.Minimum;
else if (iHorizontalPosition > HorizontalScroll.Maximum)
iHorizontalPosition = HorizontalScroll.Maximum;

}

HorizontalScroll.Value = iHorizontalPosition;
}
}

And it does work.

Nearly.

But there are two unexpected points:
- The movement of content and scroll bars differs.
- The panel doesn't scroll the same distance every time I scroll one mouse wheel step. The distance gets bigger at first. Then, after ten or so steps, it gets smaller for the same amount of steps. And then it gets bigger again. The scrolling distance seems to oscillate (without the negative alternation).

How do I achieve a "normal" behaviour, like in OpenOfficeOrg, for example?


Ciao,


luker