Windows Client Development--Keep your client runtime in mind about the size and location of the last shutdown

Source: Internet
Author: User

Almost all Windows clients can be resized, so the user adjusts the size and location of the client to suit their preferences.

But when the client exits and re-runs the client, we often have to adjust the size and location of our preferences.

The Windows client does remember the size and location of the client exit time, and the next time you run it directly, the window appears directly to your preferred size and location.

Now the task is 818 of this little function.

First of all, you must have thought of using a configuration file. When the client exits, the client window information is recorded in a configuration file, which is generally an. ini file. Reading and writing the configuration file is very simple, here will not repeat it.

However, one of the fatal drawbacks of using a profile is that it is easy for others to find the file, and perhaps some modifications can be made to it.

So, to come up with a tall approach, it's not easy for users to find this information. This is written to the registration area and then read the registration area, which implements remembering Windows information when it was last closed.

You do not need to store variables such as the width and height of the client separately, and Windows provides a structure for us:
windowplacement
Prototype:

struct/* wndpl */length;UINT flags;UINT showCmd;POINT ptMinPosition;POINT ptMaxPosition;RECT rcNormalPosition;} WINDOWPLACEMENT;

Significance:
Length
length specifies how long the structure is, in bytes.

Flags
Flags specifies the flags that control the location of the minimized window and how to restore the window. This member can be one of the flags listed below, or both: • Wpf_setminposition indicates that the x and Y coordinates of the minimized window can be specified. If you are setting coordinates in a 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 it is minimized. This setting is only valid for the next recovery window. It does not change the default undo operation. This flag is valid only if sw_showminimized is specified in the ShowCmd member.
SHOWCMD Specifies the current display state of the window. This member can be one of the following values: •
Sw_hide hides the window and makes the other windows active.
· Sw_minimize minimizes the specified window and activates the top-level window in the System list.
· Sw_restore activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_SHOWNORMAL).
· Sw_show Activates the window and displays it in the current position and size.
· Sw_showmaximized Activates the window and displays it as maximized.
· Sw_showminimized Activates the window and displays it as an icon.
· Sw_showminnoactive Displays the window as an icon. The currently active window remains active.
· Sw_showna Displays the window in its current state. The currently active window remains active.
· Sw_shownoactivate Displays the window at the nearest position and size. The currently active window remains active.
· Sw_shownormal activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position (same as Sw_restore).

Ptminposition
Ptminposition Specifies the position of the upper-left corner when the window is minimized.

Ptmaxposition
Ptmaxposition Specifies the position of the upper-left corner when the window is maximized.

Rcnormalposition
rcnormalposition specifies the coordinates of the window when it is in its normal state (resiliency).

So how do we get information about this struct:
The getwindowplacement function can do this:

Function prototypes
BOOL getwindowplacement (HWND HWND,WINDOWPLACEMENT*LPWNDPL);

Parameters
HWnd: Window handle.
LPWNDPL: A pointer to the WINDOWPLACEMENT structure that stores display state and location information.
Before calling the GetWindowPlacement function, set the length of the windowplacement structure to
sizeof (widnowplacement). If the Lpwndpl->length setting is incorrect, the function getwindowplacement will fail.

The next task is how to write to the registration area, where we first use the functions in MFC, which can be simpler.

The Writeprofilebinary () is part of the MFC class CWinApp which simply dumps the windowplacement structure into the regist Ry as a reg_binary value called "WP" in a key called MainFrame. A good idea, especially for child, windows of the CMainFrame class, was to replace the hard-coded MainFrame with the caption of the frame.

BOOL CMainFrame::DestroyWindow() {    WINDOWPLACEMENT wp;    GetWindowPlacement(&wp);    AfxGetApp()->WriteProfileBinary("MainFrame", "WP", (LPBYTE)sizeof(wp));    return CMDIFrameWnd::DestroyWindow();}

The next step is to take the data from the registration area:
Using the functions provided in MFC: Getprofilebinary

void   CMainFrame :: Onshowwindow (BOOL bShow, UINT nstatus) {cmdiframewnd :: O Nshowwindow (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  ; delete  [] LWP ; } }}

This implements the information that accesses windows in MFC.

Next, you'll discuss how to implement the above operations in Win32 application.

Windows Client Development--Keep your client runtime in mind about the size and location of the last shutdown

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.