0

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;

}
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
  • to fix the formatting you should select your code and click the `{}` button in the editor toolbar, this will indent all the code by 4 spaces which stackoverflow then formats as code. – Chris Taylor Dec 23 '10 at 14:53
  • couple comments about your code: 1) you should not `DeleteObject` a GDI object that is selected in a device context. Common practice is to save the old object handle, then select it back in before you `DeleteObject`. 2) `LoadLibrary` returns a ref-counted handle. You should free that handle when appropriate. 3) you won't need to use `UpdateWindow`. GDI and winapi painting take a lot of getting used-to, so I think everyone goes through questions like this at some point. Be meticulous in reading MSDN docs for the API, and don't skim over confusing parts; try to understand them. – tenfour Dec 23 '10 at 15:00

1 Answers1

1

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.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • I've changed a little in the code; when the menu item is clicked, a boolean trigger is set to true, and then I send a message with WM_PAINT and do the paintEditSigns(), yet it's still not doing anything! – KaiserJohaan Dec 23 '10 at 16:03
  • ... you cannot directly send WM_PAINT. as i said - please be meticulous in reading MSDN. And as I also said, use `InvalidateRect` to initiate a WM_PAINT. – tenfour Dec 23 '10 at 16:20
  • Yes the InvalidateRect did the trick, I'd have thought it would be called as a sideeffect from UpdateWindow though >. – KaiserJohaan Dec 23 '10 at 16:53