VC + + MFC How to save application configuration information to the Registry (ii)

Source: Internet
Author: User

In the previous article, we introduced several interfaces for writing registry data and reading registry data, and described how to use it.

This piece teaches you how to keep your application in the state it was in before the next time it was opened.

On the basis of the previous add code, to add the WM_CLOSE message response function, because we only have to save the window position information before the window closes, so save the window position to the registry code to write in this message handler function.

The code is as follows:

voidCtestclassdlg::onclose () {if(AfxGetApp ()->getprofileint ("Settings","saveposition",1))    {        //where to save the windowwindowplacement WP; GetWindowPlacement (&WP); AfxGetApp ()->writeprofileint ("Settings","Framestatus", Wp.showcmd); AfxGetApp ()->writeprofileint ("Settings","Frametop", Wp.rcNormalPosition.top); AfxGetApp ()->writeprofileint ("Settings","Frameleft", Wp.rcNormalPosition.left); AfxGetApp ()->writeprofileint ("Settings","Framebottom", Wp.rcNormalPosition.bottom); AfxGetApp ()->writeprofileint ("Settings","Frameright", Wp.rcNormalPosition.right); } cdialog::onclose ();}

Save the location information in the registry before the application is closed, and we can get this data the next time it is opened so that the application's window will appear before it is closed.

The code that initializes the window in MFC is generally added to the OnInitDialog () function.

The code is as follows:

BOOL Ctestclassdlg::oninitdialog () {cdialog::oninitdialog (); ................................    ................................        //Todo:add Extra Initialization here    intS, T, B, R, L; if(AfxGetApp ()->getprofileint ("Settings","saveposition",1))    {        //Only the restore if there is a previously saved position        if( -1! = (s = AfxGetApp ()->getprofileint ("Settings","Framestatus",   -1)) &&-1! = (t = AfxGetApp ()->getprofileint ("Settings","Frametop",      -1)) &&-1! = (L = AfxGetApp ()->getprofileint ("Settings","Frameleft",     -1)) &&-1! = (b = AfxGetApp ()->getprofileint ("Settings","Framebottom",   -1)) &&-1! = (R = AfxGetApp ()->getprofileint ("Settings","Frameright",    -1)) {windowplacement WP; //Restore the window ' s statusWp.showcmd =s; //Restore the window ' s width and heightWp.rcNormalPosition.bottom =b; Wp.rcNormalPosition.right=R; //The following correction is needed when the taskbar is//At the left or top and it's not "Auto-hidden"RECT Workarea; SystemParametersInfo (Spi_getworkarea,0, &workarea,0); L+=Workarea.left; T+=Workarea.top; //Make sure the window was not completely out of sight            intmax_x = GetSystemMetrics (sm_cxscreen)-GetSystemMetrics (Sm_cxicon); intmax_y = GetSystemMetrics (sm_cyscreen)-GetSystemMetrics (Sm_cyicon); Wp.rcNormalPosition.top=min (t, max_y); Wp.rcNormalPosition.left=min (l, max_x); SetWindowPlacement (&WP); }    }    returnTRUE;//return TRUE Unless you set the focus to a control}
Running the application

Then adjust the size and position of the window:

Close the program and open it again and you'll see that the application's window size and location are the same as the last time. You can test it!

The following are changes to the registry:

The writing is not very detailed, but these things are a layer of window paper stuff. There is no need to delve into it, and there is nothing to delve into. The important thing is to be able to use it.

In order to facilitate the use of the next time to quickly review, but also to help beginners, do the above simple finishing!

Here are some reference code, you can learn from it!

voidCmainframe::activateframe (intncmdshow) {    if(m_bfirst) {M_bfirst=FALSE; Windowplacement* PWNDPL =Newwindowplacement; PWNDPL->length =sizeof(windowplacement); CWINAPP* Papp =AfxGetApp (); //Restore window PositionPwndpl->flags = Papp->getprofileint (_t ("windowplacement"), _t ("FLAGS"),0); PWNDPL->showcmd = Papp->getprofileint (_t ("windowplacement"), _t ("ShowCmd"),0); nCmdShow= pwndpl->ShowCmd; PWNDPL->ptminposition.x = Papp->getprofileint (_t ("windowplacement"), _t ("MINX"),0); PWNDPL->PTMINPOSITION.Y = Papp->getprofileint (_t ("windowplacement"), _t ("Miny"),0); PWNDPL->ptmaxposition.x = Papp->getprofileint (_t ("windowplacement"), _t ("MAXX"),0); PWNDPL->PTMAXPOSITION.Y = Papp->getprofileint (_t ("windowplacement"), _t ("Maxy"),0); PWNDPL->rcnormalposition.top = Papp->getprofileint (_t ("windowplacement"), _t ("TOP"),0); PWNDPL->rcnormalposition.left = Papp->getprofileint (_t ("windowplacement"), _t (" Left"),0); PWNDPL->rcnormalposition.right = Papp->getprofileint (_t ("windowplacement"), _t (" Right"),0); PWNDPL->rcnormalposition.bottom = Papp->getprofileint (_t ("windowplacement"), _t ("BOTTOM"),0); //Set Window Positionsetwindowplacement (PWNDPL); DeletePWNDPL; } cframewnd::activateframe (nCmdShow);}voidCmainframe::onclose () {windowplacement* PWNDPL =Newwindowplacement; PWNDPL->length =sizeof(windowplacement); //Get window Positiongetwindowplacement (PWNDPL); CWINAPP* Papp =AfxGetApp (); //Save Window PositionPapp->writeprofileint (_t ("windowplacement"), _t ("FLAGS"), PWNDPL-flags); Papp->writeprofileint (_t ("windowplacement"), _t ("ShowCmd"), PWNDPL-showcmd); Papp->writeprofileint (_t ("windowplacement"), _t ("MINX"), PWNDPL-ptminposition.x); Papp->writeprofileint (_t ("windowplacement"), _t ("Miny"), PWNDPL-PTMINPOSITION.Y); Papp->writeprofileint (_t ("windowplacement"), _t ("MAXX"), PWNDPL-ptmaxposition.x); Papp->writeprofileint (_t ("windowplacement"), _t ("Maxy"), PWNDPL-PTMAXPOSITION.Y); Papp->writeprofileint (_t ("windowplacement"), _t ("TOP"), PWNDPL-rcnormalposition.left); Papp->writeprofileint (_t ("windowplacement"), _t (" Left"), PWNDPL-rcnormalposition.top); Papp->writeprofileint (_t ("windowplacement"), _t (" Right"), PWNDPL-rcnormalposition.right); Papp->writeprofileint (_t ("windowplacement"), _t ("BOTTOM"), PWNDPL-rcnormalposition.bottom); DeletePWNDPL; Cframewnd::onclose ();}

VC + + MFC How to save application configuration information to the Registry (ii)

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.