Sign in to follow this  

MessageBox() Not working in vc++

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all! I have just decided to make the leap from Dev-c++ to Visual C++ :) I have decided to start with some simple examples to get comfortable with the ide but I ran into a problem right away. In this example I just want a popup message box. Here is the code.
#include <windows.h>

int WINAPI WinMain(HINSTANCE instance, HINSTANCE previnstance, PSTR cmdline, int show)
{
	MessageBox(NULL, "Message Box Test!", "Hello World!", MB_OK);

	//Return to windows with no errors
	return (0);
}
I try to run it and it barks at me for the two text parameters. It says they have to of type LPCWSTR. I then changed it to TEXT("Message Box Test!"), and TEXT("Hello World!"). It now compiles fine now but I have another problem. The background color of the message box is white while the button that says ok is gray. How do I change the default color of this? Thank you!

Share this post


Link to post
Share on other sites
It should take on the standard system colors. The only way I know of to change it is by monkeying around under Display Properties->Appearance->Advanced. When you get a message box in other applications, does it have the same appearance?

Share this post


Link to post
Share on other sites
Quote:
Original post by Funkyjive
I try to run it and it barks at me for the two text parameters. It says they have to of type LPCWSTR. I then changed it to TEXT("Message Box Test!"), and TEXT("Hello World!"). It now compiles fine now but I have another problem. The background color of the message box is white while the button that says ok is gray. How do I change the default color of this?
Any chance of a screenshot? It sounds like this is a Windows appearance setting (Right click on the desktop, go to Properties, then the Appearance tab and look at options there).
If you want to change it from the system default, you'll have to create the dialog box yourself (Which is a lot more complicated).

And about the TEXT() thing, you can change your application's character set from Unicode to Multi Byte Character Set (Project -> Properties (Alt+F7) -> Configuration Properties -> General -> Character Set). That'll do away with the need for the TEXT() macros and LPCWSTR errors, which is probably a good thing if you're just starting with Windows code.

EDIT: Too slow [smile]

Share this post


Link to post
Share on other sites
Thank you for the reply. I went and changed the character set and the TEXT() problem is taken care of. As for the white background here is a small pic of the problem...

White Background Problem

I tested the program under Dev-c++ and the same thing so you must be right on about the settings of windows as opposed to the programming. Im running under Vista : ( so Ill play around to figure it out. I guess I never noticed it before.

Thanks again!

Share this post


Link to post
Share on other sites
The easy fix for this, based on the information presented in the OP is to add an L to the beginning of the string, but outside the quotes, as in:


MessageBox(NULL, L"MessageBox Test", L"Test", MB_OK);


This way you don't need to change any project settings, display settings or use the TEXT() macro. Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by u235
The easy fix for this, based on the information presented in the OP is to add an L to the beginning of the string, but outside the quotes, as in:


MessageBox(NULL, L"MessageBox Test", L"Test", MB_OK);


This way you don't need to change any project settings, display settings or use the TEXT() macro. Hope that helps.
True, although if the OP is just starting with Win32 programming, it's simpler to use MBCS. If they still use unicode, then they'll either have to convert to/from MBCS and unicode or they'll have to make sure that everything uses unicode (E.g. file IO). Personally, I think it's easier to use MBCS - at least till you get the hang of Win32, particularly since a lot of tutorials online assume MBCS.

Share this post


Link to post
Share on other sites
If you haven't already, try downloading the latest Platform SDK and the Vista SDK.

You might also find this information handy - the article describes how to enable Vista/XP visual themes for your programs. Technically this should work already if you're compiling with VS2005 or later on the latest PSDK (although that may not be the case for the Express Edition of VC++, I don't honestly know).

Share this post


Link to post
Share on other sites
Quote:
Original post by Funkyjive
I try to run it and it barks at me for the two text parameters. It says they have to of type LPCWSTR. I then changed it to TEXT("Message Box Test!"), and TEXT("Hello World!")


This is because of the Character Set. If your character set is Unicode, then you must put a L before every string, if its is Multi-Byte you must not. LPCWSTR is a long pointer to a wide const string, a const wchat_t, which is a 2 byte long Unicode char. If you want to avoid using TEXT() or L before every string, go to the project properties, Configuration Properties, General, Character Set, and change to MultiByte. hth

Share this post


Link to post
Share on other sites

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this