我正在編寫一個Win32程序,並且我想將X pos和Y pos文本輸出到屏幕,並且我想知道如何將SHORT轉換爲TCHAR。請不要使用atoi或itoa功能 。將SHORT轉換爲TCHAR?
這是一個玩具程序,我想給出鼠標的位置文本,但是我不知道如何將short轉換爲TCHAR。
我正在編寫一個Win32程序,並且我想將X pos和Y pos文本輸出到屏幕,並且我想知道如何將SHORT轉換爲TCHAR。請不要使用atoi或itoa功能 。將SHORT轉換爲TCHAR?
這是一個玩具程序,我想給出鼠標的位置文本,但是我不知道如何將short轉換爲TCHAR。
也許你想一個unsigned int類型轉換爲字符串。您可以使用std :: to_wstring如果TCHAR被定義爲WCHAR:
short x = 123;
std::wstring s = std::to_wstring(x);
然後轉換s.c_str()
爲TCHAR*
。
SHORT myVal;
TCHAR buf[32]; // 32 is big enough to contain a 16-bit int as a string
_stprintf_s(buf,ARRAYSIZE(buf),"%hd",myVal);
// now buf contains the text as a TCHAR
你可以使用stringstream。
#include <sstream>
std::stringstream ss("");
ss << nX << " " << nY;
TextOut(GetDC(hWnd), 100, 100, reinterpret_cast<TCHAR *>(ss.str().c_str()), ss.str().size());
使用['的std :: to_string'](http://en.cppreference.com/w/cpp/string/basic_string/to_string)或其寬字符兄弟,不使用'TCHAR'類型。 「TCHAR」和朋友是支持Windows 9x的微軟技術。你的目標是Windows 9x嗎? –
當你說「TCHAR」時,你的意思是「單個TCHAR(它是char *還是wchar_t'的別名,取決於你如何編譯你的代碼),或者你的意思是」一個TCHAR數組「這又是一個」char「或」wchar_t「的數組,換句話說,就是一個C風格的字符串。 – jalf
嗯,你在泄漏DC,而且你正在繪製循環之外,這將會導致重繪腐敗 –