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

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


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 *)  := 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', * 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',,lpszFileName); WritePrivateProfileInt(lpszSectionName, 'Left',   rcPos.left,lpszFileName); WritePrivateProfileInt(lpszSectionName, 'Bottom', rcPos.bottom,lpszFileName); WritePrivateProfileInt(lpszSectionName, 'Right',  rcPos.right,lpszFileName);End;  {SavePlacement}End.
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job