Win32: TextOut no se muestre
Pregunta
Hace poco tener mi MainWindow texto de escritura mediante el uso de WM_PAINT, pero ahora me doy cuenta de que tal vez no era el mejor mensaje para hacerlo, así que estoy tratando otra versión;
El MainWindow contiene un menú, al clicing un elemento de menú que se envía el MSG ID_FILE_PID y construye las 4 ventanas nuevas aswell como muestra el texto en el MainWindow (función paintEditSigns). Las 4 ventanas funciona bien, pero el texto de trabajo dosn't en absoluto, a menos que lo hago en la función main (), como se muestra ... ¿Qué diablos es esto? O_O
Por cierto:? Todavía no tengo ni idea de por qué la pantalla de código en StackOverflow sigue mirando de manera extraña cuando publico, ¿por qué es esto
switch(message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILE_PID:
{
HWND hWndButton;
HWND hWndEdit;
HWND hWndEdit2;
HWND hWndDisplay;
// drawing the text in mainwindow
trigger=true;
// adding new windows in the mainwindow
hWndButton = CreateWindowEx(0,TEXT("BUTTON"),"Modify",WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, 170,56,80,30,hWnd,(HMENU)ID_BUTTON,hThisInstance,NULL);
hWndEdit = CreateWindowEx(0,RICHEDIT_CLASS,TEXT(""),WS_CHILD | WS_VISIBLE | WS_BORDER,
120,30,80,25,hWnd,(HMENU)ID_EDIT,hThisInstance,NULL);
hWndEdit2 = CreateWindowEx(0,RICHEDIT_CLASS,TEXT(""),WS_CHILD | WS_VISIBLE | WS_BORDER,
220,30,80,25,hWnd,(HMENU)ID_EDIT2,hThisInstance,NULL);
hWndDisplay = CreateWindowEx(0,TEXT("STATIC"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,
0,100,450,140,hWnd,(HMENU)ID_DISPLAY,hThisInstance,NULL);
UpdateWindow(hWnd);
break;
}
.....
case WM_PAINT:
{
if (trigger) {
paintEditSigns()
}
break;
}
//
// Main function
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
hThisInstance = hInstance;
LoadLibrary("Riched20.dll");
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
if(!(wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MYICON)))) {
HRESULT res = GetLastError();
}
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = TEXT("testcpp");
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
wc.lpszClassName,
TEXT("test"),
WS_OVERLAPPEDWINDOW,
300,
200,
450,
300,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd,nCmdShow);
//paintEditSigns() -- here it works, but not when in the message part
MSG msg;
while (GetMessage(&msg, NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void paintEditSigns() {
HFONT hf = createFont();
PAINTSTRUCT ps;
HWND hWnd = FindWindow(TEXT("testcpp"),TEXT("test"));
HBRUSH hbruzh = CreateSolidBrush(RGB(0,0,0));
HDC hdz = BeginPaint(hWnd,&ps);
string s = "Memory Address";
SelectBrush(hdz,hbruzh);
SelectFont(hdz,hf);
TextOut(hdz,0,100,s.c_str(),s.length());
EndPaint(hWnd,&ps);
DeleteObject(hbruzh);
}
HFONT createFont() {
HDC hdc;
long lfHeight;
hdc = GetDC(NULL);
lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
HFONT hf = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "MS Sans Serif");
return hf;
}
Solución
Puede solamente utilización BeginPaint
/ EndPaint
en respuesta a WM_PAINT
. Y WM_PAINT
es el lugar apropiado para hacer el dibujo como este.
Windows llama WM_PAINT
cuando una parte de la ventana está "invalidado". Por ejemplo si se restaura la ventana, o una parte de la ventana se vuelve visible después de mover una ventana fuera del camino, o cambiar el tamaño de la ventana.
Cuando tenga que actualizar manualmente la pantalla, "invalida" el área que usted necesita para volver a trazar llamando InvalidateRect
(esto indica a Windows el área de la ventana que necesita ser redibujado).
Común es simplemente invalidar toda la ventana en lugar de calcular los límites reales de píxel perfecto de la zona que desea dibujar.