simple modal dialog

B

Burt

I need to put up a simple modal dialog, and am surprised how hard it seems
to be. Am I missing something here?

I have created a dialog resource IDD_DELETE_S9. It has some static text
plus 3 buttons. The buttons have ids of IDOK, IDCANCEL and IDC_RENAME. All
I want to do is put up this modal dialog asking the user what to do. All I
care is which button he pressed.

I tried using

int answer = DialogBox(0, MAKEINTRESOURCE(IDD_DELETE_S9),
hwnd, (DLGPROC)Import_DialogProc);

but the dialog never shows and it just drops through, giving me a non-useful
'answer'.

Everything I am now reading in MSDN seems to indicate that I need to write a
new class inheriting from CDialog to handle this and return the button
pressed. Is that really needed?

This sure seems like something that should be accomplished in a one-line
call...?
 
J

Jeff Partch [MVP]

Burt said:
I need to put up a simple modal dialog, and am surprised how hard it seems
to be. Am I missing something here?

I have created a dialog resource IDD_DELETE_S9. It has some static text
plus 3 buttons. The buttons have ids of IDOK, IDCANCEL and IDC_RENAME. All
I want to do is put up this modal dialog asking the user what to do. All I
care is which button he pressed.

I tried using

int answer = DialogBox(0, MAKEINTRESOURCE(IDD_DELETE_S9),
hwnd, (DLGPROC)Import_DialogProc);

What happens if you replace your (HINSTANCE)0 argument with
GetModuleHandle(NULL)...?
but the dialog never shows and it just drops through, giving me a non-useful
'answer'.

What does it return?
Everything I am now reading in MSDN seems to indicate that I need to write a
new class inheriting from CDialog to handle this and return the button
pressed. Is that really needed?

Not really.
This sure seems like something that should be accomplished in a one-line
call...?

Yup. Followed by evaluation of the return of course.
 
S

Scott McPhillips [MVP]

Burt said:
I need to put up a simple modal dialog, and am surprised how hard it seems
to be. Am I missing something here?

I have created a dialog resource IDD_DELETE_S9. It has some static text
plus 3 buttons. The buttons have ids of IDOK, IDCANCEL and IDC_RENAME. All
I want to do is put up this modal dialog asking the user what to do. All I
care is which button he pressed.

I tried using

int answer = DialogBox(0, MAKEINTRESOURCE(IDD_DELETE_S9),
hwnd, (DLGPROC)Import_DialogProc);

but the dialog never shows and it just drops through, giving me a non-useful
'answer'.

Everything I am now reading in MSDN seems to indicate that I need to write a
new class inheriting from CDialog to handle this and return the button
pressed. Is that really needed?

This sure seems like something that should be accomplished in a one-line
call...?

It's only hard if you have no idea what you are doing. If you are using
MFC then the DialogBox API call is not applicable.

In Visual C++ the IDE 'class wizard' writes the new class derived from
CDialog for you. Right click on your dialog template and select Class
Wizard. Name your CDialog class.

CYourDialog dlg;
dlg.DoModal();

DoModal returns IDOK or IDCANCEL. Detecting your unique button requires
you to do something, like set a bool when it is clicked.
 
B

Burt

I tried using
What happens if you replace your (HINSTANCE)0 argument with
GetModuleHandle(NULL)...?

Same thing. No change in behavior
What does it return?
-1


Yup. Followed by evaluation of the return of course.

Yes, I have code folloing that that looks like:

if (answer == IDCANCEL)
return FALSE; // user decided to stop

etc. So you are saying that my DialogBox call should be enough? It never
enters my CALLBACK and never shows the dialog. Any idea why that would be?
 
B

Burt Johnson

Scott McPhillips said:
It's only hard if you have no idea what you are doing. If you are using
MFC then the DialogBox API call is not applicable.

I never said I knew what I was doing. I have only been using MFC for a
couple months, after 32 years programming other environments. No need
to be insulting though...

Why is DialogBox not applicable for MFC? Is that another of the
'managed memory model' (or whatever it is called) routines? If so, how
the heck can I tell the difference when reading MSDN. I have led down
that path a couple times now, and it is frustrating, to say the least.
In Visual C++ the IDE 'class wizard' writes the new class derived from
CDialog for you. Right click on your dialog template and select Class
Wizard. Name your CDialog class.

CYourDialog dlg;
dlg.DoModal();

DoModal returns IDOK or IDCANCEL. Detecting your unique button requires
you to do something, like set a bool when it is clicked.

Yeah, I know how to go through that hoop. Just seems silly to have to
add a whole new class just to get something simple like this. In other
environments I have programmed in, it was a simple one-line call with
the addition of a resource to define the appearance (which I have
created).
 
W

William DePalo [MVP VC++]

Burt Johnson said:
I never said I knew what I was doing. I have only been using MFC for a
couple months, after 32 years programming other environments. No need
to be insulting though...

Why is DialogBox not applicable for MFC?

DialogBox() is a function in the Win32 API. MFC provides a CDialog class to
simplify things and hide some of the details.
Is that another of the 'managed memory model' (or whatever
it is called) routines? If so, how the heck can I tell the
difference when reading MSDN.

No, it is not. MFC is a class library used by and large to build native
applications.
I have led down that path a couple times now, and it is frustrating,
to say the least.

Well, it is at worst an embarassment of choices. There is the native API,
MFC, ATL, WTL, .Net Framework, etc
Yeah, I know how to go through that hoop. Just seems silly to have to
add a whole new class just to get something simple like this.

I guess that depends on one's perspective and expectations.
In other environments I have programmed in, it was a simple one-line call
with
the addition of a resource to define the appearance (which I have
created).

Well, there is the MessageBox() function which can put up a dialog in one
line but you'll have to settle for predefined sets of buttons such as ok, or
yes and no or yes and no and cancel etc

Back to your problem ... One thing you can do is to pass the ID of the
button used to close the dialog to the EndDialog() call that dismisses the
dialog. EndDialog() sees to it that that value is returned by the call to
DialogBox(). Since control IDs are small positive numbers, if you get 0 or a
negative result then the call failed. In that case call GetlastError() to
determine the reason why the call failed. If you don't understand the code -
they are defined in <winerror.h> - you can post it here.

Regards,
Will
 
J

Jeff Partch [MVP]

Burt said:

Do then follow the advice to see what GetLastError has to say. What OS are
you testing on?
Yes, I have code folloing that that looks like:

if (answer == IDCANCEL)
return FALSE; // user decided to stop

etc. So you are saying that my DialogBox call should be enough? It never
enters my CALLBACK and never shows the dialog. Any idea why that would
be?

I agree that it's not the MFC way to so it -- and concur with the
admonitions for the most part, but in a quick test it works for me -- even
using the (HINSTANCE)0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top