3

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?

hmjd
  • 120,187
  • 20
  • 207
  • 252
Killercode
  • 894
  • 1
  • 20
  • 34
  • 2
    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 Apr 18 '13 at 14:30
  • 2
    Use `DrawTextA()` and `FormatMessageA()` as the `W` variants take wide strings, but `std::string` is a `char*`. – hmjd Apr 18 '13 at 14:31
  • I tried another workaround using textout... but it prints some weird stuff :( – Killercode Apr 18 '13 at 14:44
  • 1
    @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 Apr 18 '13 at 15:13
  • The project settings are probably saying `unicode`, which chooses wide strings and the `DrawTextW` versions of functions. You might need to [change the settings](http://stackoverflow.com/q/1319461/1084416). – Peter Wood Apr 18 '13 at 15:36

1 Answers1

3

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.)

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175