win32: TextOut non essere visualizzati
Domanda
Di recente ho visto il mio MainWindow testo scrittura utilizzando WM_PAINT, ma ora mi rendo conto che non era forse il miglior messaggio di farlo in, quindi sto cercando un'altra versione;
Il MainWindow contiene un menu, su clicing una voce di menu il msg ID_FILE_PID viene inviato e costruisce le 4 nuove finestre proprio come viene visualizzato il testo nella MainWindow (funzione paintEditSigns). Le 4 finestre funziona bene ma il lavoro dosn't di testo a tutti, a meno che non lo faccio nella funzione main (), come mostrato ... che diavolo è questo? O_O
A proposito:? Ho ancora alcun indizio perché il codice-display sul StackOverflow continua a guardare in modo strano quando ho posto, perché è questo
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;
}
Soluzione
Puoi solo uso BeginPaint
/ EndPaint
in risposta a WM_PAINT
. E WM_PAINT
è il luogo adatto per fare il disegno come questo.
Windows chiama WM_PAINT
quando una parte della finestra è "invalidato". Per esempio, se si ripristina la finestra, o una parte della finestra diventa visibile dopo lo spostamento di una finestra di mezzo, o il ridimensionamento della finestra.
Quando è necessario aggiornare manualmente la visualizzazione, "invalidate" l'area è necessario ridisegnare chiamando InvalidateRect
(questo dice a Windows l'area della finestra che deve essere ridisegnato).
Comune è quello di invalidare solo l'intera finestra invece di calcolare il limite effettivo di pixel-perfetta della zona che si desidera disegnare.