Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PostMessage to simulate mouse click
#1
Hello,
I am attempting to simulate mouse click using PostMessage approach so that the cursor does not move.
I have been successful doing so from a local level.
However, when I attempt to create a function from global Class file global.cs, I get the following error:

System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)

the error is pointing to line: 
     ScreenToClient(Parent_hwnd,ref ControlPoint);

I am calling the function like the following:
   var w = wnd.find(1, "Wind");
   var e = w.Elm["STATICTEXT", "Text"].Find(1);
   LeftClick(w.Handle,e.Rect.CenterX,e.Rect.CenterY);
 
Code:
Copy      Help
public static uint WM_LBUTTONDOWN = 0x0201;
public static uint WM_LBUTTONUP = 0x0202;

public static void LeftClick( nint Parent_hwnd, int Control_CenterX, int Control_CenterY) {
            
    POINT ControlPoint = new POINT(Control_CenterX, Control_CenterY);
    Console.WriteLine($"X: {ControlPoint.x}, Y: {ControlPoint.y}");
    ScreenToClient(Parent_hwnd,ref ControlPoint);
    Console.WriteLine($"X: {ControlPoint.x}, Y: {ControlPoint.y}");
    PostMessage(Parent_hwnd, WM_LBUTTONDOWN, 1,MakeLParam(ControlPoint.x, ControlPoint.y));
    PostMessage(Parent_hwnd, WM_LBUTTONUP, 0,MakeLParam(ControlPoint.x, ControlPoint.y));
}


    [DllImport("user32.dll")]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    unsafe static int MakeLParam(int x, int y) => (y << 16) | (x & 0xFFFF);
        
    static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
#2
Bad PostMessage declaration. See menu Code > Windows API.

See also: wnd.PostMath2.MakeLparam, elm.PostClick (use the UI element tool to create code)
#3
Thanks Gintaras,
For some reason the method PostClick would only work if the mouse is hovered over the intended window for the click. The mouse does not move during the procedure, but must anywhere on top of the intended window.
Otherwise, if the mouse is not over the window, then nothing happens.
Any ideas for this behavior?
#4
The app gets the current mouse position not from the message. PostMessage will not work with that window.


Forum Jump:


Users browsing this thread: 1 Guest(s)