Results 1 to 13 of 13

Thread: x,y lines following mouse

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    x,y lines following mouse

    i'm using a selection rectangle to select an area. to make it easier to select the area i make the cursor a crosshair.

    what i would really like to do is for there to be four lines that follow the mouse cursor

    Code:
                   y line
                      |
                      |                   
    x Line            |
    ------------ --  -- -------------------
                      |
                      |
                      |
    that way the user can easily see the area he is about to select before he selects it.

    I'm not really sure how to do this. i can get the x,y location of the mouse easy enough.

    and i guess i'd have to use the mousemove event to constantly re-get the x,y position.

    but i'm not sure how to go about drawing the lines, and putting the rest of it together.

    any pointers would be gratefully received

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    that diagram doesn't reflect what i mant to represent. it was meant to be a cross hair with an x and y line.

    sorry

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    What you want to do is very easy to do it I understood it well...you'll just have to put in the MouseMove event it to draw a vertical line and a horizontal line:

    the vertical line:
    point A(
    mouse X's cordinate,
    0)

    point B(
    mouse X's cordenate,
    picture's height)

    the horizontal line:
    point A(
    mouse y's cordinate,
    0)

    point B(
    mouse y's cordinate,
    width)

    Hope i was clear enough
    \m/\m/

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You have to erase the old line, too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    i've wrriten some basic code like this:

    the drawline bits are correct, but i'm not sure how to use this method with the mousemove method and the paint event.

    i guess every time there is a mousemove event i need to call the method below (or something like it)
    but i'm not sure how to do it.

    Code:
    		public void DrawLineFloat(PaintEventArgs pe)
    		{
    			// Create pen.
    			Pen blackPen = new Pen(Color.FromArgb(128,0,0,255), 1);
    			// Create coordinates of points that define line.
    			float formLeft = this.Left;
    			float formTop = this.Top ;
    			float formRight = this.Right ;
    			float formBottom = this.Bottom ;
    			// Draw line to screen.
    			pe.Graphics.DrawLine(blackPen,0,mouseY,mouseX,mouseY );
    			pe.Graphics.DrawLine(blackPen, mouseX, 0, mouseX,mouseY);
    			pe.Graphics.DrawLine(blackPen, formRight, mouseY, mouseX, mouseY);
    			pe.Graphics.DrawLine(blackPen, mouseX, formBottom,mouseX, mouseY);
    		}

    I did muck around with using the form.refresh method, but that just meant the whole form being andreloaded (i open an image in the onpaint handler) and was crap.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't use PaintEvent here. Put the code in the mousemove handler and obtain a Graphics differently, I think Form has a method to do that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    yeah thats sort of my question.

    i'd thought of putting the code in the mousemove, but i'm unsure how to obtain a graphics object etc so i can do the drawline.

    i feel like i'm being really stupid.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's what a reference is for

    Form.CreateGraphics
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    Cheers, thats the ****er i was after. got it working of sorts. now just need to delete the existing line on every mousemove

    Code:
    			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);

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    From what I've read in the forums pictures seem to have 2 properties, in the back one you can put the image and in the other you can draw your lines. That way you just delete the upper one and draw the lines
    \m/\m/

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I recommend using the invert ROP2 mode (R2_NOT in plain API) to draw the line and erase it again.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    I'm suprised there isn't a provided class for drawing BoundingBoxes like that....

    this code doesn't work but it's has an interesting effect...
    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace BoundingBox
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(484, 505);
    			this.Cursor = System.Windows.Forms.Cursors.Cross;
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    			this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
    			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    		private bool isMouseDown = false;
    		private System.Drawing.Drawing2D.GraphicsState orgState = null;
    		private System.Drawing.Drawing2D.GraphicsState oldState = null;
    		private PointF origanPoint;
    
    		private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    		{
    			Graphics g = this.CreateGraphics();
    			if(orgState != null)
    			{	
    				g.Restore(orgState);
    				g.Flush();
    				orgState = null;
    				oldState = null;
    			}
    			isMouseDown = true;
    			orgState = g.Save();
    			oldState = g.Save();
    			origanPoint = new PointF(e.X, e.Y);
    		}
    
    		private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    		{
    			isMouseDown = false;
    		}
    
    		private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    		{
    			if(isMouseDown)
    			{
    				Graphics g = this.CreateGraphics();
    				g.Restore(oldState);
    				g.Flush();
    				g.DrawRectangle(System.Drawing.Pens.Black,origanPoint.X,origanPoint.Y,(e.X - origanPoint.X),(e.Y - origanPoint.Y));
    				oldState = g.Save();
    			}
    		}
    	}
    }
    Magiaus

    If I helped give me some points.

  13. #13
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    well drop this in an empty form name Form1 and you got a box drawing machine. not the best though because it will erase stuff that might not need to be erased...... in the old days i would have back buffered an image of the form or ctl but now thats to much stuff to figure out cause I haven't used the drawing namespace much.....
    Code:
    		private bool isMouseDown = false;
    		private PointF origanPoint;
    
    		private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    		{
    			//Graphics g = this.CreateGraphics();
    			isMouseDown = true;
    			origanPoint = new PointF(e.X, e.Y);
    		}
    
    		private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    		{
    			isMouseDown = false;
    		}
    
    		private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    		{
    			if(isMouseDown)
    			{
    				Graphics g = this.CreateGraphics();
    				g.Clear(this.BackColor);
    				g.DrawRectangle(System.Drawing.Pens.Black,origanPoint.X,origanPoint.Y,(e.X - origanPoint.X),(e.Y - origanPoint.Y));
    			}
    		}
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width