Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / MFC
Article

Save and restore dialog controls' states

Rate me:
Please Sign up or sign in to vote.
4.22/5 (12 votes)
28 Feb 20041 min read 76K   1.7K   20   16
A simple but universal way to save and restore state of dialog controls

Sample image

Introduction

In some (or most?) cases, when we are using dialog windows to communicate with the user, we need to save/restore state of checkboxes, edit controls, radio buttons, etc. There are several possibilities of how to do it. You can program functions newly for every dialog window or use "universal" solution. I created for myself one simple class that I am using in most cases with no changes. All that you will need to implement this function into your application is to add five lines of code to your dialog source file.

Using the code

First of all, if you want to use CDialogSettings class in your application, you must include these header files:

#include "stdafx.h"
#include "resource.h"
#include "dlgset.h"    <= CDialogSettings class

To restore dialog controls' states (text of edit controls, checked/unchecked state of checkboxes, etc.) we will need to add the following two lines of code to the dialog initialization routine. If you are creating a non-MFC application, insert CDialogSettings class here:

INT_PTR CALLBACK dialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg) {
    case WM_INITDIALOG:
      {
        CDialogSettings ds(hwndDlg);
        ds.Load("Software\\CodeProject\\DialogAppExample");
      }
      break;
      ...

If you are creating an MFC application, insert CDialogSettings class here:

BOOL CDlgSetExampleDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  
  CDialogSettings ds(m_hWnd);
  ds.Load("Software\\CodeProject\\DialogAppExample");   

  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

Code that will store dialog controls' state (CDialogSettings class uses registry) you can add into the IDOK handler (you need to save settings only if the user confirms dialog), or into the WM_DESTROY / OnDestroy routine. In the non-MFC application, it will look like this:

case WM_DESTROY:
      {
        CDialogSettings ds(hwndDlg);
        ds.Save("Software\\CodeProject\\DialogAppExample");
      }
      break;

and in the MFC application:

void CDlgSetExampleDlg::OnDestroy() 
{
  CDialogSettings ds(m_hWnd);
  ds.Save("Software\\CodeProject\\DialogAppExample");

  CDialog::OnDestroy();  
}

That's all. I hope, you find this class useful.

This demo project (and also article) was created using umdev - multi-compiler IDE.

History

  • 24. February 2004 - First release.
  • 1. March 2004 - Updated project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Project doesn't work Pin
Anonymous2-May-04 22:32
Anonymous2-May-04 22:32 
GeneralMFC: small correction Pin
allelimo28-Feb-04 2:56
allelimo28-Feb-04 2:56 
GeneralRe: MFC: small correction Pin
uemaker28-Feb-04 3:24
uemaker28-Feb-04 3:24 
GeneralComments on the code Pin
khb25-Feb-04 20:45
khb25-Feb-04 20:45 
GeneralRe: Comments on the code Pin
uemaker25-Feb-04 20:55
uemaker25-Feb-04 20:55 
GeneralRe: Comments on the code Pin
uemaker25-Feb-04 22:45
uemaker25-Feb-04 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.