0

According to this post, I tried to include a new line in a MessageBox like below:

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

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

But compiler generated the error:

E0020: identifier "Environment" is undefined

I tried including <windows.system.h>, but it did nothing.

Project type: C++ ATL

Thanks in Advance.

GTAVLover
  • 1,407
  • 3
  • 22
  • 41

2 Answers2

1

You can't include Environment.NewLine in your native C++ application because it is a .NET construct. For a newline character in standard C++ use std::endl or a '\n' character:

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

For a newline in the MessageBox WinAPI function use \r\n characters. Your code should be:

#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);
}
Ron
  • 14,674
  • 4
  • 34
  • 47
  • When I try to use `+ "\n" +` for a new line, compiler says: `E2140 - expression must have integral or unscoped enum type`. – GTAVLover Jul 18 '17 at 05:38
1

Environment.NewLine is used in C#, as in that post in C++ it is Environment::NewLine

For a new line you can use "\n"

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • But why compiler says `Environment` isn't a class or a namespace name? Isn't it available in C++? Or do I need to add a `using` directive like `using namespace Environment`? – GTAVLover Jul 18 '17 at 05:31
  • 1
    @GTAVLover you don't have the right project type, to use Environment, you will need a .net based project, like CLI/CLR – meJustAndrew Jul 18 '17 at 05:34
  • Thanks , then is `"\n"` the only way to use a newline in an ATL project? – GTAVLover Jul 18 '17 at 05:34
  • @GTAVLover there may be wrappers around this, but it is the most common way. – meJustAndrew Jul 18 '17 at 05:36
  • But can I do it like described [here](https://learn.microsoft.com/en-us/cpp/dotnet/how-to-compile-mfc-and-atl-code-by-using-clr)? – GTAVLover Jul 18 '17 at 05:37
  • 1
    @GTAVLover you can build indeed the ALT project on .net as a CLR project, but only for Environment.Newline, there will be no worth for the effort. – meJustAndrew Jul 18 '17 at 05:39
  • Thanks for important information about project types. – GTAVLover Jul 18 '17 at 05:41