gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:19:12 25 4
gpt4 key购买 nike

我正在用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com