DesktopWidth()/DesktopHeight() unreliable?

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

DesktopWidth()/DesktopHeight() unreliable?

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: DesktopWidth()/DesktopHeight() unreliable?

Post by RASHAD »

Using PB 5.7 with DPI enabled both results are identical
Egypt my love
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: DesktopWidth()/DesktopHeight() unreliable?

Post 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?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: DesktopWidth()/DesktopHeight() unreliable?

Post 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
Egypt my love
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DesktopWidth()/DesktopHeight() unreliable?

Post 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
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: DesktopWidth()/DesktopHeight() unreliable?

Post 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.
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: DesktopWidth()/DesktopHeight() unreliable?

Post by BarryG »

Good to know, Little John. Thanks for the link.
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: DesktopWidth()/DesktopHeight() unreliable?

Post 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).
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: DesktopWidth()/DesktopHeight() unreliable?

Post 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.
Post Reply