Mega Code Archive

 
Categories / Delphi / Forms
 

How to Restore a Delphi Form from Minimized Maximized state to the Previous State, Programmatically!

Title: How to Restore a Delphi Form from Minimized / Maximized state to the Previous State, Programmatically! In SDI and MDI Delphi applications, at a certain moment in the application life, there might be several forms (in SDI; or MDI child forms in a MDI application) created and opened (only one active though). By design, in the upper right corner of the form window the three buttons can be used by the user to minimize, restore and maximize (and close) the form. The WindowState property of the TForm object represents how the form appears on the screen. You can set WindowState property to minimize, maximize, or restore the form window (wsNormal, wsMinimized, wsMaximized). Back to Previous State if Minimized? One problem with the WindowState property is that if a form is minimized and you need to restore it to the previous size and location - you do not know if the form was previously maximized or was displayed in its "normal" (neither minimized nor maximized) state! Let's say that a form is minimized and you want to restore it programmatically. If it was maximized you need to maxiimize it; if it was in the nrmal state you need to return it to the normal state. To determine if the form is minimized you can read the WinowState property. Since you do not know what was the state of the form before it was minimized, you do not know to what state to restore it, either to maximized or to normal? In other words, you cannot use the WindowState property to restore the form to its previous window state! The ShowWindow API! To programmatically restore the form to irs previous state be it minimized, maximized or normal, you should use ShowWindow API function. This function is defined in the Windows unit. Even though the name is "ShowWindow" this function will not necessarily activate (show) the window if you do not say it. The ShowWindow function sets the specified window's show state. It takes two parameters: Handle of the window and an integer that specifies how the window is to be shown. To restore the form to its previous state use this call: ShowWindow(theForm.Handle, SW_RESTORE) ; "theForm" is the name of the form you need to programmatically restore to its previous state. The "SW_RESTORE" activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. Used when restoring a minimized window.