Hi all,

I am trying to get the Mouse Coordinates that are relative to my form. At the moment I am using:

xs = Cursor.Position.X;
ys = Cursor.Position.Y;

However this doesn't get the "local" form mouse coordinates. My form is x = 640 y = 480. And I want to be able to take the x and y positions when a user clicks down.

Thanks

-Erslich

Try this snippet:

private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            label1.Text = "Local position is " + e.X + ", " + e.Y + ".";
            label2.Text = "Global position is " + Cursor.Position.X + ", " + Cursor.Position.Y + ".";
        }

That's how I got it working!

Thanks :)

hey, thanks for supplying this thread. i've enhanced the codings a little so that i get 2 seperate coordinates.

namely:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
txtMouseX.Text = e.X.ToString();
txtMouseY.Text = e.Y.ToString();
}

and :

bool MouseClicked = true;

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{

if (MouseClicked == true)
{
this.label1.Text = "first point " + e.X + ", " + e.Y + ".";

MouseClicked = false;
}
else
{
this.label2.Text = "second point " + e.X + ", " + e.Y + ".";
}
}

i would like to know how do i compare this 2 coordinates. like what are the difference between the points. thanks in advance.

Try this:

bool MouseClicked = false;
        double x1;
        double y1;

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (MouseClicked)
            {
                this.label2.Text = "second point " + e.X + ", " + e.Y + ".";
                MouseClicked = false;

                double x2 = Convert.ToDouble(e.X);
                double y2 = Convert.ToDouble(e.Y);
                double dist = Math.Sqrt(Math.Pow((x1 - x2), 2.0) + Math.Pow((y1 - y2), 2.0));
                label5.Text = "From " + x1.ToString() + "," + y1.ToString() + " to " +
                    x2.ToString() + "," + y2.ToString() + " is " + dist.ToString() + " pixels.";
                label6.Text = (x1 - x2).ToString() + " is the change in X.";
                label7.Text = (y1 - y2).ToString() + " is the change in Y.";
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (!MouseClicked)
            {
                this.label1.Text = "first point " + e.X + ", " + e.Y + ".";
                MouseClicked = true;
                x1 = Convert.ToDouble(e.X);
                y1 = Convert.ToDouble(e.Y);
                label5.Text = "";
                label6.Text = "";
                label7.Text = "";
            }
        }

You may need to declare x2,y2 outside of the Mouse_Up event, if you need them to persist a bit longer...

sorry, let me try to elaborate my problem. i have 2 point. point 1's coordinates are 119, 201. and point 2's coordinates are 119, 217. but both of the points are stored in a label. how can i extract both points and do a calculation of the differences?

If you had a label that was formatted like this:

119,201,119,217

You could parse it like this:

using System.Text.RegularExpressions;

string[] coords = Regex.Split(label1.Text,@"\D+");
            label2.Text = coords[0]+ "," + coords[1];
            label3.Text = coords[2] + "," + coords[3];

this displays the coordinates of the mouse? cause what i am doing is to do a calculation of the differences of the 2 point.

I want to get the cursor coordinates when the mouse is clicked in inkcanvas.here i used mouse event handler and i am trying to display cordinates in a textbox but somehow its not working.i have used the following code snippet.can someone please help

private void inkCanvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Point position = e.GetPosition(this);
            this.textBox1.Text = position.X.ToString()+","+position.Y.ToString();
        }

i have the following code to find the coordinates of all the strokes in inkcanvas.

private string GetAllPoints(InkCanvas oInkCanvas)
        {
            int iS = -1;
            int iP;
            string sP = "";
            string s = "";
            string r = "\r\n";

            System.Windows.Input.StylusPointCollection colStylusPoints;
            foreach (Stroke oS in oInkCanvas.Strokes)
            {
                iP = -1;

                // Header for each Stroke.


                if (oS.DrawingAttributes.FitToCurve == true)
                {

                    colStylusPoints = oS.GetBezierStylusPoints();
                    foreach (StylusPoint oPoint in colStylusPoints)
                    {
                        iP += 1;
                        s +=((int)oPoint.X).ToString() + "\t" +
                        ((int)oPoint.Y).ToString() + r;
                    }
                }

                else
                {

                    colStylusPoints = oS.StylusPoints;
                    foreach (StylusPoint oPoint in colStylusPoints)
                    {
                        iP += 1;
                        s += 
                        ((int)oPoint.X).ToString() + "\t" +
                        ((int)oPoint.Y).ToString() + r;
                    }
                }
            }
            return s;
        }

Actually i need to find the coordinates of the current stroke i have drawn and not the coordinates of the previous strokes on inkcanvas.
How do i make the modifications?

help me please i want to solve my problem in c# but lcan't ,i want to adjust distance between to point in image fourm by drow line and take this line to compile to mm or nn.

commented: do not try to hijack threads. Post as a new question -2

hi i want the code for to get the position of the mouse where the clicking operation is performed outside the form. thx in advance

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.