Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Pascal
Expertise: Beginner
Mar 19, 1997

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 converted a TurboPascal for Windows routine that allows you to save and restore any window's size and state using WinAPI calls. This especially helpful for those whose applications save their work space. This is really nice code that I believe will help people who need to do this sort of thing. This source code will allow for a couple of routines that you can use to save and restore any window's size, position and maximized/minimized state.

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

------------------------------------------------------------------------

Unit WinPos;

Interface

Uses

  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 window

torestore*)

                           lpszFileName,           (*INI file name*)

                           lpszSectionName: pChar; (*Section name in INI

file*)

                           SetMin: BOOL); (*Restore position If

minimized?*)

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 to

save*)

                         lpszFileName,            (*INI file name*)

                         lpszSectionName: pChar); (*Section name in INI

file*)



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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date