-1

I have been trying to get X,Y coordinates in double (decimal etc) format in C# Form for weeks. I've tried every way, read every almost every question, videos etc but I've could not find any solution for my working (sorry my english).

I can get the mouse coordinates but I want it in double format like X: 12,212 Y: 102,233.

Here is my codes which I tried. Thank you all.

////////////////////////
////////WAY 1 (NOT DOUBLE)///////
//////////////////////
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;

label1.Text = coordinates.X.ToString();
label2.Text = coordinates.Y.ToString();

////////////////////////
////////WAY 2 (NOT DOUBLE)///////
//////////////////////
Point po = e.Location;

label1.Text = po.X.ToString();
label2.Text = po.Y.ToString();

////////////////////////
////////WAY 3 (NOT DOUBLE)///////
//////////////////////
double xCoordinate = e.X;
double yCoordinate = e.Y;

label1.Text = xCoordinate.ToString("0.0#");
label2.Text = yCoordinate.ToString("0.0#");
Sinatr
  • 20,892
  • 15
  • 90
  • 319
Furkan
  • 15
  • 5
  • Winforms or WPF? (C# forms is a bit confusing). – Atlasmaybe Sep 07 '17 at 12:17
  • What format are you getting? `int`? `float`? – juanferrer Sep 07 '17 at 12:20
  • @JuanFerrer : Assuming that's Winforms, it's always an `int` :https://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx – Atlasmaybe Sep 07 '17 at 12:21
  • Hi Juan, I am getting only int format. – Furkan Sep 07 '17 at 12:21
  • Hi @Atlasmaybe I am using WinForm. – Furkan Sep 07 '17 at 12:22
  • @Furkan Well, it depends. You can't assume he's using the `Point` from WinForms. Maybe it's a user defined class. Or maybe it's coming from an external library. You have to ask to know =] – juanferrer Sep 07 '17 at 12:22
  • 3
    "X: 12,212" ... 12,1212 what? Pixels? cm? mm? inches? – Fildor Sep 07 '17 at 12:25
  • [MouseEventArgs's X](https://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.x(v=vs.110).aspx) comes in Pixels of type int : "The x-coordinate of the mouse, in pixels." - There are not really "half pixels". But if you want the Position in mm for example, you have to calculate the value using the screen resolution. – Fildor Sep 07 '17 at 12:27
  • normally the coordinate system is in `int` because the unit is pixels. That makes me wonder how to move only half a pixel – Mong Zhu Sep 07 '17 at 12:28
  • @Furkan : using Winforms and "default" providers, you can't get the coordinates in double format, because the point you get refers to a specific pixel! For a screen, there's nothing smaller than a pixel and you can't get part of it. – Atlasmaybe Sep 07 '17 at 12:28
  • @Fildor I think in pixel. I don't know how to get in cm, mm or etc... – Furkan Sep 07 '17 at 12:29
  • @Atlasmaybe I understand. I just want to get a result like Autocad drawing screen with coordinates. I think for a newbie it is hard :) – Furkan Sep 07 '17 at 12:31
  • 2
    @Furkan; Autocad defines its own spacial referential. You need to define also you own referential if you want to get coordinates in something else than pixels. But let me give you an advise, this is advanced programming, so don't push too hard and keep the pixels. ;) – Atlasmaybe Sep 07 '17 at 12:35
  • @Atlasmaybe thank you and i will keep it in my mind :) – Furkan Sep 07 '17 at 12:39

1 Answers1

2

Cursor position is given in pixels. You could try to force that into a double, but it would make no sense. There's no such thing as half a pixel.

If your idea was to get the cursor position in double, you're out of luck. But if you were just using the cursor position to debug (and there is another control that you're trying to get coordinates from), maybe we can still help.

A more detailed question will bring a more detailed answer.

Fildor
  • 14,510
  • 4
  • 35
  • 67
juanferrer
  • 1,192
  • 1
  • 16
  • 29
  • I've watched a video on youtube and the guy were using the chart. He defined a code that using math.round and pixeltoValue. The video's name is : C# .NET Cross Hair Cursor - Video Tutorial. – Furkan Sep 07 '17 at 12:37
  • Oh, I see. That thing on the video are not mouse coordinates, but chart coordinates. That function `pixelToValue()` is mapping a point in the screen (in pixels) to two values from the chart. For example: `(200, 150)` maps to `(5.268, 11.015)`. – juanferrer Sep 07 '17 at 13:11
  • Yes... I think may be a coordinate system can be defined by chart but I have no idea how can be done :) – Furkan Sep 07 '17 at 13:16
  • [This question](https://stackoverflow.com/questions/9647666) is about how to display the values of a chart in a point. Hope it helps =] – juanferrer Sep 07 '17 at 13:31
  • Thank you Juan :) – Furkan Sep 07 '17 at 15:13