-2

I developed application that use PostMessage to simulate mouse click in another app. I have screenshots of second app, i have coordinates for mouse click and i am simulate mouse click for second app. That all work fine on windows 7, but when i run it on windows 8 i am so confused: screenshots of second app on win8 and win7 are the same, but when i emulate click by PostMessage, click occurred in different area. And i have two questions: 1) Why PostMessage work different on win8? 2) How it can be fixed?

EDIT: For PostMessage lParam = ((HiWord << 16) | (LoWord & 0xffff)), where LoWord is Left and HiWord is Top coordinates

EDIT2: More code

Step1

var hW = Win32.User32.FindWindow(null, "AppName");

Step2

MouseEngine.Click(hW, 100, 100);

where

public void Click(IntPtr hWnd, int left, int top)
    {
        // send mouse down
        User32.PostMessage(
            hWnd,
            User32.Message.WM_LBUTTONDOWN,
            0,
            User32.MakeLParam(left, top));
        // send mouse up
        User32.PostMessage(
            hWnd,
            User32.Message.WM_LBUTTONUP,
            0,
            User32.MakeLParam(left, top));
    }

where

public static int MakeLParam(int LoWord, int HiWord)
{
    return ((HiWord << 16) | (LoWord & 0xffff));
}
13
  • 2
    Does your both machines have same screen resolution? same dpi settings etc? May 10, 2015 at 14:58
  • 2
    Post your code which you claims to be working. For different screen resolution, you can translate it via simple math as I shown here. May 10, 2015 at 15:06
  • 4
    By your own admission, the PostMessage is working - the message gets posted. The issue is with the coordinates. Make sure your program is marked high DPI aware. Also, you should switch to UI automation. You can't use PostMessage to simulate input. May 10, 2015 at 15:39
  • 3
    Please post an answer to your question now that you have the solution. May 10, 2015 at 17:55
  • 2
    "I developed application that use PostMessage to simulate mouse click in another app." There's your error. Don't. Ever. It's wrong, for more than one reason. Use the right tool for the job: UI Automation. Added bonus: You don't need to calculate coordinates! May 11, 2015 at 15:28

1 Answer 1

2

Problem occurred because on current machine with Win8.1 DPI default value 120(125%), while on all other machines is 96(100%)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.