Question

I recently having my mainwindow write text by using WM_PAINT, but now I realise it was maybe not the best message to do so in, so I'm trying another version;

The mainwindow contains a menu, upon clicing a menu item the ID_FILE_PID msg is sent and it builds the 4 new windows aswell as displays text in the mainwindow (paintEditSigns function). The 4 windows works fine but the text dosn't work at all, unless I do it in the main() function as shown... what on earth is this? O_O

BTW: I still have no clue why the code-display on StackOverflow keeps looking so wierd when I post, why is this?

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;

}
Was it helpful?

Solution

You can only use BeginPaint/EndPaint in response to WM_PAINT. And WM_PAINT is the appropriate place to do drawing like this.

Windows calls WM_PAINT when a part of the window is "invalidated". For example if you restore the window, or part of the window becomes visible after moving a window out of the way, or resizing the window.

When you need to manually update the display, "invalidate" the area you need to redraw by calling InvalidateRect (this tells Windows the area of the window that needs to be redrawn).

Common is to just invalidate the whole window instead of calculating the actual pixel-perfect boundary of the area you want to draw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top