Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MessageBox, AfxMessageBox; what is the difference ?

Posted on 2004-03-26
18
Medium Priority
?
6,965 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 !
0
Comment
Question by:mrwad99
  • 6
  • 4
  • 4
  • +2
18 Comments
 
LVL 3

Accepted Solution

by:
akalmani earned 80 total points
ID: 10687549
AfxMessageBox is a MFC inbuilt wrapper which takes only the Text and buttons (ID's buttons and Icons needed) as the parameters. But MessageBox is a CWnd function and it takes Text , Caption and Buttons(ID's buttons and Icons needed) as the parameters. You if are calling MessageBox in any of the CWnd derived classes then you need not pass Handle to the Window, Caption and button ID.

SDK function ::MessageBox(HWND, Text, Caption, UINT) -> HWND can be NULL, Need to give text, Caption can be NULL, UINT is defaulted to MB_OK.
CWnd derived classes : MessageBox(Text, Caption, UINT) -> Caption can be NULL, Text has to be given, UINT is defaulted to MB_OK.

CView is derived from CWnd so it worked. In CWinApp class you need to specify all or use AfxMessageBox everywhere.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10687628
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....
0
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 45

Expert Comment

by:AndyAinscow
ID: 10687744
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).
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10687881
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.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10687917
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.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10688429
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?
0
 
LVL 1

Assisted Solution

by:GJanusz
GJanusz earned 80 total points
ID: 10689010
@ Author

I've always assumed that the MFC developers made AfxMessageBox because they thought it would be a commonly used function, and wanted to make a more MFCized version of it than ::MessageBox.  It may have been based on their experience from what was commonly needed when just writing Win32 apps.

The biggest difference doesn't affect you:  You can't use AfxMessageBox unless you are using MFC.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 80 total points
ID: 10689112
I found in MSDN that you can pass a help id to AfxMessageMap. Then - when pressing F1 - a specific help of that message box is available.

That is the only difference i could find. However, i never used this feature and i don't know any application that uses it.

Regards, Alex
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10689445
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 !
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10690265
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).
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10690293
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*
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10690324
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.
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 80 total points
ID: 10690444
Some development since my last comment.
MesssageBox and ::MessageBox are identical UNLESS you have a function in your class called MessageBox which takes the same parameters.

I had the situation that a client wanted a different title than the default with AfxMessageBox, so I thought simple, replaced AfxMessageBox with MessageBox and gave it the new title.
What I found was the following.  The message box appeared with the new title BUT the user could still do things on the application - it behaved as modeless.  Now most uses of the message box are to get a response back (yes/no) so still being able to run things on the program without responding is a catastrophe waiting to happen.  I rapidly changed it back to AfxMessageBox which forces the user to respond before they could continue and found out about how to change the title of the AfxMessageBox.

To summarise the MessageBox can behave very differently than AfxMessageBox (and in a dangerous way), use AfxMessageBox at all costs.
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10690454
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!
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10690502
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 ?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10690822
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.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10694425
Right I increased the points to 80 for an even split, as everyone gave relevant information.

Thanks to all for this !
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
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.
Integration Management Part 2
Suggested Courses

927 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