-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));
}
galakt
  • 1,374
  • 13
  • 22
  • 2
    Does your both machines have same screen resolution? same dpi settings etc? – Sriram Sakthivel May 10 '15 at 14:58
  • No. But i tested it on machines with win7 and different screen resolution and it work fine. – galakt May 10 '15 at 15:04
  • 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](http://stackoverflow.com/a/23968643/2530848). – Sriram Sakthivel May 10 '15 at 15:06
  • @MickyDuncan OP said "but when i emulate click by PostMessage, click occurred in different area" So it can't be the elevation problem. – Sriram Sakthivel May 10 '15 at 15:07
  • @SriramSakthivel Posted code how i calculate LParam – galakt May 10 '15 at 15:30
  • @galakt Please post _more_ code than just a simple description of arguments posted to a Win32 function –  May 10 '15 at 15:32
  • 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. – Raymond Chen May 10 '15 at 15:39
  • @MickyDuncan Posted more code. First app, that simulate click, is WPF. Second app, where click must be occurred, is not WPF app – galakt May 10 '15 at 15:48
  • @MickyDuncan OP said "click occurred in different area". Click work, the issue is with the coordinates – galakt May 10 '15 at 16:04
  • 1
    There's no difference between `PostMessage` on Win7 and Win8. Perhaps you are subject to DPI virtualization. What you should be asking yourself is why you are faking input in this way? Why don't you use automation, or even `SendInput`? – David Heffernan May 10 '15 at 16:17
  • 3
    Please post an answer to your question now that you have the solution. – Raymond Chen May 10 '15 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](https://msdn.microsoft.com/en-us/library/ms747327.aspx). Added bonus: You don't need to calculate coordinates! – IInspectable May 11 '15 at 15:28
  • 1
    You got linked to from The Old New Thing for the bad idea of doing this via PostMessage. – Joshua May 12 '15 at 18:02

1 Answers1

2

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

galakt
  • 1,374
  • 13
  • 22