visual-studio 如何显示 Win32 MessageBox?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1148538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:47:00  来源:igfitidea点击:

How do I show a Win32 MessageBox?

visual-studiowinapivisual-c++

提问by user133466

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

我正在尝试制作一个弹出消息框,上面写着“Hello World”。我从文件>新建项目>Visual C++>CLR>Windows 窗体应用程序开始,然后我从工具箱中拖出一个按钮到窗体上,双击它进入

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'

错误 C2440:“”:无法从“const char [12]”转换为“System::Windows::Forms::MessageBox”

回答by RichieHindle

You need:

你需要:

MessageBox::Show("Hello World");

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

(根据您在 Visual Studio 2005 中的说明进行测试。)

回答by Kim Gr?sman

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 中的 Windows 应用程序”——您已经创建了一个 C++/CLI 应用程序,这实际上并不是一回事。

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

C++/CLI 是 Microsoft 尝试创建更接近 .NET 运行时的 C++ 方言。

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

如果要构建 C 程序,请从 Visual C++ -> Win 32 项目开始。

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

在生成的代码中,在_tWinMain函数中,添加对原生MessageBox函数的调用:

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.

这应该让你开始。