Drawing lines in C# on scrollable form

Question
Question
Sign in to vote
0
Sign in to vote

I have been drawing some lines on a form (no problem) but when I scroll down along the form (with the form’s scrollbar) the line remains in view. How do I change the line’s position as I scroll with the Form’s scrollbar?

Here’s a basic sample on how a line is drawn (with OnPaint method of the form):

Point[] points = new Point[4];

points[0] = new Point(520, 40);

points[1] = new Point(520, 400);

Graphics g = this.CreateGraphics();

Pen pen = new Pen(Color.Blue, 2);

g.DrawLine(pen, points[0], points[1]);

Essentially I need to be able to change the points to

points[0] = new Point(520 + xScrollValue, 40 + yScrollValue) ;

where xScrollValue and yScrollValue are obtained when I scroll across or down the form.

How can this be done? Or is there a better way?

Thanks

Gerry

Wednesday, February 13, 2008 12:27 PM
Avatar of jn3_16
jn3_1670 Points
Answers

Answers
Question
Sign in to vote
0
Sign in to vote
Hi,

scrollable controls have a property called AutoScrollOffset. It gives away the position of the scrollbar. You can use this to calculate what to draw.

Hope this helps!

Wednesday, February 13, 2008 1:44 PM
Avatar of tuxbear
tuxbear1,380 Points

Question
Sign in to vote
0
Sign in to vote
That is a good idea, now that I have had more time to look at it, this seems to work a little better. using autoscrollofset seemed to cause a lot of jitters… Give this a try (I know its sloppy)

Point[] points = new Point[4];
Graphics g;
Pen pen = new Pen(Color.Blue, 2);
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
g.Clear(this.BackColor );
g.DrawLine(pen, points[0], points[1]);
}
private void Form1_Scroll(object sender, ScrollEventArgs e)
{
points[0] = new Point(520 + this.AutoScrollPosition.X, 40 + this.AutoScrollPosition.Y);
points[1] = new Point(520 + this.AutoScrollPosition.X, 400 + this.AutoScrollPosition.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
points[0] = new Point(520 , 40 );
points[1] = new Point(520 , 400 );
}

Wednesday, February 13, 2008 3:32 PM
Avatar of wadep389
wadep38925 Points
All replies
Question
Sign in to vote
0
Sign in to vote
You could try somthing like this…

private void Form1_Scroll(object sender, ScrollEventArgs e)

{

getpoint(e);

}

private void getpoint(ScrollEventArgs e)

{

Point newpoint = new Point(0, 0);

if (e.ScrollOrientation == ScrollOrientation.VerticalScroll )

{

newpoint.Y += e.NewValue;

points[0].Y += newpoint.Y;

}

else

{

newpoint.X = e.NewValue;

points[0].X = newpoint.X;

}

}

You will also need to add a comparision of the new and old value to determin if you should be adding or subtracting from the points x/y postion.

This probably isn’t an ideal way to do this, I havn’t used drawing in forms very often.

But a quick fix I suppose.

Let me know if this doesn’t help I will try again when I have more time.

-Paul

Wednesday, February 13, 2008 1:42 PM
Avatar of wadep389
wadep38925 Points

Question
Sign in to vote
0
Sign in to vote
Hi,

scrollable controls have a property called AutoScrollOffset. It gives away the position of the scrollbar. You can use this to calculate what to draw.

Hope this helps!

Wednesday, February 13, 2008 1:44 PM
Avatar of tuxbear
tuxbear1,380 Points

Question
Sign in to vote
0
Sign in to vote
That is a good idea, now that I have had more time to look at it, this seems to work a little better. using autoscrollofset seemed to cause a lot of jitters… Give this a try (I know its sloppy)

Point[] points = new Point[4];

Graphics g;

Pen pen = new Pen(Color.Blue, 2);

public Form1()

{

InitializeComponent();

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

g.Clear(this.BackColor );

g.DrawLine(pen, points[0], points[1]);

}

private void Form1_Scroll(object sender, ScrollEventArgs e)

{

points[0] = new Point(520 + this.AutoScrollPosition.X, 40 + this.AutoScrollPosition.Y);

points[1] = new Point(520 + this.AutoScrollPosition.X, 400 + this.AutoScrollPosition.Y);

}

private void Form1_Load(object sender, EventArgs e)

{

g = this.CreateGraphics();

points[0] = new Point(520 , 40 );

points[1] = new Point(520 , 400 );

}

Wednesday, February 13, 2008 3:32 PM
Avatar of wadep389
wadep38925 Points

Question
Sign in to vote
0
Sign in to vote
These are good ideas. Thanks.

The only problem is that I don’t want the line to keep moving along with my scrollbar as I scroll. The above seems to be doing that. I want it to disappear (up) if I scroll down and re-appear when I scroll back up etc…

I’m thinking I need to multiply the length of the line by a factor (between 0 to 1) at OnPaint or something. 0 implies I have scrolled away from the location completely (and thus the line disappears) and 1 implies I am back to the orignal area of the form and the length of the line is restored…

Gerry

Wednesday, February 13, 2008 4:02 PM
Avatar of jn3_16
jn3_1670 Points

Question
Sign in to vote
0
Sign in to vote
Gerry,

My code from the previous post seems to be working like you want? give it a try in a simple project.

Wednesday, February 13, 2008 4:16 PM
Avatar of wadep389
wadep38925 Points

Question
Sign in to vote
0
Sign in to vote

Ooops yes it does work. Thanks all

Gerry

Wednesday, February 13, 2008 5:20 PM
Avatar of jn3_16
jn3_1670 Points

猜你喜欢

转载自blog.csdn.net/wojiuguowei/article/details/120870221