C++ 标识符 "Environment" 未定义

C++ Identifier "Environment" is undefined

根据 this post,我尝试在 MessageBox 中包含一个新行,如下所示:

std::wstring msg = "Text here" + Environment.NewLine + "some other text";

MessageBox(nullptr, msg.c_str(), L"Hello World!", MB_ICONINFORMATION);

但是编译器产生了错误:

E0020: identifier "Environment" is undefined

我尝试包括 <windows.system.h>,但它什么也没做。

项目类型:C++ ATL

提前致谢。

您不能在本机 C++ 应用程序中包含 Environment.NewLine,因为它是 .NET 构造。对于标准 C++ 中的换行符,使用 std::endl'\n' 字符:

#include <iostream>
int main(){
    std::cout << "Hello World." << std::endl;
    std::cout << "This prints\n text on two lines.";
}

对于 MessageBox WinAPI 函数中的换行符,使用 \r\n 个字符。您的代码应该是:

#include <Windows.h>
#include <string>
int main(){
    std::wstring msg = L"Text here \r\n some other text";
    MessageBox(NULL, msg.c_str(), L"Hello World!", MB_ICONINFORMATION);
}

Environment.NewLine 在 C# 中使用,因为在 C++ 中 post 是 Environment::NewLine

换行可以使用"\n"