devxlogo

How do I restore a window’s size and position?

How do I restore a window’s size and position?

Question:
How do I restore a window’s size and position?

Answer:

This new tip comes from Hamish. He has converteda TurboPascalfor Windows routine that allows you to save and restore any window’s sizeand state using WinAPI calls. This especially helpful for those whoseapplicationssave their work space. This is really nice code that I believe will help peoplewho need to do this sort of thing. This source code will allow for acouple of routines that you can use tosave and restore any window’s size, position and maximized/minimized state.

You will find usage notes at the start of each routine. Here is thecode from Hamish:

------------------------------------------------------------------------Unit WinPos;InterfaceUses  SysUtils, WinTypes,  WinProcs, Messages,  Dialogs;Procedure RestorePlacement(hWindow: HWND; lpszFileName, lpszSectionName:pChar; SetMin: BOOL);Procedure SavePlacement (hWindow: HWND; lpszFileName, lpszSectionName:pChar);Implementation(*******************************************************)(*    WritePrivateProfileInt                           *)(*                                                     *)(*  Writes an integer to a private profile .INI file.  *)(*  Service function used by SavePlacement.            *)(*******************************************************)Function WritePrivateProfileInt(lpszSection: pChar; lpszEntry: pChar;                                iToWrite: word; lpszFileName: pChar):BOOL;Var  szWork : array[0..10] of char;Begin  wvsprintf(szWork, '%06u', iToWrite);  WritePrivateProfileInt :=  WritePrivateProfileString(lpszSection, lpszEntry, szWork,lpszFileName);End;(************************************************************)(*    RestorePlacement                                      *)(*                                                          *)(*  Restores size, position & state to last saved placement.*)(*  If SetMin is True then the window will be minimized if  *)(*  it was minimized when SavePlacement was last executed.  *)(*  If SetMin is False the window will be restored to  *)(*  its normal (i.e. neither maximized or minimized) state.   *)(*                                                          *)(*  In Delphi you would call this routine from your         *)(*  FormCreate event handling code.                         *)(************************************************************)Procedure RestorePlacement(hWindow: HWND;          (*Handle of windowtorestore*)                           lpszFileName,           (*INI file name*)                           lpszSectionName: pChar; (*Section name in INIfile*)                           SetMin: BOOL); (*Restore position Ifminimized?*)Var wpPos  : TWINDOWPLACEMENT; ptPos  : TPOINT; rcPos  : TRECT;Begin wpPos.length := sizeof(wpPos);(* Flag to restore minimized position *) If (SetMin)   Then wpPos.flags := WPF_SETMINPOSITION   Else wpPos.flags := 0;(* Get window state (max, min or restored) *) wpPos.showCmd := GetPrivateProfileInt (lpszSectionName, 'ShowCmd',                                        SW_SHOWNORMAL, lpszFileName);(* Minimized position *) ptPos.x := GetPrivateProfileInt(lpszSectionName, 'MinX', -1,lpszFileName); ptPos.y := GetPrivateProfileInt(lpszSectionName, 'MinY', -1,lpszFileName); If ((ptPos.x = -1) or (ptPos.y = -1))  Then wpPos.flags := 0; wpPos.ptMinPosition := ptPos;(* Maximized position *) ptPos.x := GetPrivateProfileInt(lpszSectionName, 'MaxX', 0,lpszFileName); ptPos.y := GetPrivateProfileInt(lpszSectionName, 'MaxY', 0,lpszFileName); wpPos.ptMaxPosition := ptPos;(* Window position and size *) rcPos.top  := GetPrivateProfileInt(lpszSectionName, 'Top',GetSystemMetrics(SM_CYSCREEN)div 8, lpszFileName); rcPos.left := GetPrivateProfileInt(lpszSectionName, 'Left',GetSystemMetrics(SM_CXSCREEN)div 8, lpszFileName); rcPos.bottom := GetPrivateProfileInt(lpszSectionName, 'Bottom',rcPos.top * 7,lpszFileName); rcPos.right  := GetPrivateProfileInt(lpszSectionName, 'Right',rcPos.left* 7,lpszFileName); wpPos.rcNormalPosition := rcPos;(* Restore everything *) SetWindowPlacement(hWindow, @wpPos);End;  {RestorePlacement}(*************************************************************)(*    SavePlacement                                          *)(*                                                           *)(*  Saves size, position and Max/Min state to INI file.      *)(*                                                           *)(*  In Delphi you would normally call this routine from your *)(*  FormCloseQuery event handling code.                      *)(*************************************************************)Procedure SavePlacement (hWindow: HWND;           (*Handle of window tosave*)                         lpszFileName,            (*INI file name*)                         lpszSectionName: pChar); (*Section name in INIfile*)Var wpPos  : TWINDOWPLACEMENT; ptPos  : TPOINT; rcPos  : TRECT;Begin wpPos.length := sizeof(wpPos);(* Save everything *) GetWindowPlacement(hWindow, @wpPos);(* Save current window state (maximized, minimized or restored) *) WritePrivateProfileInt(lpszSectionName, 'ShowCmd', wpPos.showCmd,lpszFileName);(* Save Minimized position *) ptPos := wpPos.ptMinPosition; WritePrivateProfileInt(lpszSectionName, 'MinX', ptPos.x, lpszFileName); WritePrivateProfileInt(lpszSectionName, 'MinY', ptPos.y, lpszFileName);(* Save Maximized position *) ptPos := wpPos.ptMaxPosition; WritePrivateProfileInt(lpszSectionName, 'MaxX', ptPos.x, lpszFileName); WritePrivateProfileInt(lpszSectionName, 'MaxY', ptPos.y, lpszFileName);(* Save Restored position and size *) rcPos := wpPos.rcNormalPosition; WritePrivateProfileInt(lpszSectionName, 'Top',    rcPos.top,lpszFileName); WritePrivateProfileInt(lpszSectionName, 'Left',   rcPos.left,lpszFileName); WritePrivateProfileInt(lpszSectionName, 'Bottom', rcPos.bottom,lpszFileName); WritePrivateProfileInt(lpszSectionName, 'Right',  rcPos.right,lpszFileName);End;  {SavePlacement}End.
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist