user133466
user133466

Reputation: 3415

How do I show a Win32 MessageBox?

I'm trying to make a pop up message box with "Hello World" written on it. I started off with File>New Project>Visual C++>CLR>Windows Form Application Then I dragged a button from the toolbox onto the form, double clicked it entered

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox("Hello World");
}

then I compiled... but I got an error message saying

error C2440: '' : cannot convert from 'const char [12]' to 'System::Windows::Forms::MessageBox'

Upvotes: 2

Views: 14838

Answers (2)

Kim Gräsman
Kim Gräsman

Reputation: 7596

I'm not sure what your ultimate goals are, but the subject line mentioned a "Windows Application in C" -- you've created a C++/CLI application, which isn't really the same thing.

C++/CLI is Microsoft's attempt to create a C++ dialect closer to the .NET runtime.

If you want to build a C program, start with a Visual C++ -> Win 32 Project.

In the generated code, in the _tWinMain function, add a call to the native MessageBox function:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    MessageBox(NULL, _T("Hello world!"), _T("My program"), MB_OK);

// ...
}

That should get you started.

Upvotes: 5

RichieHindle
RichieHindle

Reputation: 281855

You need:

MessageBox::Show("Hello World");

(Tested according to your instructions in Visual Studio 2005.)

Upvotes: 10

Related Questions