Page 1 of 1
[Solved] Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 2:59 am
by BarryG
So I've just started to foray into making my app DPI-aware, and my first issue is below.

My desktop's DPI is set to 150% in Windows.
The code below is meant to center the window where the mouse is, when the app is compiled with DPI awareness set.
But depending on where my mouse is when I run it, the window is either near the mouse (if the mouse is near the top left of the desktop), or not even shown (when the mouse is near the bottom right of the desktop). It's like DesktopMouseX() and DesktopMouseY() isn't getting where the mouse actually is?
The code works perfectly when my desktop's DPI is set to 100% (but not 150%), so how do I fix this? Thanks.
Code: Select all
; Enable DPI aware in Compiler Options.
ww=600
wh=100
OpenWindow(0,0,0,ww,wh,"Window should be centered on the mouse",#PB_Window_SystemMenu|#PB_Window_Invisible)
mx=DesktopMouseX()
my=DesktopMouseY()
ResizeWindow(0,mx-(ww/2),my-(wh/2),ww,wh)
HideWindow(0,0)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Re: Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 4:44 am
by wombats
Code: Select all
ww=600
wh=100
OpenWindow(0,0,0,ww,wh,"Window should be centered on the mouse",#PB_Window_SystemMenu|#PB_Window_Invisible)
mx=DesktopUnscaledX(DesktopMouseX())
my=DesktopUnscaledY(DesktopMouseY())
ResizeWindow(0,mx-(ww/2),my-(wh/2),ww,wh)
HideWindow(0,0)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Re: Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 5:00 am
by BarryG
Oh, okay. That works! Thanks.
But I didn't understand what those commands did (never used DPI-awareness before). So basically I need to use these commands everywhere when getting the mouse position? I (naively?) assumed that enabling DPI-awareness would take care of it all.
Re: Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 5:43 am
by DeanH
I use something like below to work out where to position a window not quite in the middle. The taskbar height needs to be taken into account, especially as it is different in Windows 11 to 10, 7, and others.
Code: Select all
winwidth=990 ;fits old 1024 x 768 square monitors
winheight=690 ;fills height on a 1366 x 768 monitor set to 125%.
ExamineDesktops()
dw=DesktopWidth(0) ;1920 or 1366 for most monitors and low-cost laptops
dw=DesktopUnscaledX(dw) ;1536 at 125%, 1280 at 150% scaled from 1920
dh=DesktopHeight(0) ;1080 or 768
dh=DesktopUnscaledY(dh) ;864 at 125%, 720 at 150%. 614 at 125% if dh is 768
dh-48 ;48 pixel taskbar height; is 46 on Win 11, 40 on Win 10 and 7.
x=(dw-winwidth)/2-2
y=(dh-winheight)/2-16 ;I like to set the window slightly above centre
Re: Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 5:53 am
by wombats
BarryG wrote: Fri Feb 14, 2025 5:00 am
Oh, okay. That works! Thanks.
But I didn't understand what those commands did (never used DPI-awareness before). So basically I need to use these commands everywhere when getting the mouse position? I (naively?) assumed that enabling DPI-awareness would take care of it all.
You're welcome!
It is a bit confusing, yeah. I'm still learning about it, myself. I think most gadget things are taken care of, but not drawing, so in CanvasGadget, etc., we need to scale the drawing ourselves. I find that I have to scale the drawing, but for the logic, I find just using DesktopUnscaledX/Y for the mouse value makes it a bit easier.
Re: Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 5:58 am
by BarryG
And I just discovered that I need to use DesktopScaledY() with WindowY() as well, because WindowY() by itself doesn't give the window's true Y position on the desktop when the desktop is 150% DPI.
[Edit] And to fix my app, I also have to do this now:
Code: Select all
screenw=DesktopUnscaledX(GetSystemMetrics_(#SM_CXSCREEN))
screenh=DesktopUnscaledY(GetSystemMetrics_(#SM_CYSCREEN))
I really thought enabling DPI awareness in Compiler Options would do all of this automatically.

Re: [Solved] Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 7:55 am
by AZJIO
I get the scaling coefficient at the beginning of the code when starting the program, and then I multiply all the coordinates by it. At 100% it is 1 and nothing changes, at 150% it is 1.5 and I multiply everything by 1.5, more precisely by the variable in which the coefficient is located.
You need to add WM_DPICHANGED to change it during operation.
Re: [Solved] Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 8:37 am
by BarryG
AZJIO wrote: Fri Feb 14, 2025 7:55 amI get the scaling coefficient at the beginning of the code when starting the program
I've tried to get the Windows scaling factor without success for years. Care to share how?

Re: [Solved] Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 9:04 am
by AZJIO
Re: [Solved] Are DesktopMouseX() and DesktopMouseY() DPI-aware?
Posted: Fri Feb 14, 2025 12:40 pm
by BarryG
Thank you! It didn't work at first but then I realized DPI-awareness had to be turned on in Compiler Settings.