Vol*_*rix 0 c++ wm-paint textout lpcwstr
整个代码片段......
#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);
}
Run Code Online (Sandbox Code Playgroud)
问题是我收到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;
Run Code Online (Sandbox Code Playgroud)
我尝试了转换((LPCWSTR)gTextObjs[i].s.c_str()),但显然基于研究这是非常错误的,因为每个字符串数组操作一个字节到两个?"C4018: '<' : signed/unsigned mismatch"此次更改后也收到警告.我是c ++的新手,考虑到转换是错误的,我很遗憾这个错误.
我已经回顾了SO中的一些不同的线程,似乎没有任何东西适合这个特定的情况(或者我只是不太了解这些线程和我之间的相关性).:[
此外,只是为了澄清...转换"有效",但它打印一些奇怪的日本/中国符号看起来写作,它总是不同的.