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();
}
}