Search code examples
c#winformspositionwindowcursor

How do you set a cursor's position in a different window/application?


I'm trying to set a cursor position to a specific set of coords in a different window, but the mouse never refreshes on that window unless I move the physical mouse I have.

This is for a program that moves the users cursor for them to a specific location IN A DIFFERENT WINDOW, and clicks after a logical expression returns true.

int x = 0;
int y = 0;
Cursor.Position = new Point(x, y);

I want the cursor to actually move inside of a window and not just in form1. The code will move the cursor to that position but it will not move on the window I am currently on.


Solution

  • Sending RAW Input data to use mouse. Some applications read raw mouse strokes while others read virtual mouse strokes.

    int to_x = 500;
    int to_y = 300;
    int screenWidth = InternalGetSystemMetrics(0);
    screenHeight = InternalGetSystemMetrics(1);
    // Mickey X coordinate
    int mic_x = (int)System.Math.Round(to_x * 65536.0 / screenWidth);
    // Mickey Y coordinate
    int mic_y = (int)System.Math.Round(to_y * 65536.0 / screenHeight);
    // 0x0001 | 0x8000: Move + Absolute position
    Mouse_Event(0x0001 | 0x8000, mic_x, mic_y, 0, 0);
    // 0x0002: Left button down
    Mouse_Event(0x0002, mic_x, mic_y, 0, 0);
    // 0x0004: Left button up
    Mouse_Event(0x0004, mic_x, mic_y, 0, 0);```