1

I need to check a pixel color and I let the user tag the position on the screen. I've tried both GetCursorPos() and GetPhysicalCursorPosition() but neither returns the correct value.

(I noticed that getting my screen resolution via GetSystemMetrics returns a wrong resolution as well. It reports to be 2560x1440, but it really is 3840x2160.)

So, when multiplying the result of GetCursorPos() by 1.5, I do get the correct cursor position for my monitor. However, the positions on my other two 1080p monitors are still wrong. They were not correct before.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
L. Etienne
  • 77
  • 7
  • 1
    Look here: [How to get the cursor position](https://stackoverflow.com/questions/2396576/how-to-get-the-cursor-position). `GetCursorPos()` and `ScreenToClient()` (for mapping to Window coordinates) should be everything you need. Q: What version of Windows, how many monitors are configured, and which show the wrong screen resolution? See also: [GetSystemMetrics() returns wrong value for SM_CXSCREEN](https://stackoverflow.com/a/2630531/3135317) – FoggyDay Mar 29 '20 at 01:36
  • Windows 10, three monitors. Already tried GetCursorPos and ScreenToClient(NULL, point) //Null because i want it relative to the screen) I can't use a window because the name of the window changes constantly. Also, the window is not supposed to move anyways – L. Etienne Mar 29 '20 at 01:41
  • 1
    You might have different scaling settings (DPI) on your different resolution monitors which can change the returned values. Is your app DPI aware? – 1201ProgramAlarm Mar 29 '20 at 01:42
  • @1201ProgramAlarm - the link I cited explicitly mentions the importance of "DPI aware". Thank you for emphasizing this. Here's another link: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiaware?redirectedfrom=MSDN – FoggyDay Mar 29 '20 at 01:44
  • Wow! SetProcessDPIAware() was an easy partial fix! The position is now correct for my main monitor without any corrections. However, my two side monitors are still off. Could it be that they are 1080p hardware but scaled to 2160p via the geforce control panel? (DSR, windows actually sees them as being native 4k) – L. Etienne Mar 29 '20 at 01:48
  • After clicking on a sidelink on the docs page for `SetProcessDPIAware()` i found the manual function. `SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);` This does the trick! It now works! Thanks for pointing me in the right direction! – L. Etienne Mar 29 '20 at 01:53
  • @L.Etienne - since this is somewhat a corner-case programming question, it may be beneficial to write up a formal answer explaining the multi-monitor solution with `SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);`. A good answer would be equally beneficial to your rep as well. – David C. Rankin Mar 29 '20 at 02:11
  • I don't know what there is to elaborate.. I just added this line `SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);` in the intialization of my program and it further calls of `GetCursorPos()` return the cursor position in actual pixel space, not in weird-scaled pixel space like it is at default. – L. Etienne Mar 30 '20 at 15:18

0 Answers0