Size of OpenWindowedScreen() is incorrect

Just starting out? Need help? Post your questions and find answers here.
Brainois
New User
New User
Posts: 3
Joined: Fri Oct 25, 2024 6:13 pm

Size of OpenWindowedScreen() is incorrect

Post by Brainois »

Dear,

Trying to run 'Example 1' from the OpenWindowedScreen() (french) help, I noted that the size of the screen is smaller than expexted.

Image

PureBasic 6.12 LTS (x64)
Windows 11 Family

// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)
Brainois
New User
New User
Posts: 3
Joined: Fri Oct 25, 2024 6:13 pm

Re: Size of OpenWindowedScreen() is incorrect

Post by Brainois »

After some investigation, a workaround seems to be to increase the hieght of OpenWindowedScreen() by 25% compared to the height specified in OpenWindow()

Code: Select all

If InitSprite() = 0
    MessageRequester("Erreur", "Impossible d'ouvrir l'écran & l'environnement nécessaire aux sprites !", 0)
    End
  EndIf
  
  winH.l = 180
  scrH.l = Round(winH * 1.25, #PB_Round_Up)
  If OpenWindow(0, 0, 0, 400, winH, "Un écran dans une fenêtre...", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
    If OpenWindowedScreen(WindowID(0), 0, 0, 400, scrH)
      CreateSprite(0, 20, 20)
      If StartDrawing(SpriteOutput(0))
        Box(0, 0, 20, 20, RGB(255, 0, 155))
        Box(5, 5, 10, 10, RGB(155, 0, 255))
        StopDrawing()
      EndIf
    Else
      MessageRequester("Erreur", "Impossible d'ouvrir un écran dans la fenêtre!", 0)
      End
    EndIf
  EndIf
  
  direction = 2
  Repeat
    ; Il est très important de traiter tous les évènements restants dans la file d'attente à chaque tour
    ;
    Repeat
      Event = WindowEvent()
      
      Select Event 
        Case #PB_Event_Gadget
          If EventGadget() = 0
            End
          EndIf
        
        Case #PB_Event_CloseWindow
          End 
      EndSelect
    Until Event = 0
  
    FlipBuffers() 
    ClearScreen(RGB(0, 0, 0))
    DisplaySprite(0, x, x)
    x + direction
    If x > scrH - 20 : direction = -2 : EndIf
    If x < 0   : direction =  2 : EndIf
    Delay(1)
  ForEver

User avatar
JHPJHP
Addict
Addict
Posts: 2267
Joined: Sat Oct 09, 2010 3:47 am

Re: Size of OpenWindowedScreen() is incorrect

Post by JHPJHP »

Hi Brainois,

Welcome to the forum.

There is a compiler option located in the menu of the IDE: Compiler > Compiler Options... > Enable DPI aware executable.
1. If you remove this option OpenWindowedScreen will size correctly to the window, but there may be some pixelation.
2. Preferably you can adjust the size of OpenWindowedScreen to match your OS - Display Settings: 125%, 150%, 175%, etc. (scale)
• It's not usually a good idea to hard code this value especially if you plan on distributing your program.

Personally, I use the following (dynamic) Macro's when adjusting for scale:
ScaleFactorM can be used to increase screen size, alternately ScaleFactorD to decrease window size.
• There are numerous other solutions, this is just my preferred method.

Code: Select all

Macro ScaleFactor
  DesktopResolutionX()
EndMacro

Macro ScaleFactorM(value)
  (value) * ScaleFactor
EndMacro

Macro ScaleFactorD(value)
  (value) / ScaleFactor
EndMacro
Your example code would then look something like the following:

Code: Select all

Macro ScaleFactor
  DesktopResolutionX()
EndMacro

Macro ScaleFactorM(value)
  (value) * ScaleFactor
EndMacro

Macro ScaleFactorD(value)
  (value) / ScaleFactor
EndMacro

If InitSprite() = 0
  MessageRequester("Erreur", "Impossible d'ouvrir l'écran & l'environnement nécessaire aux sprites !", 0)
  End
EndIf

winW.l = 400 : scrW.l = ScaleFactorM(winW)
winH.l = 180 : scrH.l = ScaleFactorM(winH)

If OpenWindow(0, 0, 0, winW, winH, "Un écran dans une fenêtre...", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  If OpenWindowedScreen(WindowID(0), 0, 0, scrW, scrH)
    CreateSprite(0, 20, 20)
    If StartDrawing(SpriteOutput(0))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
    EndIf
  Else
    MessageRequester("Erreur", "Impossible d'ouvrir un écran dans la fenêtre!", 0)
    End
  EndIf
EndIf

direction = 2
Repeat
  ; Il est très important de traiter tous les évènements restants dans la file d'attente à chaque tour
  ;
  Repeat
    Event = WindowEvent()
    
    Select Event 
      Case #PB_Event_Gadget
        If EventGadget() = 0
          End
        EndIf
      
      Case #PB_Event_CloseWindow
        End 
    EndSelect
  Until Event = 0

  FlipBuffers() 
  ClearScreen(RGB(0, 0, 0))
  DisplaySprite(0, x, x)
  x + direction
  If x > scrH - 20 : direction = -2 : EndIf
  If x < 0   : direction =  2 : EndIf
  Delay(1)
ForEver

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Post Reply