Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi...
1) how can i set the x point and the y point of the mouse at the desctop? (not in the form...!)
2) how can i click at the point that i have set?

your help will be very useful for me!!! thanks in advance!!! :)
Posted

1 solution

This one should do the job:

C#
public static void ClickSomePoint() {
    // Set the cursor position
    System.Windows.Forms.Cursor.Position = new Point(20, 35);

    DoClickMouse(0x2); // Left mouse button down
    DoClickMouse(0x4); // Left mouse button up
}

static void DoClickMouse(int mouseButton) {
    var input = new INPUT() {
        dwType = 0, // Mouse input
        mi = new MOUSEINPUT() { dwFlags = mouseButton }
    };

    if (SendInput(1, input, Marshal.SizeOf(input)) == 0) {
        throw new Exception();
    }
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT {
    int dx;
    int dy;
    int mouseData;
    public int dwFlags;
    int time;
    IntPtr dwExtraInfo;
}
struct INPUT {
    public uint dwType;
    public MOUSEINPUT mi;
}
[DllImport("user32.dll", SetLastError=true)]
static extern uint SendInput(uint cInputs, INPUT input, int size);

Source: stackoverflow.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900