[Source] Restore Windows for OpenScreen() [Windows OS]

Share your advanced PureBasic knowledge/code with the community.
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

[Source] Restore Windows for OpenScreen() [Windows OS]

Post by Mijikai »

So every window that is open when OpenScreen() is called can be misplaced (ex. multi monitor system) after the progam ends. :twisted:
This is highly annoying, so i wrote some code to reset all windows. :D

Code & Example (should also work on x86):

Code: Select all


EnableExplicit

Procedure.i RestoreWindows(Handle.i,Store.i)
  Static NewList *wp.WINDOWPLACEMENT()
  Select Store
    Case #True
      ForEach *wp()
        FreeMemory(*wp())
      Next
      ClearList(*wp())
      EnumWindows_(@RestoreWindows(),-1)
      ProcedureReturn #Null
    Case #False
      ForEach *wp()
        Store = PeekI(*wp() + SizeOf(WINDOWPLACEMENT))
        SetWindowPlacement_(Store,*wp())
        If *wp()\showCmd = #SW_MAXIMIZE;<- quick hack to deal with maximized windows ^_^
          ShowWindow_(Store,#SW_RESTORE)
          ShowWindow_(Store,*wp()\showCmd)
        EndIf
        FreeMemory(*wp())
      Next
      ClearList(*wp())
      ProcedureReturn #Null
    Default
      If IsWindowVisible_(Handle)
        Store = AllocateMemory(SizeOf(WINDOWPLACEMENT) + SizeOf(Integer))
        If Store
          If AddElement(*wp())
            *wp() = Store
            *wp()\Length = SizeOf(WINDOWPLACEMENT)
            GetWindowPlacement_(Handle,*wp())
            PokeI(*wp() + SizeOf(WINDOWPLACEMENT),Handle)
          Else
            FreeMemory(Store)
          EndIf
        EndIf
      EndIf
      ProcedureReturn #True
  EndSelect
EndProcedure

RestoreWindows(#Null,#True);<- store all window positions

InitSprite()
If OpenScreen(800,600,32,#Null$,#PB_Screen_WaitSynchronization,60)
  Repeat
    FlipBuffers()
  Until GetAsyncKeyState_(#VK_ESCAPE) & 1
  CloseScreen()
EndIf

RestoreWindows(#Null,#False);<- restore all window positions

End
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: [Source] Restore Windows for OpenScreen() [Windows OS]

Post by idle »

good tip thanks
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Re: [Source] Restore Windows for OpenScreen() [Windows OS]

Post by Jeromyal »

This is great,

The compiler flag for DPI awareness can not be on or strange things happen.
But who needs a full screen app to be dpi aware anyway? (rhetorical)
however if there was a way to switch it off in code one could have a startup dialog dpi aware beforehand? (This ONE does not know)

I was initially excited seeing the title of this, thinking someone had solved the old mystery of how to switch from full screen to windowed screen and vise versa while using the 3d engine.
However, This is equally as good of a prize.
Post Reply