2013-04-18 80 views
3

我想drwa在C++我不能使用DrawText的()

碰巧我有了返回狀態的方法的類使用GDI一些文字,我想畫它

狀態是一個std :: string!

所以這是我到目前爲止有:

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

的錯誤,我已經是:

 
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' 

我無法找到一個方法來解決這個......誰能幫我出來嗎?

+2

看起來你正在使用不同寬度的字符串要從當前打印文本選定的Windows API。要麼切換到使用窄字符串(可能不是最好的解決方案)或使遊戲狀態變成一個寬字符串。 – Will

+2

使用'DrawTextA()'和'FormatMessageA()'作爲'W'變體寬字符串,但'std :: string'是'char *'。 – hmjd

+0

我嘗試了另一種解決方法,使用textout ...但它打印一些奇怪的東西:( – Killercode

回答

3

std::string使用字符,但DrawTextW期待寬字符(WCHAR s,這是相同的wchar_t S)。

你可以用你的字符串顯式地調用DrawTextA。它將使用寬字符複製字符串並將其傳遞到DrawTextW

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

(另請注意,它需要一個指向RECT,所以你需要&爲好。)