Scrollbar mystery

G

Guest

I have a mystery where my scrollbar behaves differently from when I use it's
slider/arrow keys compared to when I manipulate it's value with a
onMouseWheel event...

When I manipulate my scrollbar's controls (the slider or the arrow key) to
go to the bottom, Once it reaches the bottom and cannot go any further I
somehow lose 9 pixels of height, so when it refreshes my image the bottom 9
pixels are clipped off.

But if I use my MouseWheel event and wheel down to the bottom, it lines up
perfectly. The MouseWheel event just takes the Delta and modified the
scorllbar.Value property directly, not allowing it to go > max or < min.

So why does this happen?

The only properties I change are the scrollbar's Maximum and Minimum values,
where Minimum is 0 and Maximum is my image's height subtracted by the height
of the client size rectangle. Everything else left at default.
 
G

Guest

I created a small sample app which demonstrates the behavior I am talking
about, since it's really hard for me to explain in text.

create a WindowsApplication and add my control like this:

ScrollControl s = new ScrollControl();
s.Location = new Point(10, 10);
s.Size = new Size(250, 250);
this.Controls.Add(s);

here is the control itself:

public partial class ScrollControl : UserControl
{
Bitmap bmp;

public ScrollControl()
{
InitializeComponent();

bmp = new Bitmap(400, 400);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Black, 0, -vScrollBar1.Value, 400, 400);
Pen p = new Pen(Brushes.Red, 10);
g.DrawRectangle(p, 0, -vScrollBar1.Value, 400, 400);
g.Dispose();

vScrollBar1.Scroll += new ScrollEventHandler(vScrollBar1_Scroll);
}

protected override void OnPaint(PaintEventArgs e)
{
Point loc = new Point(0, -vScrollBar1.Value);
e.Graphics.DrawImageUnscaled(bmp, new Rectangle(loc, bmp.Size));
}

public override void Refresh()
{
this.OnPaint(new PaintEventArgs(this.CreateGraphics(), new
Rectangle(new Point(0, 0), this.ClientSize)));
}

protected override void OnSizeChanged(EventArgs e)
{
vScrollBar1.SetBounds(ClientRectangle.Right - vScrollBar1.Width,
0, vScrollBar1.Width, ClientRectangle.Height);
vScrollBar1.Maximum = bmp.Height - ClientSize.Height;
}

void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
Refresh();
}

protected override void OnMouseWheel(MouseEventArgs e)
{
int move = e.Delta / 10 * -1;
if (move > 0)
{
if (vScrollBar1.Value <= vScrollBar1.Maximum &&
vScrollBar1.Value + move >= vScrollBar1.Maximum)
vScrollBar1.Value = vScrollBar1.Maximum;
else
vScrollBar1.Value += move;
}
else
{
if (vScrollBar1.Value >= vScrollBar1.Minimum &&
vScrollBar1.Value + move <= vScrollBar1.Minimum)
vScrollBar1.Value = vScrollBar1.Minimum;
else
vScrollBar1.Value += move;
}
Refresh();
}
}
 
G

Guest

Alex Fu said:
int move = e.Delta * SystemInformation.MouseWheelScrollLines / 120;

Ah, that makes the mouse wheel scrolling alot smoother, thanks!

Just that, it didnt solve the problem with the normal scroll bar control...

notice how when you you the mouse wheel to go to the very bottom of the
scroll area, you will be able to see the bottom red border?

now try doing the same thing using the scroll bar control, either the down
arrow or the thumb tab. It wont let you each the actual bottom so you can
never see that bottom red border...

why is that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top