3
votes

I am trying to drwa some text using GDI on c++

It happens that I have a class that has a method to return the status and I want to draw it

The status is a std::string!

So here is what I have so far:

        RECT status = RECT();
        status.left = rcClient.right-200;
        status.left = rcClient.left;
        status.top = rcClient.top;
        status.bottom = rcClient.bottom;

        DrawTextW(hdc, game->GetStatus().c_str(), 1, status, 0);

The errors I have are:

error C2664: 'FormatMessageW' : cannot convert parameter 5 from 'LPWSTR' to 'char *'687 damas
error C2664: 'FormatMessageW' : cannot convert parameter 5 from 'wchar_t *' to 'char *'damas
error C2664: 'DrawTextW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'  

I can't find a way to solve this... can someone help me out?

1
It looks like you are using different width strings for the text you want to print from the currently selected Windows API. Either switch to using narrow strings (probably not the best solution) or make the game status a wide string. - Will
Use DrawTextA() and FormatMessageA() as the W variants take wide strings, but std::string is a char*. - hmjd
I tried another workaround using textout... but it prints some weird stuff :( - Killercode
@Killercode It's not wierd stuff, you just have the wrong type of string. As the previous comments said, either change to wstring or change to DrawTextA. - john
The project settings are probably saying unicode, which chooses wide strings and the DrawTextW versions of functions. You might need to change the settings. - Peter Wood

1 Answers

3
votes

A std::string uses chars, but DrawTextW is expecting wide chars (WCHARs, which are identical to wchar_ts).

You can call DrawTextA explicitly with your string. It will make a copy of your string using wide characters and pass it on to DrawTextW.

DrawTextA(hdc, game->GetStatus().c_str(), 1, &status, 0);

(Also note that it takes a pointer to the RECT, so you need the & as well.)