65.9K
CodeProject is changing. Read more.
Home

Save and restore dialog controls' states

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.22/5 (10 votes)

Feb 24, 2004

1 min read

viewsIcon

77083

downloadIcon

1718

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.