Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

simple modal dialog

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Burt - 28 Jun 2005 02:35 GMT
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...?
Jeff Partch [MVP] - 28 Jun 2005 03:06 GMT
> I need to put up a simple modal dialog, and am surprised how hard it seems
> to be.  Am I missing something here?
[quoted text clipped - 8 lines]
>                 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.

Signature

Jeff Partch [VC++ MVP]

Burt - 28 Jun 2005 04:02 GMT
>> I tried using
>>
[quoted text clipped - 3 lines]
> What happens if you replace your (HINSTANCE)0 argument with
> GetModuleHandle(NULL)...?

Same thing. No change in behavior

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

-1

>> This sure seems like something that should be accomplished in a one-line
>> call...?
>
> 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?
Jeff Partch [MVP] - 28 Jun 2005 12:24 GMT
> >> but the dialog never shows and it just drops through, giving me a
> > non-useful
[quoted text clipped - 3 lines]
>
> -1

Do then follow the advice to see what GetLastError has to say. What OS are
you testing on?

> >> This sure seems like something that should be accomplished in a one-line
> >> call...?
[quoted text clipped - 8 lines]
> 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.

Signature

Jeff Partch [VC++ MVP]

Scott McPhillips [MVP] - 28 Jun 2005 03:06 GMT
> I need to put up a simple modal dialog, and am surprised how hard it seems
> to be.  Am I missing something here?
[quoted text clipped - 18 lines]
> 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.

Signature

Scott McPhillips [VC++ MVP]

Burt Johnson - 28 Jun 2005 05:14 GMT
> 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
[quoted text clipped - 5 lines]
> 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).

Signature

- Burt Johnson
 MindStorm, Inc.
 http://www.mindstorm-inc.com/software.html

William DePalo [MVP VC++] - 28 Jun 2005 05:39 GMT
>> 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.
[quoted text clipped - 4 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.