• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

std::string in GDI+

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
Ok how do i get a std::string type variable into the string buffer for display in GDI+?

for example:

Code:
int nav_mode = 0;
std::string nav_mode_txt = "GPS";

if(nav_mode == 1)
   nav_mode_txt = "VOR";

Now how would i get that nav_mode_txt variable into a WCHAR buffer? So far have tried several things and have had no luck....
 
Let me just add that the string buffer i have to work with is a WCHAR type. normally my text is formatted in this function:

Code:
StringCbPrintf(mpszDest, mcbDest, L"%s", nav_mode_txt);

note though that when i try the above all i get is (null) displayed in the GDI+ text.
 
It's probably easier if you declare your std::string as a std::wstring instead, and use the L constant to specify a wide string when using wide constants.

Something like this:

Code:
using namespace std;

wchar_t wc[1024];
wstring nav_mode_txt;
int nav_mode = 0;

nav_mode_txt = nav_mode == 1 ? L"GPS" : L"VOR";
swprintf( wc, sizeof wc, nav_mode_txt.c_str() );

the wc buffer, being of the wchar_t type, can be passed to the GDI+ text functions directly.
 
Could you use c_str() ?

That's exactly what I've suggested to use in my example.

c_str() is a member function of the string (and wstring ) classes, which returns a buffer of char (if the source is a string) or a buffer of wchar_t (if the source is a wstring) starting from the C++ object.

Or, you mean using c_str() directly ? Yes, if the string type is wstring, you could just pass the .c_str() to GDI+, but usually one would need to add some formatting first, so the swprintf is usually needed anyway.

But yes, the code would also work like this:

Code:
using namespace std;

wstring nav_mode_txt;
int nav_mode = 0;

nav_mode_txt = nav_mode == 1 ? L"GPS" : L"VOR";

graphic.DrawString(nav_mode_txt.c_str(), -1, &font, pointF, &brush);  // GDI+ call

Assuming we already defined the font, point and brush, of course...
 
Last edited:
Back
Top