如何将 int 转换为 LPCTSTR?赢32

Nic*_*ick 4 c++ winapi

我想在 win32 MessageBox 中显示一个 int 值。我已经阅读了一些不同的方法来执行这个转换。有人可以为我提供一个很好的实现。

Win32 编程的新手,所以很容易:)

更新

所以这就是我到目前为止所拥有的。它有效......但文本看起来像中文或其他一些双字节字符。我不是在探索 Unicode 与非 Unicode 类型。有人可以帮助我了解我哪里出错了吗?

 int volumeLevel = 6;
 std::stringstream os;
 os<<volumeLevel;
 std::string intString = os.str();  
  MessageBox(plugin.hwndParent,(LPCTSTR)intString.c_str(), L"", MB_OK);
Run Code Online (Sandbox Code Playgroud)

Mah*_*EFE 5

像 belov 一样转换为 MFC :

int number = 1;

CString t;

t.Format(_T("%d"), number);

AfxMessageBox(t);
Run Code Online (Sandbox Code Playgroud)

我用过,它对我有用。


In *_*ico 3

LPCTSTR定义如下:

#ifdef  UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif
Run Code Online (Sandbox Code Playgroud)

std::string::c_str()仅返回一个const char*。您不能将 aconst char*直接转换为const wchar_t*. 通常编译器会抱怨它,但是通过LPCTSTR强制转换,你最终会迫使编译器闭嘴。所以当然它在运行时不会像你期望的那样工作。根据您的问题,您可能想要的是这样的:

// See Felix Dombek's comment under OP's question.
#ifdef UNICODE
typedef std::wostringstream tstringstream;
#else
typedef std::ostringstream tstringstream;
#endif

int volumeLevel = 6;    
tstringstream stros;    
stros << volumeLevel;     
::MessageBox(plugin.hwndParent, stros.str().c_str(), L"", MB_OK);  
Run Code Online (Sandbox Code Playgroud)