Save application configuration in MFC using Writeprofileint and other methods

Source: Internet
Author: User

0. Introduction

A set of methods for reading and writing application configuration is provided in the CWinApp class:
Getprofileint
Writeprofileint

GetProfileString
WriteProfileString

It is easy to read and write application configuration.

1. About the Cwinapp::setregistrykey method

After using VC + + wizards to build the MFC project, you can see this statement in InitInstance:
Setregistrykey (_t ("native application generated by Application Wizard");

This function will set up a working environment for several of the methods mentioned above, and if you write data with Writeprofileint, you will

is written to the following registry location:
Hkey_current_user\software\ Application Wizard generated local application \ Application name \

If Setregistrykey is not executed in InitInstance, writing to the data with Writeprofileint writes to the

%windir%\ the application name. ini.

2. Usage A. If the Setregistrykey is executed in InitInstance ("The Breeze");

For the following:
Writeprofileint ("section", "Val1", 10);

The data will be written to the following path in the registry:

[hkey_current_user\software\ Wind house \ Test Application \section]
"Val1" =dword:0000000a

Note: "Test Application" is the name of the application.

B. If Setregistrykey is not executed in InitInstance

For the following:
Writeprofileint ("section", "Val1", 10);

will be written in the "%windir%\ test application. ini":

[Section]
val1=10

3. Example: Save the application's window size and location

Change the Big Hour
void Cmydlg::onsize (UINT nType, int cx, int cy)
if (m_binitdialog && cx<nx && cy<ny)
{
Save current size position
CRect Rcwindow;
GetWindowRect (&rcwindow);
Theapp.writeprofileint ("Settings", "left", rcwindow.left);
Theapp.writeprofileint ("Settings", "Top", rcwindow.top);
Theapp.writeprofileint ("Settings", "right", rcwindow.right);
Theapp.writeprofileint ("Settings", "bottom", rcwindow.bottom);
}

When you move a window
void Cmydlg::onmove (int x, int y)
{
if (m_binitdialog && x!=0 && y!=0)
{
Save current size position
CRect Rcwindow;
GetWindowRect (&rcwindow);
Theapp.writeprofileint ("Settings", "left", rcwindow.left);
Theapp.writeprofileint ("Settings", "Top", rcwindow.top);
Theapp.writeprofileint ("Settings", "right", rcwindow.right);
Theapp.writeprofileint ("Settings", "bottom", rcwindow.bottom);
}
}

Initialization
BOOL Cmydlg::oninitdialog ()
{
CRect Rcwindow;
Rcwindow.left=theapp.getprofileint ("Settings", "left", 200);
Rcwindow.top=theapp.getprofileint ("Settings", "top", 120);
Rcwindow.right=theapp.getprofileint ("Settings", "right", 800);
Rcwindow.bottom=theapp.getprofileint ("Settings", "bottom", 600);
MoveWindow (&rcwindow);
}

Save application configuration in MFC using Writeprofileint, etc.

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.