How to find the size of a restored window

Just starting out? Need help? Post your questions and find answers here.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

How to find the size of a restored window

Post by akj »

If a PB program is currently displaying a maximised window, how can I determine the size that it would be when restored to normal size without actually restoring it. This is being done JUST before the program terminates to enable me to save the final non-maximised window metrics to an INI file. Is there an API to discover the size?

I don't want to restore the window as the flash is quite noticable to the user and I have failed prevent the flash despite trying techniques such as hiding the window. I've also tried:

Code: Select all

SendMessage_(WindowID(#winMain), #WM_SETREDRAW, #False, 0)
but this messes up the screen when the program ends.
Anthony Jordan
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Edit: Ah well. But my solution is simple and with no API.

You can't. You have to store the restored size and the maximized state separately. Every time the user resizes the window, you update the stored size, except when the size event was triggered by a maximize or minimize. When the program ends, write the values to disk.

Code: Select all

OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget)
CreateGadgetList(WindowID(0))


Repeat
  Select WaitWindowEvent()
    Case #PB_Event_SizeWindow, #PB_Event_MoveWindow
      If GetWindowState(0) = #PB_Window_Normal
        Debug "Store X/Y/W/H"
      EndIf
      Debug "Store state"
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Thank you Sparkie, the use of GetWindowPlacement_() solved it. My resulting code is:

Code: Select all

Define flags, info.WINDOWPLACEMENT
flags = #PB_Window_SystemMenu|#PB_Window_TitleBar
flags | #PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget
flags | #PB_Window_SizeGadget
OpenWindow(0, 100, 200, 300, 400, "Test", flags) 
CreatePreferences("myfile.ini")
info\length = SizeOf(WINDOWPLACEMENT)
GetWindowPlacement_(WindowID(0), @info)
With info\rcNormalPosition
  WritePreferenceInteger("WinX", \left)
  WritePreferenceInteger("WinY", \top)
  WritePreferenceInteger("Width", \right-\left-2*GetSystemMetrics_(#SM_CXFRAME))
  WritePreferenceInteger("Height", \bottom-\top-2*GetSystemMetrics_(#SM_CYFRAME)-GetSystemMetrics_(#SM_CYCAPTION))
EndWith
ClosePreferences()
End
Incidentally, if the OpenWindow() flag #PB_Window_SizeGadget is omitted, then the width and height saved in the .INI file are 298, 398 rather then the correct 300, 400. Could this be a previously unnoticed PureBasic bug?
Anthony Jordan
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Sparkie's code probably uses values incompatible with PB's values (which do not include window borders).
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@Trond:

I must admit that when I first read your reply I dismissed it as being rather trivial. But on re-reading it (because of the extra comment you added), I now realise it is a perfectly viable solution and I intend to adopt it because of it's platform independence. Thank you.
Anthony Jordan
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

akj wrote:Incidentally, if the OpenWindow() flag #PB_Window_SizeGadget is omitted, then the width and height saved in the .INI file are 298, 398 rather then the correct 300, 400. Could this be a previously unnoticed PureBasic bug?
No bug, the code just needs a little adjustment...

Code: Select all

Define flags, info.WINDOWPLACEMENT 
flags = #PB_Window_SystemMenu|#PB_Window_TitleBar 
flags | #PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget 
;flags | #PB_Window_SizeGadget 
OpenWindow(0, 100, 200, 300, 400, "Test", flags) 
CreatePreferences("myfile.ini") 
info\length = SizeOf(WINDOWPLACEMENT) 
GetWindowPlacement_(WindowID(0), @info) 
cxFrame = GetSystemMetrics_(#SM_CXFIXEDFRAME) * 2
cyFrame = GetSystemMetrics_(#SM_CYFIXEDFRAME) * 2
cyCaption = GetSystemMetrics_(#SM_CYCAPTION)
If flags & #WS_THICKFRAME
  cxFrame = GetSystemMetrics_(#SM_CXFRAME) * 2
  cyFrame = GetSystemMetrics_(#SM_CYFRAME) * 2
EndIf

With info\rcNormalPosition 
  WritePreferenceInteger("WinX", \left) 
  WritePreferenceInteger("WinY", \top) 
  WritePreferenceInteger("Width", \right-\left-cxFrame) 
  WritePreferenceInteger("Height", \bottom-\top-cyFrame-cyCaption) 
EndWith 
ClosePreferences() 
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply