Page 1 of 1
How to find the size of a restored window
Posted: Thu Dec 18, 2008 11:51 pm
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.
Posted: Fri Dec 19, 2008 12:14 am
by Sparkie
Posted: Fri Dec 19, 2008 12:16 am
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
Posted: Fri Dec 19, 2008 1:14 am
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?
Posted: Fri Dec 19, 2008 1:21 am
by Trond
Sparkie's code probably uses values incompatible with PB's values (which do not include window borders).
Posted: Fri Dec 19, 2008 1:53 am
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.
Posted: Fri Dec 19, 2008 3:15 am
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