The question is simple, I have a code that's using the PostMessage function like so :
public enum WMessages : int
{
WM_MOUSEMOVE= 0x0200,
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14,
}
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
private int MAKELPARAM(int p, int p_2)
{
return (p_2 * 0x10000 + p);
}
public void SendClick(WMessages type, Point pos, IntPtr hwnd)
{
switch (type)
{
case WMessages.WM_LBUTTONDOWN:
PostMessage(hwnd,
(int)0x201, 1,
MAKELPARAM(pos.X, pos.Y));//(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
case WMessages.WM_LBUTTONUP:
PostMessage(hwnd,
(int)WMessages.WM_LBUTTONUP, 0, // <--(2) but you are telling to do WM_LBUTTONDOWN
MAKELPARAM(pos.X, pos.Y));//(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
default:
return;
}
}
The only problem is that it clicks in a wrong place. For example, if i input the point 50,50 it'll click in 70,70 for example, offsetted to the window handle.
I wonder why it happens? Hope to get an answer.
Thanks in advance.
Example :
IntPtr atat = (IntPtr)0x00010870;
SendRClick(WMessages.WM_RBUTTONDOWN, new Point(50, 50), atat);
SendRClick(WMessages.WM_RBUTTONUP, new Point(50, 50), atat);
Edit :
I've found something interesting, apparently it happens only in 1 of my screens (I have 2), in the other one its not happening.
The screen that it happens in's resolution is 1920/1080, the screen that does it correctly's resolution is 1280/1024 (my main screen, does it correctly).
Edit 2 :
Also another thing that I've forgot to mention, the ratio between the resulted point to the inserted point is 1.5 in X and also in Y.
For example, if I've entered X=100,Y=100, it'll be resulted in 150 X and 150 Y offset from the window handle.
Edit 3 :
An alternative would be to devide the x and y by 1.5 and then it'll be multiplied again by default... but its not really an answer
Edit 4 :
If it's a duplicate then give me a link to an original thread then.
Edit 5 :
I've solved it. Adding a manifest didn't help, but then i got to know this file:
https://github.com/Microsoft/WPF-Samples/blob/master/PerMonitorDPI/Developer%20Guide%20-%20Per%20Monitor%20DPI%20-%20WPF%20Preview.docx
By adding to the manifest file this line:
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings"> PerMonitor</dpiAwareness>
in the <windowsSettings>
area I've solved the problem.