Windows Client development -- enable your client to remember the size and location of the last shutdown while running
Almost all windows clients can be resized. Therefore, you can adjust the client size and location based on your preferences.
However, when the client exits and re-runs the client, we often have to adjust the size and position we like.
The windows client remembers the size and position of the client when it exits. When it is run next time, the window displays the size and position you like.
The current task is a small feature.
First, you must have thought of using the configuration file. When the client exits, the client window information is recorded in a configuration file, which is generally a. ini file. Reading and writing the configuration file is simple. I will not go into details here.
However, one of the fatal disadvantages of using a configuration file is that it is easy for others to find and modify it.
Therefore, it is difficult for users to find this information by using a high-end method. This is written to the registration area, and then read the registration area to remember the windows information that was last disabled.
You do not need to store the width and height variables of the client separately. windows provides us with a struct:
WINDOWPLACEMENT
Prototype:
typedef struct tagWINDOWPLACEMENT{ /* wndpl */UINT length;UINT flags;UINT showCmd;POINT ptMinPosition;POINT ptMaxPosition;RECT rcNormalPosition;} WINDOWPLACEMENT;
Meaning:
Length
Length specifies the length of the structure, in bytes.
Flags
Flags specifies the flag for controlling the location of the minimized window and the method for restoring the window. This member can be one of the logos listed below, or both: · WPF_SETMINPOSITION indicates that the x and y coordinates of the window can be minimized. If you are setting coordinates in the ptMinPosition member, you must specify this flag.
ShowCmd
WPF_RESTORETOMAXIMIZED indicates that the restored window will be maximized, regardless of whether it is maximized before the minimization. This setting is only valid in the next recovery window. It does not change the default Restoration Operation. This flag is valid only when SW_SHOWMINIMIZED is specified in the showCmd member.
ShowCmd specifies the current display status of the window. This member can be one of the following values :·
SW_HIDE hides the window and enables other windows to be activated.
· SW_MINIMIZE minimizes the specified window and activates the top-level window in the system list.
· SW_RESTORE is activated and a window is displayed. If the window is minimized or maximized, Windows restores it to its original size and position (the same as SW_SHOWNORMAL ).
· SW_SHOW activates the window and displays the window according to the current position and size.
· SW_SHOWMAXIMIZED activation window and display it to the maximum extent.
· SW_SHOWMINIMIZED activation window and display it as an icon.
· SW_SHOWMINNOACTIVE displays the window as an icon. The currently activated window remains active.
· SW_SHOWNA display window by current status. The currently activated window remains active.
· SW_SHOWNOACTIVATE displays the Window Based on the nearest position and size. The currently activated window remains active.
· SW_SHOWNORMAL is activated and a window is displayed. If the window is minimized or maximized, Windows restores it to its original size and position (the same as SW_RESTORE ).
PtMinPosition
PtMinPosition specifies the position in the upper left corner when the window is minimized.
PtMaxPosition
PtMaxPosition specifies the position in the upper left corner when the window is maximized.
RcNormalPosition
RcNormalPosition specifies the coordinates when the window is in the normal state (restoring.
So how can we get the struct information:
GetWindowPlacementFunctions can:
Function prototype
BOOL GetWindowPlacement (HWND hWnd, WINDOWPLACEMENT * lpwndpl );
Parameters
HWnd: Window handle.
Lpwndpl: pointer to the WINDOWPLACEMENT structure, which stores the status and position information.
Before calling the GetWindowPlacement function, set the length of the WINDOWPLACEMENT structure
Sizeof (WIDNOWPLACEMENT ). If lpwndpl-> length is set incorrectly, the GetWindowPlacement function fails.
The next task is how to write data to the registration area. Here we first use the function in MFC, which can be simpler.
The WriteProfileBinary () is part of the MFC class CWinApp which simply dumps the WINDOWPLACEMENT structure into the registry as a REG_BINARY value called "WP" in a key called MainFrame. A good idea, especially for child windows of the CMainFrame class, is to replace the hard-coded MainFrame with the caption of the frame.
BOOL CMainFrame::DestroyWindow() { WINDOWPLACEMENT wp; GetWindowPlacement(&wp); AfxGetApp()->WriteProfileBinary("MainFrame", "WP", (LPBYTE)&wp, sizeof(wp)); return CMDIFrameWnd::DestroyWindow();}
The next step is how to retrieve data from the registration area:
Use the function: GetProfileBinary provided in MFC
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) { CMDIFrameWnd::OnShowWindow(bShow, nStatus); static bool bOnce = true; if(bShow && !IsWindowVisible() && bOnce) { bOnce = false; WINDOWPLACEMENT *lwp; UINT nl; if(AfxGetApp()->GetProfileBinary("MainFrame", "WP", (LPBYTE*)&lwp, &nl)) { SetWindowPlacement(lwp); delete [] lwp; } }}
In this way, the windows information is accessed in MFC.
Next, we will discuss how to implement the above operations in win32 application.