整个代码片段...
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
//=========================================================
// Globals.
HWND ghMainWnd = 0;
HINSTANCE ghAppInst = 0;
struct TextObj
{
string s; // The string object.
POINT p; // The position of the string, relative to the
// upper-left corner of the client rectangle of
// the window.
};
vector<TextObj> gTextObjs;
// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Objects for painting.
HDC hdc = 0;
PAINTSTRUCT ps;
TextObj to;
switch( msg )
{
// Handle left mouse button click message.
case WM_LBUTTONDOWN:
{
to.s = "Hello, World.";
// Point that was clicked is stored in the lParam.
to.p.x = LOWORD(lParam);
to.p.y = HIWORD(lParam);
// Add to our global list of text objects.
gTextObjs.push_back( to );
InvalidateRect(hWnd, 0, false);
return 0;
}
// Handle paint message.
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
for(int i = 0; i < gTextObjs.size(); ++i)
TextOut(hdc,
gTextObjs[i].p.x,
gTextObjs[i].p.y,
gTextObjs[i].s.c_str(),
gTextObjs[i].s.size());
EndPaint(hWnd, &ps);
return 0;
}
// Handle key down message.
case WM_KEYDOWN:
{
if( wParam == VK_ESCAPE )
DestroyWindow(ghMainWnd);
return 0;
// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
}
// Forward any other messages we didn't handle to the
// default window procedure.
return DefWindowProc(hWnd, msg, wParam, lParam);
}
问题是我收到来自 Visual Studio 2012 的错误,告诉我“const char *”类型的参数与 LPCWSTR 类型的参数不兼容。这发生在这行代码上:
hdc = BeginPaint(hWnd, &ps);
for(int i = 0; i < gTextObjs.size(); ++i)
TextOut(hdc,
gTextObjs[i].p.x,
gTextObjs[i].p.y,gTextObjs[i].s.c_str(), // <---happens here
gTextObjs[i].s.size());
EndPaint(hWnd, &ps);
return 0;
我尝试了转换((LPCWSTR)gTextObjs[i].s.c_str()
),但显然这是非常错误的研究,因为每个字符数组操作一个字节到两个?"C4018: '<' : signed/unsigned mismatch"
在此更改后还收到警告。我是 C++ 新手,考虑到转换错误,我对这个错误非常迷茫。
我已经查看了 SO 中的一些不同线程,但似乎没有任何东西适合这种特定情况(或者我只是不太了解这些线程与我的线程之间的相关性)。:[
另外,只是为了澄清......转换“有效”,但它会打印一些看起来很奇怪的日文/中文符号,而且总是不同的。