Fullscreen mode hotkey for Windows GUI applications

Share your advanced PureBasic knowledge/code with the community.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Fullscreen mode hotkey for Windows GUI applications

Post by firace »

Minimal version - Use this as a starting point :)

Code: Select all

Procedure toggleFullScreen()      ;  toggle FullScreen mode using F11
  static w, h, x, y, fs
  ExamineDesktops()
  
  If fs=0   ;; not in full screen -> switch to full screen
    w = WindowWidth(0 , #PB_Window_FrameCoordinate)
    h = WindowHeight(0, #PB_Window_FrameCoordinate)
    x = windowX(0)
    y = windowY(0)
    ResizeWindow(0,0,0,DesktopWidth(0),DesktopHeight(0))
    SetWindowLongPtr_(WindowID(0),#GWL_STYLE,GetWindowLongPtr_(WindowID(0),#GWL_STYLE)&~#WS_CAPTION&~#WS_SIZEBOX)
    fs = 1
  Else  ;; and back to normal mode
    ResizeWindow(0,x,y,w,h)
    SetWindowLongPtr_(WindowID(0),#GWL_STYLE,GetWindowLongPtr_(WindowID(0),#GWL_STYLE)|#WS_CAPTION|#WS_SIZEBOX)
    fs = 0
  EndIf 
EndProcedure

OpenWindow(0,220,220,500,400,"*", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget)
TextGadget(1, 50, 50, 277, 33, "Press F11 for fullscreen mode")

AddKeyboardShortcut(0, #PB_Shortcut_F11, 14) :  BindEvent(#PB_Event_Menu, @toggleFullScreen(), 0, 14)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Fullscreen mode hotkey for Windows GUI applications

Post by Lord »

Thanks for this nice code snipped.
But there seems to be one little problem:
The window 'travels' each time it returns to normal size by 5 pixel to the right.
I had to modify your code like this:

Code: Select all

ResizeWindow(0,x-5,y,w,h)
Image
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Fullscreen mode hotkey for Windows GUI applications

Post by BarryG »

firace wrote: Fri Oct 06, 2023 4:21 pmUse this as a starting point
Sure thing. So here's a shorter version (less code) that also doesn't need the Desktop lib:

Code: Select all

Procedure toggleFullScreen()
  hwnd=WindowID(0)
  Static fs
  fs=1-fs
  If fs=0
    SetWindowLongPtr_(hWnd,#GWL_STYLE,GetWindowLongPtr_(hWnd,#GWL_STYLE)|#WS_CAPTION|#WS_SIZEBOX)
    ShowWindow_(hWnd,#SW_RESTORE)
  Else
    SetWindowLongPtr_(hWnd,#GWL_STYLE,GetWindowLongPtr_(hWnd,#GWL_STYLE)&~#WS_CAPTION&~#WS_SIZEBOX)
    ShowWindow_(hWnd,#SW_MAXIMIZE)
  EndIf
EndProcedure

OpenWindow(0,220,220,500,400,"*", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget)
TextGadget(1, 50, 50, 277, 33, "Press F11 for fullscreen mode")

AddKeyboardShortcut(0, #PB_Shortcut_F11, 14) : BindEvent(#PB_Event_Menu, @toggleFullScreen(), 0, 14)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Fullscreen mode hotkey for Windows GUI applications

Post by RASHAD »

Code: Select all

Procedure toggleFullScreen()      ;  toggle FullScreen mode using F11
  If GetWindowState(0) & #PB_Window_Maximize
    SetWindowLongPtr_(WindowID(0),#GWL_STYLE,GetWindowLongPtr_(WindowID(0),#GWL_STYLE)|#WS_SIZEBOX)
    SetWindowState(0,#PB_Window_Normal)
  Else
    SetWindowLongPtr_(WindowID(0),#GWL_STYLE,GetWindowLongPtr_(WindowID(0),#GWL_STYLE) &~#WS_SIZEBOX)
    SetWindowState(0,#PB_Window_Maximize)
  EndIf 
EndProcedure

OpenWindow(0,220,220,500,400,"*", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget|#PB_Window_SizeGadget)
TextGadget(1, 50, 50, 277, 33, "Press F11 for fullscreen mode")

AddKeyboardShortcut(0, #PB_Shortcut_F11, 14) :  BindEvent(#PB_Event_Menu, @toggleFullScreen(), 0, 14)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Egypt my love
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Fullscreen mode hotkey for Windows GUI applications

Post by BarryG »

Nice further reduction, Rashad. Good job! (But you can still see the window caption in full-screen mode, which OP doesn't want).
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Fullscreen mode hotkey for Windows GUI applications

Post by firace »

Thanks all for the feedback & improvements

This should be good (and window number is no longer hardcoded in the procedure)

Code: Select all

Procedure toggleFullScreen()
  hwnd=WindowID(EventWindow())
  
  If GetWindowState(EventWindow()) & #PB_Window_Maximize  
    SetWindowLongPtr_(hWnd,#GWL_STYLE,GetWindowLongPtr_(hWnd,#GWL_STYLE)|#WS_CAPTION)    ;; switch to normal mode
    ShowWindow_(hWnd,#SW_RESTORE)
  Else
    SetWindowLongPtr_(hWnd,#GWL_STYLE,GetWindowLongPtr_(hWnd,#GWL_STYLE)&~#WS_CAPTION)   ;; switch to full screen mode
    ShowWindow_(hWnd,#SW_MAXIMIZE)
  EndIf
EndProcedure

#WIN = 3
OpenWindow(#WIN,220,220,500,400,"*", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget)
TextGadget(1, 50, 50, 277, 33, "Press F11 for fullscreen mode")

AddKeyboardShortcut(#WIN, #PB_Shortcut_F11, 14) : BindEvent(#PB_Event_Menu, @toggleFullScreen(), #WIN, 14)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Post Reply