We help IT Professionals succeed at work.

MessageBox, AfxMessageBox; what is the difference ?

mrwad99
mrwad99 asked
on
10,277 Views
Last Modified: 2013-11-20
Ah hello.  Quick question this...

In my app I recently implemented code that would display a messagebox on the handling of a window message.  Now this handler is in the main app (i.e. MyApp.cpp).  However, when I code

MessageBox("Some string");

I get the error

'MessageBoxA' : function does not take 1 parameters

Yet this code works fine in my view class/dialogue classes.  Why ?

I can use AfxMessageBox("Some string") which will work fine, so what is the difference between the two ?

Also, MSDN states "Use the global function AfxMessageBox instead of this member function to implement a message box in your application" for CWnd::MessageBox.  Why is this ?

TIA !
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Right, understood the point about derivatives of CWnd; that is clear.  Thanks.

But why does MSDN state "Use the global function AfxMessageBox instead of this member function to implement a message box in your application" for CWnd::MessageBox".  Is there something wrong/less efficient with CWnd::MessageBox as opposed to the AfxMessageBox ?  I could not pick up anything to hint at that from the links given, but you may know of something....
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Try the following
In your app show a messagebox with AfxMessageBox and try to do something on your main app while the messagebox is visible.  Repeat with the MessageBox.  See the difference (at least it used to work differently, I hope it still does).

Commented:
Its the modal issue and the title of the Messagebox varies. AfxMessageBox acts as modal to main window. The default title I think it takes the executable name.
But MessageBox will act modal to CView or the view window. You can specify your own title.
Modal means you won't be able to work with your application until you close the message box.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
The default title I think it takes the executable name. (AfxMessageBox)

Yes - but it is possible to change it, how is quite well hidden in the help.

Author

Commented:
AndyAinscow/akalmani -

I tried the suggestion and there is no difference in the modal-ness (?) of the AfxMessageBox compared to that of the MessageBox.  The title is the same, which is the exe name.  One subtle difference is that there is the default 'alert' icon (a yellow triangle and black shriek) with AfxMessageBox() that does not occur with the standard MessageBox() present on the dialogue.

But that still does not explain *why* to use the Afx call.  Or should I just let the fact be that it "just is" better?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
GJanusz:

You state ::MessageBox, and the MessageBox function I was talking about was actually CWnd::MessageBox - the reason why I could not use this in my main app has already been explained by akalmani.  However, I am now aware that there are *three* types of MessageBox() functions; the new addition being this

::MessageBox()

you mention.  What is this, to what class does it belong and what is the reason for having this *and* AfxMessageBox() ?

TIA !

Commented:
Ah, well, I'm guessing you are firmly implanted in MFC.  MFC is a primarily a great big wrapper (sometimes thin and sometimes not) for direct calls to services made available by the operating system.  The direct Windows OS function calls are typically known as Win32 calls.  Win32 programming has no classes as you know them in MFC.  They belong to the classless namespace -- which is why I labeled the MessageBox function as ::MessageBox.  In other words, they are all standalone functions that typically leverage Windows-internal memory, or memory you need to provide, to keep track of its states.  HANDLEs are an example of this.

If that doesn't scare you sufficiently, then take note that some things you may want to do are not provided by the MFC wrappers, and it occasionally pays off to be familiar with the Win32 functionality it is wrapping, so you can go look it up to see if what you need is provided at the Win32 level.

If you step into almost any MFC code, you will run into Win32 functions calls that you can't step into, but will be documented (almost always, ahem).

Commented:
Clarification for 2nd to last sentence in 1st paragraph:
In other words, they are all standalone functions that typically leverage Windows-internal memory, or memory you need to provide, to keep track of its states [rather than keeping track of state information in a class instance].

Clarification for very last sentence:
Documented in MSDN, I mean.  Certainly not in the code itself!  *choke*

Author

Commented:
Ah, those Win32 functions are hidden away in all the windows DLLs then I take it, which is why you cannot step into them ?

And I also assume that AfxMessageBox is an MFC function; ::MessageBox as you have stated is a Win32 function.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Yes and Yes.  There are a tiny number of classless MFC functions.  The ones I know of all start with Afx like AfxMessageBox.  Another commonly used one is AfxGetMainWnd(), which you'll be glad for eventually!

Author

Commented:
GJanusz: Thanks.

AndyAinscow: I cannot get a messagebox to be modeless with the standard MessageBox() call.  It is always modal.  How did you achieve this ?
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
I cannot get a messagebox to be modeless with the standard MessageBox() call.  It is always modal.  How did you achieve this ?

I found this a few years ago - maybe it has changed now (bug fixed?)

My situation was I had an SDI app without a doc or view!
I wanted a toolbar and the SDI Frame gave support for that but I had stripped the doc/view out and replaced it with a dialog (actually property sheet).  Following from akalmani's earlier comment you might be able to reproduce it with an MDI based app.  It might not apply to a bog standard SDI or dialog.

Author

Commented:
Right I increased the points to 80 for an even split, as everyone gave relevant information.

Thanks to all for this !
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.