how to init dialog controls and also use DoModal()

Posted on 2009-04-03
Last Modified: 2013-11-20
Hi, i like to use dialog templates from the resources, but don't want to add classes for each new dialog.
For example, i have a CListCtrl that is empty. I want it to be filled with data, before it's shown to the user.
The DoModal() creates the dialog itself, and so it doesn't allow me to do any initialization.

So I went to use the callback procedure provided by DialogBox() / CreateDialog(). But this doesn't work. please see code below (1)
Next thing I tried using CDialog::Attach(). But then the DoModal() raises exceptions. please see (2)

Can you experts please give me a tip -

--> I want to avoid creating derived CDialog classes for each resource template each time
AND keep modal behaviour
AND  initialize dialog controls

- Is it possible ? Thanks very much experts!

// callback -------------------------------------------------------------------------------------------
                       // init controls ...
                       return TRUE;
return FALSE;
// attempt 1 -------------------------------------------------------------------------------------------
DialogBox(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_MYDIALOG), AfxGetMainWnd()->m_hWnd, (DLGPROC)DlgProc);
// attempt 2 -------------------------------------------------------------------------------------------
HWND hDlg = CreateDialog(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_MYDIALOG), AfxGetMainWnd()->m_hWnd, (DLGPROC)DlgProc);
CDialog dlg;
	int retval = dlg.DoModal();

Open in new window

Question by:stev75
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 19

Expert Comment

ID: 24059335
You must do the controls initialization in OnInitDialog() function.

OnInitDialog is the default handler for WM_INITDIALOG message
LVL 19

Expert Comment

ID: 24059350

Author Comment

ID: 24059387
Thanks, the question is>
If there's a way to use something like PreTranslate message or a Hook for my dialog instance, that is NOT a CDialog derived object ?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 19

Accepted Solution

alb66 earned 250 total points
ID: 24059541
You can derive a class from CDialog and pass the template to it

class CYourDialog : public CDialog
   CYourDialog( UINT nIDTemplate, CWnd* pParentWnd = NULL );

   virtual BOOL OnInitDialog();

CYourDialog::CYourDialog( UINT nIDTemplate, CWnd* pParentWnd ) <--- pass the template here
   : CDialog( nIDTemplate, pParentWnd  )

BOOL CYourDialog::OnInitDialog()

   // initialize controls here

Author Comment

ID: 24059799
yes, i thought of a more elegant way, but it may be that it's the only way

Author Closing Comment

ID: 31566223
i was way too fast with my previous comment (see below)... in fact, it IS elegant, since it is a good way of constructing the object and very short piece of code ! Thanks !!
LVL 44

Expert Comment

ID: 24059960
Two points - the first you might have realised, its unclear from your comment.

1)  As alb66 says the OnInitDialog is the place to do initialisation of controls - it is called BEFORE the dialog is displayed so you can fill the controls there.

2) Maybe this class layout is useful.
class CMyBaseDialog : CDialog
class CMyDerivedDialog1 : CMyBaseDialog
class CMyDerivedDialog2 : CMyBaseDialog

Then you can use virtual functions for stuff in the derived classes and have the common code in the CMyBaseDialog class.

Author Comment

ID: 24075022
Hi Andy,
2) the "one-level" architecture fits my needs. If you derive another class from your already CDialog -derived class, it would make sense if you want to have  fonts / colours for each dialog, which will be set in the base and each derived will automatically have it.
the point is, when you work with visual studio, and you design you're dialog VISUAL, you will get a new header and cpp file each time, generated by visual studio. I'm going to not use the generated stuff anymore, to have shorter and clearer implementation. This idea came up when I read the comment from alb66. I also think, this will speed up compilation time.
LVL 44

Expert Comment

ID: 24075074
you can still use the wizard generated files.
Consider - you make a new dialog in resource editor, create a new class for it eg. CMyNewDialog.  Now the wizard generates a cpp and h file with CMyNewDiaog based on CDialog.  In these two (cpp , h) files do a gobal find/replace with CDialog changed to CBaseDialog (or whatever the common class is called).  Finished - you now have the common code linked up.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ShiftLeft challenge 21 96
EvenOdd challenge 10 160
sumHeights  challenge 17 83
Notepad++ Question 11 28
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question