Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the scrolling offset when storing coordinates

Tags:

c#

I am working on a Form that takes and draws a point on a mouse click. I am confused about how to properly get and add the scrolling offset so that the points can be drawn correctly. For example, right now when I add a point where the upper left coordinate is (0,0), the point redraws itself and moves with the scrolling action instead of staying at the spot it was originally created at. I have set

this.AutoScroll = true

and have set the minimum size manually

this.AutoScrollMinsSize = new Size(800,600);

Here is what my mouse click event looks like so far:

if (e.Button == MouseButtons.Left)
{
  Point newPoint = new Point(e.X, e.Y);
  p.X += this.AutoScrollOffset.X;
  p.Y += this.AutoScrollOffset.Y;
  this.Invalidate();
}

What is the proper way to use the AutoScrollOffset property to keep the points where they belong instead of moving as I scroll?

I should add that my program also overrides the Scroll event to repaint when a scroll event occurs to fix the problem of a drawing disappearing once the visible area was left.

like image 432
Elaine B Avatar asked Jun 08 '26 03:06

Elaine B


1 Answers

AutoScrollOffset is not the correct property to use. It has very limited use, it can apply an offset to the scroll position when the ScrollControlIntoView() method is used. Which is pretty rare, never once used it myself.

You need to use the AutoScrollPosition property instead:

    if (e.Button == MouseButtons.Left) {
        var newPoint = new Point(e.X - this.AutoScrollPosition.X,
                                 e.Y - this.AutoScrollPosition.Y);
        // etc..
    }

Note that substraction is required, a bit unintuitive.

like image 53
Hans Passant Avatar answered Jun 10 '26 16:06

Hans Passant