Page 1 of 1

DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 3:42 am
by BarryG
Foreword: I'm using a single monitor at 125% DPI for this test.

Have a look at this code. What I want to do is center any given window (even third-party) on the desktop. But if I use ExamineDesktops() and DesktopWidth()/DesktopHeight() to do the calculations, the window does NOT get centered. It only gets centered if I use the Windows API call of GetSystemMetrics_().

The simple, foolproof, standalone code and screenshot below explain everything. Shouldn't DesktopWidth()/DesktopHeight() match what GetSystemMetrics_() returns? Is this a bug?

As you can see with your own eyes, I simply cannot rely on DesktopWidth()/DesktopHeight() at all for 125% DPI (or higher) displays. :(

Code: Select all

; Set your monitor to 125% DPI first!

gsm_W=GetSystemMetrics_(#SM_CXSCREEN)
gsm_H=GetSystemMetrics_(#SM_CYSCREEN)
Debug gsm_W ; 1024
Debug gsm_H ; 819

ExamineDesktops()
ed_W=DesktopWidth(0)
ed_H=DesktopHeight(0)
Debug ed_W ; 1280
Debug ed_H ; 1024

w=500
h=250
OpenWindow(1,0,0,w,h,"GetSystemMetrics",#PB_Window_SystemMenu)
ResizeWindow(1,(gsm_W-w)/2,(gsm_H-h)/2,#PB_Ignore,#PB_Ignore)

OpenWindow(2,0,0,w,h,"ExamineDesktops",#PB_Window_SystemMenu)
ResizeWindow(2,(ed_W-w)/2,(ed_H-h)/2,#PB_Ignore,#PB_Ignore)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Image

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 4:05 am
by RASHAD
Using PB 5.7 with DPI enabled both results are identical

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 4:20 am
by BarryG
RASHAD wrote:Using PB 5.7 with DPI enabled both results are identical
Not here (using 5.71 beta 1) with a 125% DPI monitor. When I enable DPI in the Compiler Options, both sets of variables still have different results, and the windows are now centered opposite: that is, the ExamineDesktops window is centered, but the GetSystemMetrics window is off-center (opposite to what my screenshot shows). What the hell.

So, I guess we should just enable DPI for our apps and use DesktopWidth()/DesktopHeight() then? Why is DPI even an option?

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 4:56 am
by RASHAD
Hi Barry :P
No one can solve the DPI problems but you
For me I use Per Monitor Aware through the registry
And when I want a different scale I do it programmatic
Search the forum for a good example by Danilo

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 8:13 am
by Little John
BarryG wrote:Is this a bug?
Yes, and it has already been fixed for the next version of PB 5.71:
viewtopic.php?f=4&t=72833

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 8:33 am
by Olliv
Not only ! It is not only a bug ! Do not forget the window borders.

Code: Select all

X = (DesktopWidth(0) - WindowWidth(Window, #PB_Window_FrameCoordinate) ) / 2
Y = (DesktopHeight(0) - WindowHeight(Window, #PB_Window_FrameCoordinate) ) / 2
ResizeWindow(Window, X, Y, #PB_Ignore, #PB_Ignore)
When we set a window size in OpenWindow(), we specify the window inner size, not the window total size.

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 8:33 am
by BarryG
Good to know, Little John. Thanks for the link.

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 8:34 am
by BarryG
Olliv wrote:Do not forget the window borders.
I know, but including the borders still makes it way off-center. (They only add about 7 pixels on each window side).

Re: DesktopWidth()/DesktopHeight() unreliable?

Posted: Sun Jun 09, 2019 8:44 am
by Olliv
7 pixels on your specific theme, what it can changes. And 7 pixels for the width. For the height, it is more, including title bar (near 24 pixels).

If you can check if WindowWidth() and WindowHeight() in the both modes (inner mode and frame mode) have the right values with and without DPI aware. I am not sure they are perfect.