Mouse movement in C # (coordinates)

I am trying to make Teamviewer as part of the software for fun, which allows one person to view another personโ€™s screen and click and all that. Anyway, I have the most sockets, but I donโ€™t know how to make the mouse clicks work correctly. Here is the code I found online to move the mouse programmatically:

public static class VirtualMouse { // import the necessary API function so .NET can // marshall parameters appropriately [DllImport("user32.dll")] static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); // constants for the mouse_input() API function private const int MOUSEEVENTF_MOVE = 0x0001; private const int MOUSEEVENTF_LEFTDOWN = 0x0002; private const int MOUSEEVENTF_LEFTUP = 0x0004; private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; private const int MOUSEEVENTF_RIGHTUP = 0x0010; private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; private const int MOUSEEVENTF_MIDDLEUP = 0x0040; private const int MOUSEEVENTF_ABSOLUTE = 0x8000; // simulates movement of the mouse. parameters specify changes // in relative position. positive values indicate movement // right or down public static void Move(int xDelta, int yDelta) { mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0); } // simulates movement of the mouse. parameters specify an // absolute location, with the top left corner being the // origin public static void MoveTo(int x, int y) { mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0); } // simulates a click-and-release action of the left mouse // button at its current position public static void LeftClick() { mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); } } 

Now I want to move the mouse using the MoveTo method, but crazy high numbers are required for any movements. Anyway, can I match the coordinates for moving here to the position on the screen in pixels? Sorry if this seems like an obvious question, but I have been searching for searches for almost an hour, and I cannot find any discussion of which units are used for the x and y positions of the mouse, so I cannot configure any type of formula to match clicks on one panel to click on the user's screen.

+4
source share
1 answer

From Microsoft Documentation :

If MOUSEEVENTF_ABSOLUTE is specified, dx and dy contain normalized absolute coordinates between 0 and 65535. An event procedure displays these coordinates on the display surface. coordinate (0,0) is displayed in the upper left corner of the display surface, (65535,65535) is displayed in the lower right corner.

You can use this to convert the input in pixels to the desired value, for example:

 var inputXinPixels = 200; var inputYinPixels = 200; var screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; var outputX = inputXinPixels * 65535 / screenBounds.Width; var outputY = inputYinPixels * 65535 / screenBounds.Height; MoveTo(outputX, outputY); 

Please keep in mind that this may not be true for multiple monitors. Also note that the document says:

This feature has been replaced. Use SendInput instead .

Addendum : J3soon specified , the above formula may not be the best. Based on a study done for AutoHokey , internal code works better:

 var outputX = (inputXinPixels * 65536 / screenBounds.Width) + 1; var outputY = (inputYinPixels * 65536 / screenBounds.Height) + 1; 

See AutoHotkey Source Code for reference.


If I were in your position, I would use Cursor.Position. The following code works as expected:

 System.Windows.Forms.Cursor.Position = new System.Drawing.Point(200, 200); 

Yes, it places the mouse pointer at the coordinates (200, 200) of the screen pixels [Tested on LinqPad].

Addition . I looked that uses System.Windows.Forms.Cursor.Position for internal use. In Mono on Windows at least. This is a call to SetCursorPos . No strange coordinate conversion is required.

+5
source

Source: https://habr.com/ru/post/1439517/


All Articles