Jump to content

simple draw line

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello

I am trying to have the program draw on the form itself, where the user clicks and holds the mouse down (mouseevent) then the user moves the mouse to any spot on the form and does mouse up (or releases the left mouse button) and it draws a blue line from mouse down to mouse up, i have the code below, looks like it works, but when i run it and try it, its not, it just has all the points 0,0,0,0 but here is the code below, hope this makes sense what i want to do, this is for training on gui interfaces etc. but i am learning C# networking with this.

thanks in advance:



public int mouseupx = 0, mouseupy = 0, mousedownx = 0, mousedowny = 0;

        public Point location1 = new Point();

        public Point location2 = new Point();



        private void Form1_MouseDown(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                mousedownx = location1.X;

                mousedowny = location1.Y;

            }


        }


        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            Graphics surface;

            Pen pen = new Pen(Color.Blue, 2.0f);

            surface = this.CreateGraphics();

            mouseupx = location2.X;

            mouseupy = location2.Y;

            surface.DrawLine(pen, mousedownx, mousedowny, mouseupx, mouseupy);

        }


Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#2
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hey everyone

I got what i was looking for, well sorta, the good news is that it is working, but the problem is, its not really acurate, what i mean by that is when you do left click hold and mouse up, the line shows up more down to the bottom of the form, wondering how i can get it more acurate where the mouse was, here is the code below that works but is not acurate, any ideas?


public int mouseupx = 0, mouseupy = 0, mousedownx = 0, mousedowny = 0;

private void Form1_MouseDown(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                mousedownx = MousePosition.X;

                mousedowny = MousePosition.Y;

                label1.Text = mousedownx.ToString();

                label2.Text = mousedowny.ToString();


            }


        }


        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            Graphics surface;

            Pen pen = new Pen(Color.Blue, 2.0f);

            surface = this.CreateGraphics();

            mouseupx = MousePosition.X;

            mouseupy = MousePosition.Y;

            surface.DrawLine(pen, mousedownx, mousedowny, mouseupx, mouseupy);

        }


Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#3
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
Okay, it's simple. First post had wrong assignments but I see you managed that. Let me explain the second posted code. MousePosition is not what I would use.

Try using e.Location within those mouse events. MousePosition is working in screen-coordinates, as MSDN documentation explains, while the event gives you form-coordinates.

A hint, try using Point coordinates. Don't use separate variables for x/y because it makes code longer than it has to be. That's it.

Now another objective assignment for you, if you wanna do it. Try making a whole List of point-pairs (even make a class for that) and let the program draw all the lines. After that maybe add some Save button that will save that list of lines into XML file? Good hunting.

#4
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Yay got it working thank you, here is the code below before i forget, but you said to use point though? let me know how you would use that? would I have to create 2 point objects and point object getting the mousedown and the other point object getting the mouseup?

if that is it its not working, why i dont know, but the e.location.x is working very acruatly, like a champ thanks :) but before i say this is done, maybe you can let me know what i am doing wrong with the point objects so it will be knowledgable for all to see.

thanks again

here is the code below, fyi the code that is commented out works



public int mouseupx = 0, mouseupy = 0, mousedownx = 0, mousedowny = 0;

        public Point location1 = new Point();

        public Point location2 = new Point();


private void Form1_MouseDown(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                /*

                mousedownx = e.Location.X;

                mousedowny = e.Location.Y;

                 */

                mousedownx = location1.X;

                mousedowny = location1.Y;


                label1.Text = mousedownx.ToString();

                label2.Text = mousedowny.ToString();


            }


        }


        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {


            Graphics surface;

            Pen pen = new Pen(Color.Blue, 2.0f);

            surface = this.CreateGraphics();

            mouseupx = location2.X;

            mouseupy = location2.Y;


            /*

            mouseupx = e.Location.X;

            mouseupy = e.Location.Y;

             */

            surface.DrawLine(pen, mousedownx, mousedowny, mouseupx, mouseupy);

        }


Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#5
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
                /*

                mousedownx = e.Location.X;

                mousedowny = e.Location.Y;

                 */

Here you are referring to e parameter which is given by those mouse events, it contains e.Location member that has a valid point coordinate.

                mousedownx = location1.X;

                mousedowny = location1.Y;

Here you are referring to a local variable, that you created yourself, and you never assigned anything to it. It contains zero because you never assigned anything else to it.


Point location1 = e.Location; //when mousedown

Point location2 = e.Location; //when mouse up

//and then you will figure out what to do with it


Some sample code for ya. Will boost you up.