Solved

how to init dialog controls and also use DoModal()

Posted on 2009-04-03
9
1,124 Views
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 -------------------------------------------------------------------------------------------

INT_PTR CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch(uMsg)

    {

	case WM_INITDIALOG:

	{

                       // 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);

if(hDlg)

{

CDialog dlg;

if(dlg.Attach(hDlg))

	{

	int retval = dlg.DoModal();

	dlg.Detach();

	}

}

Open in new window

0
Comment
Question by:stev75
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Expert Comment

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

OnInitDialog is the default handler for WM_INITDIALOG message
0
 
LVL 19

Expert Comment

by:alb66
ID: 24059350
0
 

Author Comment

by:stev75
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 ?
0
 
LVL 19

Accepted Solution

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

class CYourDialog : public CDialog
{
public:
   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()
{
   Dialog::OnInitDialog();

   // initialize controls here
   ....
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Author Closing Comment

by:stev75
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 !!
0
 
LVL 44

Expert Comment

by:AndyAinscow
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.
0
 

Author Comment

by:stev75
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.
0
 
LVL 44

Expert Comment

by:AndyAinscow
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 51
ffmpeg - "rtsp://...... Operation not permitted" 4 74
lucky13 challenge 11 123
matchUp  challenge 6 53
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now