2013-07-14 39 views
2

你好朋友我怎樣才能將類型「int」轉換爲類型「LPCSTR」?我想將變量「int cxClient」賦予「MessageBox」函數的第二個參數「LPCSTR lpText」。以下是示例代碼:如何將類型「int」轉換爲在Win32中輸入「LPCSTR」C++

int cxClient;  
cxClient = LOWORD (lParam);  
MessageBox(hwnd, cxClient, "Testing", MB_OK); 

但它不起作用。下面的功能是「消息框」功能的方法簽名:

MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType); 

回答

6

通過使用正確的sprintf變種

TCHAR buf[100]; 
_stprintf(buf, _T("%d"), cxClient); 
MessageBox(hwnd, buf, "Testing", MB_OK); 

需要<tchar.h>轉換的int值的字符串。

我覺得_stprintf是快速的答案在這裏 - 但如果你想要去的純C++像大衛建議,然後

#ifdef _UNICODE 
wostringstream oss; 
#else 
ostringstream oss; 
#endif 

oss<<cxClient; 

MessageBox(0, oss.str().c_str(), "Testing", MB_OK); 

你需要

#include <sstream> 
using namespace std; 
+0

由於該問題被標記爲C++,也許C++的答案會更好 –

+0

@DavidHe ffernan - 用ostringstream也添加一個。 – user93353

+0

哇!我知道了。現在「MessageBox」函數可以顯示我想要的變量:D。以下是我的工作代碼: #include using namespace std; #ifdef _UNICODE wostringstream oss; #else ostringstream oss; #endif oss << cxClient; MessageBox(hwnd,oss.str()。c_str(),「Testing」,MB_OK); 非常感謝你 :D –