问题
I'm trying to print a text in the WM_COMMAND
case, because I need a text to be printed after a button was pushed.
Here's the code I have:
switch(msg)
{
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 1:
PAINTSTRUCT ps;
HDC hDC;
hDC = BeginPaint(hwnd, &ps);
{
TextOut(hDC, 10, 50, "hello", 5);
}
EndPaint(hwnd, &ps);
UpdateWindow(hwnd);
break;
}
break;
}
Sadly it doesn't print anything.
edit:
I can use TextOut()
during WM_COMMAND
that way:
HDC hDC;
hDC = GetDC(hwnd);
TextOut(hDC, 10, ypos, "Warnings: ", 10);
UpdateWindow(hwnd);
回答1:
It's best to structure your program so that all painting is performed in WM_PAINT.
so you could change it to be something like:
LRESULT CALLBACK WndProc(/*blah blah blah*/)
{
static wchar_t my_text[] = L"hello";
static BOOL show_btn_text = FALSE;
HDC dc;
PAINTSTRUCT ps;
switch (msg) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case 1:
show_btn_text = !show_btn_text;
InvalidateRect(hwnd, NULL, TRUE); //tells windows that the whole client area needs to be repainted
break;
}
return 0;
case WM_PAINT:
dc = BeginPaint(hwnd, &ps);
if (show_btn_text) {
TextOut(dc, 0, 0, my_text, wcslen(my_text));
}
EndPaint(hwnd, &ps);
return 0;
/*the rest of the window procedure
}
}
回答2:
- For painting inside of WM_PAINT:
BeginPaint
/EndPaint
. - For painting outside of WM_PAINT:
GetDC
/ReleaseDC
.
来源:https://stackoverflow.com/questions/5631937/use-textout-in-wm-command