Question

I ayant récemment mon mainwindow texte d'écriture en utilisant WM_PAINT, mais maintenant je me rends compte qu'il était peut-être pas le meilleur message à le faire, donc je suis en train une autre version;

Le mainwindow contient un menu, sur clicing un élément de menu msg ID_FILE_PID est envoyé et il construit les 4 nouvelles fenêtres aswell comme affiche le texte dans la fenêtre principale (fonction paintEditSigns). Les 4 fenêtres fonctionne très bien, mais le texte de travail dosn't du tout, à moins que je le fais dans la fonction principale () comme le montre ... qu'est-ce que cela? O_O

BTW: Je n'ai encore aucune idée pourquoi le code d'affichage sur StackOverflow continue à regarder si quand je poste wierd, pourquoi est-ce

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;

}
Était-ce utile?

La solution

Vous pouvez uniquement utilisation BeginPaint / EndPaint en réponse à WM_PAINT. Et WM_PAINT est l'endroit approprié pour faire le dessin comme celui-ci.

Windows appelle WM_PAINT lorsqu'une partie de la fenêtre est « invalidée ». Par exemple, si vous restaurez la fenêtre, ou une partie de la fenêtre devient visible après le déplacement d'une fenêtre de la route, ou le redimensionnement de la fenêtre.

Lorsque vous devez mettre à jour manuellement l'affichage, « Invalider » la zone dont vous avez besoin de redessiner en appelant InvalidateRect (cela indique Windows, la zone de la fenêtre qui doit être redessinée).

Common est juste invalider toute la fenêtre au lieu de calculer la limite pixel réelle parfaite de la zone que vous voulez dessiner.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top