How I make DPI-aware apps

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How I make DPI-aware apps

Post by Fred »

I will take a look
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: How I make DPI-aware apps

Post by chi »

Fred wrote:I will take a look
Great, thanks a lot!
Et cetera is my worst enemy
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How I make DPI-aware apps

Post by Little John »

Whether or not a text is too big for a given gadget because of the DPI setting also depends on the Windows version. Namely Windows versions < 8.1 do different font scaling compared to Windows versions >= 8.1.
For details see this code.
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: How I make DPI-aware apps

Post by chi »

Looks pretty much the same to me...
ImageImage
Et cetera is my worst enemy
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How I make DPI-aware apps

Post by Little John »

chi wrote:Looks pretty much the same to me...
AFAIR we alread had that discussion ...
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: How I make DPI-aware apps

Post by chi »

yup
Et cetera is my worst enemy
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: How I make DPI-aware apps

Post by Fangbeast »

Windows 10 makes this even worse. I am using a 34 inch ultrawide and on 2560x1080 at 125% for my old eyes, the entire form gets enlarged off the screen.

I thought DPI awareness was supposed to somehow adjust everything to fit inside the current screen resolution even if the dpi was above 100%?

This has been the same with or without dpi settings on pb 5.7 and on older versions of pb.

Just weird. And each release of windows 10 keeps fiddling with the api used to produce the results.
Amateur Radio, D-STAR/VK3HAF
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How I make DPI-aware apps

Post by Dude »

Fangbeast wrote:I thought DPI awareness was supposed to somehow adjust everything to fit inside the current screen resolution even if the dpi was above 100%?
That's what I thought, but it doesn't. I use Win 10 now (and Win 7 and Win XP for testing) and my tip in the first post of this topic works perfectly on all three systems at three different Windows DPI settings (100%, 125%, and 150%). That's good enough for me.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How I make DPI-aware apps

Post by Little John »

Fangbeast wrote:Windows 10 makes this even worse.
Here is a good overview about different DPI scaling methods used by different Windows versions.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How I make DPI-aware apps

Post by Dude »

Excellent article, LJ. Thanks!
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: How I make DPI-aware apps

Post by Rinzwind »

So what exactly does the new dpi aware feature do and doesnt do in pb 5.7? And if not fuly implemented yet, can we expect a fix soon? Or does it work “as designed”? A best practice tutorial would be welcome.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: How I make DPI-aware apps

Post by mestnyi »

Ulix wrote:Translation by google!

Hello everyone !

Very interesting as a code, if I understand correctly if it is to obtain the same dimension (windows and gadgets)
whatever the resolution of the screen? or / and the setting of OS!

What is the equivalent of the instruction:

Code: Select all

Global dpi.d = GetDeviceCaps_ (GetDC_ (0), # LOGPIXELSX) / 100; The magic!
for other OS (Linux and Mac)?
The idea: Make it a multi-platform feature! :lol:

Can we use the dpi to size the fonts?

I will follow this post with interest

Ulix :wink:

Code: Select all

Global dpi.d

Define pic=CreateImage(#PB_Any,1,1)
StartVectorDrawing(ImageVectorOutput(pic))

dpi = VectorResolutionX()/100

StopDrawing()
FreeImage(pic)

OpenWindow(0,200,200,180*dpi,100*dpi,"test",#PB_Window_SystemMenu)

x=10*dpi
w=155*dpi
h=20*dpi

TextGadget(1,x,10*dpi,w,h,"This is text that fills the gadget",#PB_Text_Border)

StringGadget(2,x,40*dpi,w,h,"Another small test of fitted text")

ButtonGadget(3,x,70*dpi,w,h,"And last example of exact text")

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: How I make DPI-aware apps

Post by chi »

You have to divide by the OS' default PPI... [Since the 1980s, the Microsoft Windows operating system has set the default display "DPI" to 96 PPI, while Apple/Macintosh computers have used a default of 72 PPI. Dots per inch - Wikipedia]

Code: Select all

Global dpi.d

Define pic=CreateImage(#PB_Any,1,1)
StartVectorDrawing(ImageVectorOutput(pic))

CompilerSelect #PB_Compiler_OS    
  CompilerCase #PB_OS_Windows
    ppi = 96    
  CompilerCase #PB_OS_MacOS
    ppi = 72 ;not tested (might have changed)  
  CompilerCase #PB_OS_Linux
    ppi = 96 ;not tested (might have changed)  
CompilerEndSelect

dpi = VectorResolutionX() / ppi

StopDrawing()
FreeImage(pic)

OpenWindow(0,200,200,180*dpi,100*dpi,"test",#PB_Window_SystemMenu)

x=10*dpi
w=155*dpi
h=20*dpi

TextGadget(1,x,10*dpi,w,h,"This is text that fills the gadget",#PB_Text_Border)

StringGadget(2,x,40*dpi,w,h,"Another small test of fitted text")

ButtonGadget(3,x,70*dpi,w,h,"And last example of exact text")

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Et cetera is my worst enemy
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How I make DPI-aware apps

Post by Little John »

Well ... what is the advantage of that code?

I changed it a bit, so that the actual result will be shown (tested on Windows 10).

Code: Select all

Define ppi.i, dpi.d

CreateImage(0, 1, 1)
StartVectorDrawing(ImageVectorOutput(0))

CompilerSelect #PB_Compiler_OS   
   CompilerCase #PB_OS_Windows
      ppi = 96   
   CompilerCase #PB_OS_MacOS
      ppi = 72   ; not tested (might have changed) 
   CompilerCase #PB_OS_Linux
      ppi = 96   ; not tested (might have changed) 
CompilerEndSelect

dpi = VectorResolutionX() / ppi
StopDrawing()
FreeImage(0)

Debug dpi

CompilerIf #PB_Compiler_Version >= 570
   Debug DesktopResolutionX()
CompilerEndIf
With PB 5.70, the value of dpi is always the same as the value of DesktopResolutionX():
  • 1.25 here if DPI aware is checked in the compiler options,
  • 1.0 if DPI aware is unchecked in the compiler options.
And with PB versions prior to 5.70, the value of dpi will always be 1.0, of course (except when there is additional code or a manifest that makes the program DPI aware).
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: How I make DPI-aware apps

Post by chi »

Little John wrote:Well ... what is the advantage of that code?
PB's DPI-Awareness is only available on Windows right now... So this is a multi-platform approach ;)
Last edited by chi on Fri Jan 25, 2019 6:49 pm, edited 1 time in total.
Et cetera is my worst enemy
Post Reply