我正在尝试使文本显示在窗口内。出于某些原因,当我在const char*
或string
中使用DrawText()
或TextOut()
作为参数时,它不会显示英文文本。我能够以英语输出文本的唯一方法是使用TEXT("This is my testing text")
作为DrawText()
的第二个参数。
我注释掉了引起问题的3条线,但它们是:
//DrawText(hdc, (LPCWSTR)s.c_str(), -1, &rect, DT_NOCLIP);
//TextOut(hdc, 200, 200, (LPWSTR)myCharArray, strlen(myCharArray));
//TextOut(hdc, 200, 400, (LPWSTR)s.c_str(), s.length());
这是我通过下面链接的代码所得到的:
以下是我对上面3条粗线的评论:
我想知道为什么会这样。
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Variable Sized Arrays"), WS_VISIBLE | WS_CHILD, 10, 10, 200, 25, hWnd, (HMENU) 1, NULL, NULL );
break;
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rect;
HFONT hfont;
HDC hdc = BeginPaint(hWnd, &ps);
const char* myCharArray = "This is a test.";
string s = "This is also a test";
hfont = CreateFont(36, 0, 0, 0, FW_DONTCARE, FALSE, TRUE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Times New Roman"));
SelectObject(hdc, hfont);
SetRect(&rect, 100, 200, 900, 800);
SetTextColor(hdc, RGB(0, 128, 0));
DrawText(hdc, TEXT("This is a testerino peppercino"), -1, &rect, DT_NOCLIP);
//DrawText(hdc, (LPCWSTR)s.c_str(), -1, &rect, DT_NOCLIP);
//TextOut(hdc, 200, 200, (LPWSTR)myCharArray, strlen(myCharArray));
//TextOut(hdc, 200, 400, (LPWSTR)s.c_str(), s.length());
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}