hi
please i want to use mouse move event in windows form to get mouse coordinates to show it in the form.and using this coordinates use the events mouse up and mouse down to draw a line .
thanks

Recommended Answers

All 3 Replies

Sounds like homework :) lol.

This is so easy. rig up your mouse down and mouse up events and create class variables of type Point. Then get the mouse coordinates with the event arguments on mouse down, save it to the first. then on mouse up get the mouse coordinates with the event arguments and save it to the second Point var. then rig up an onPaint event and simply call the drawLine method of the graphics class from the first to the 2nd point. and don't forget to invalidate the form at then end of the mouse up.

Best of luck. This is too easy to just post the answer. Give it a shot. you can handle it.

commented: Good answer! +6

Totally agree with diamonddrake on this one. This is a very simple problem if you break it down. The policy here at daniweb is to help those who try first.
IF your new to coding, the first step should be to figure out the steps you need to accomplish. This is usually done in pseudocode so that you dont get caught up in language specifics and syntax just yet:

- Detect mouse button pressed
- Store location of mouse as starting point
- Detect mouse button released
- Store location of mouse as ending point
- Draw line from start point to end point

You will need to look at the MouseEventArgs paramter of your event handlers. The MouseDown and MouseUp events include an X and Y parameter in the Arguments. Then check out msdn tutorial for drawing a line on the form.

commented: Nice addition to what Diamonddrake said. +6
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace MouseLinea
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //

        }

        private static void MainFormMouseClick(object sender, MouseEventArgs e)
        {

    //  private static void Form1_MouseClick(object sender, MouseEventArgs e)
    //   {
            int x = e.X;
            int y = e.Y;

           int xi =0;
            int yi =0;
           int xf =0;
           int yf =0;

            string buttonStr = "";

                    System.Drawing.Graphics graphicsObj;
                    graphicsObj = this.CreateGraphics();
                    Pen myPen = new Pen(System.Drawing.Color.Red, 1);
           //       graphicsObj.DrawLine(myPen, xi, yi, xf, yf);
            switch (e.Button)
            {
                case MouseButtons.Right:
                    buttonStr = " Derecho ";
                    xf  = x;
                    yf = y;
                  graphicsObj.DrawLine(myPen, 50, 80, xf, yf);
               //   infoLabel.Text = "Click con el botón en: " + buttonStr + xi + " , " + yi;
                    break;
                case MouseButtons.Left:
                    buttonStr = " Izquierdo ";
                    xi = x;
                    yi = y;
                  graphicsObj.DrawLine(myPen, xi, yi, 150, 180);
                //  infoLabel.Text = "Click con el botón en: " + buttonStr + xf + " , " + yf;
                    break;
                case MouseButtons.Middle:
                    buttonStr = " Centro ";
                    break;


            }
            //  graphicsObj.DrawLine(myPen, 20,10,200,230);
            //  graphicsObj.DrawLine(myPen, xi, yi, xf, yf);
            //  infoLabel.Text = "Click con el botón en: " + buttonStr + xf + " , " + yf;
           infoLabel.Text = "Click " + buttonStr + xi + " , " + yi + "Click" + buttonStr + xf + " , " + yf;     
        }



        void MainFormLoad(object sender, EventArgs e)
        {

        }

        void InfoLabelClick(object sender, EventArgs e)
        {

        }

         void MainFormPaint(object sender, PaintEventArgs e)
// private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // System.Drawing.Graphics graphicsObj;

            // graphicsObj = this.CreateGraphics();

            // Pen myPen = new Pen(System.Drawing.Color.Red, 5);

            //graphicsObj.DrawLine(myPen, 10, 20, 200, 210);
           // graphicsObj.DrawLine(myPen, xi, yi, xf, yf);
        }



    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.