Using MessageBox in C++

G

Guest

I want to Use MessageBox in a C++ .Net Windows Forms project.

I have a form with a button - clicking the button brings up some text in a MessageBox.

If I call MessageBox from the Click event handler in the .h header file - it works fine.
If I move the event handler to the .cpp file it fails with:

"MessageBoxA is not a class or namespace name"

Any suggestions gratefully received
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?RG91Z2xhcyBDb2xsaWU=?= said:
I want to Use MessageBox in a C++ .Net Windows Forms project.

I have a form with a button - clicking the button brings up some text in a MessageBox.

If I call MessageBox from the Click event handler in the .h header file - it works fine.
If I move the event handler to the .cpp file it fails with:

"MessageBoxA is not a class or namespace name"

Are you using the Win32 function 'MessageBox' or .NET's 'MessageBox' class?
 
G

Guest

I am using .Net MessageBo

I think the problem is with the preprocessor
It works ok if I derive a new class from MessageBox and use that, and I presume (but haven't tried yet) that I can #undef MessageBox

Is there a "proper" way to handle this

Thank
Douglas Collie
 
G

Gerhard Menzl

Douglas said:
I want to Use MessageBox in a C++ .Net Windows Forms project.

I have a form with a button - clicking the button brings up some text
in a MessageBox.

If I call MessageBox from the Click event handler in the .h header
file - it works fine. If I move the event handler to the .cpp file it
fails with:

"MessageBoxA is not a class or namespace name"

Any suggestions gratefully received

This is caused by the inclusion of <windows.h> with its infamous macros,
a legacy from the Dark C Ages. I have solved the problem by moving the
definition od WinMain (or _tWinMain), which requires <windows.h>, to a
source file of its own. This allows you to remove the offending #include
statement from the CPP file of your main form.

Why Visual Studio mixes code that doesn't belong together in the first
place is beyond me. I can imagine that thousands of people must have
stumbled across this silly problem.
 

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