winforms - MessageBox::Show 中需要 C++/CLI 帮助

标签 winforms c++-cli

我正在用 C++/CLI 构建一个项目,我必须在其中以我的一种形式显示消息框。

内容必须是 std::string 和 int 的组合。

但我无法获得正确的语法。

我尝试了以下方法:

std::string stringPart = "ABC";
int intPart = 10;
MessageBox::Show("Message" + stringPart + intPart);

我也试过:

String^ msg = String::Concat("Message", stringPart);
msg = String::Concat(msg, intPart);
MessageBox::Show(msg);

谁能帮我解决一下语法问题。

谢谢。

最佳答案

你的问题是 std::string 是非托管的,不能分配给托管的 System::String。解决方案是编码。请参阅此 MSDN 页面:http://msdn.microsoft.com/en-us/library/bb384865.aspx

这是解决方案(适用于 Visual Studio):

#include <msclr/marshal_cppstd.h>

// ...

std::string stringPart = "ABC";
int intPart = 10;

String^ msg = String::Concat("Message", msclr::interop::marshal_as<System::String^>(stringPart));
msg = String::Concat(msg, intPart);
MessageBox::Show(msg);

关于winforms - MessageBox::Show 中需要 C++/CLI 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5771989/

相关文章:

c# - 多个 FileSystemWatcher 来监视本地系统上的文件?

c# - 在运行时移动控制

c# - 尝试在 RichTextBox 中调整图像大小时光标闪烁

c# - 更新 ToolStripProgressBar 和 ToolStripStatusLabel 以及一个 Action

winforms - 从进程重定向 StandardOutput

c# - 如何通过单击按钮增加 datagridview 中的项目数?

c# - 将数组从非托管 C++ 传递到 C#

c# - 使用 CLR .Net Core 库(C++/CLI) 的 Web .Net Core 3.1 项目中的 BadImageFormat

c# - 面向 .NET Framework 4.0

c++ - Silverlight 和 C++ 或 C++ 到 C# 正在进行的代码转换?