Entire Code snippet...
#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);
}
The problem is I am receiving an error from visual studio 2012 telling me that argument of type "const char*" is incompatible with parameter of type LPCWSTR. This occurs on this line of code:
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;
I attempted a conversion ((LPCWSTR)gTextObjs[i].s.c_str()
), but apparently this is VERY wrong based on research due to one byte per char array operation to two? Also received warning of "C4018: '<' : signed/unsigned mismatch"
after this change. I am new to c++ and I'm pretty lost on this error considering the conversion is wrong.
I have reviewed some different threads in SO and nothing seems to tailor to this specific case (or I just don't quite understand the correlation between those threads and mine). :[
Also, just to clarify... the conversion "works", BUT it prints some weird Japanese/Chinese symbol looking writing and it's always different.
TextOut is resolving to TextOutW. That expects UTF-16 text. You pass 8 bit text. Either switch to UTF-16 held in wstring, or call TextOutA.