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

时间:2011-04-24 17:15:40

标签: 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);

有人可以帮我解释语法。

感谢。

1 个答案:

答案 0 :(得分:9)

您的问题是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);
相关问题