简体   繁体   English

如何在Win32 C ++中将类型“int”转换为“LPCSTR”类型

[英]How to convert type “int” to type “LPCSTR” in Win32 C++

Hello friends how can I convert type "int" to type "LPCSTR"? 朋友您好,如何将类型“int”转换为“LPCSTR”类型? I want to give the variable "int cxClient" to the "MessageBox" function's second parameter "LPCSTR lpText". 我想将变量“int cxClient”赋予“MessageBox”函数的第二个参数“LPCSTR lpText”。 Following is the sample code: 以下是示例代码:

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

But it does not work. 但它不起作用。 The following function is the method signature of the "MessageBox" function: 以下函数是“MessageBox”函数的方法签名:

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

Convert the int to a string by using the right sprintf variant 使用正确的sprintf变体将int转换为字符串

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

you need <tchar.h> . 你需要<tchar.h>

I think _stprintf is the quick answer here - but if you want to go pure C++ like David suggests, then 我认为_stprintf是这里的快速答案 - 但如果你想像大卫建议的那样去纯C ++,那么

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

oss<<cxClient;

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

You need 你需要

#include <sstream>
using namespace std;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM