removing drawn lines on mousemove

sagey

New member
Joined
Jan 6, 2004
Messages
3
im trying to draw crosshairs that follow the mouse around the screen. The following code works. but not quite, it draws a new crosshair for every mouse move, but doesnt remove the old one, so i end up with a screen full of vertical and horizontal lines.

how do i remove the last lines drawn on every mouse move?


Code:
		public void MyMouseMove( Object sender, MouseEventArgs e )
		{	
			
			Pen blackPen = new Pen(Color.FromArgb(128,0,0,255), 1);
			Graphics g = SelectionRectangle.ActiveForm.CreateGraphics();
			// Create coordinates of points that define line.
			float formLeft = this.Left;
			float formTop = this.Top ;
			float formRight = this.Right ;
			float formBottom = this.Bottom ;
			mouseX = e.X ;
			mouseY = e.Y ;
			// Draw line to screen.
			g.DrawLine(blackPen,0,mouseY,mouseX,mouseY );
			g.DrawLine(blackPen, mouseX, 0, mouseX,mouseY);
			g.DrawLine(blackPen, formRight, mouseY, mouseX, mouseY);
			g.DrawLine(blackPen, mouseX, formBottom,mouseX, mouseY);
 
I would probably approach it differently.

I would have the crosshair on its own surface/graphcis object, and then blit that over to the desired X/Y location each frame.

No matter how you do it, youll probably have to rebuild your primary surface from its component layers each frame, otherwise youll have residue from previous frames sitting around.

-Hiro_Antagonist
 
Take a look at ControlPaint.DrawReversibleLine().

It is in screen coordinates but the transformation is pretty simplistic. It will do what you want.
 
Back
Top