CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: GDI - Repaint

  1. #1
    Join Date
    Oct 2005
    Posts
    13

    GDI - Repaint

    Hi everyone,
    While I was using GDI, i noticed that when the window is minimized, resized or has a child window that covers it, the drawing disappears. Is there a way to solve this problam, redraw the painting?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI - Repaint

    Windows will send a WM_PAINT message to a window when it needs to repaint itself. In Windows (prior to Windows Vista), the screen is represented by 1 big pixel buffer. When you move a window on top of another window, the pixels of the background window are lost. When you make the background window visible again, Windows will send a WM_PAINT message to the window so it can redraw itself.

    That is the reason why all drawing should be done in your WM_PAINT handler and not somewhere else.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2005
    Posts
    13

    Re: GDI - Repaint

    Quote Originally Posted by Marc G
    Windows will send a WM_PAINT message to a window when it needs to repaint itself. In Windows (prior to Windows Vista), the screen is represented by 1 big pixel buffer. When you move a window on top of another window, the pixels of the background window are lost. When you make the background window visible again, Windows will send a WM_PAINT message to the window so it can redraw itself.

    That is the reason why all drawing should be done in your WM_PAINT handler and not somewhere else.
    It would have been fine, but in my program I have buttons with a dynamic text. How do I trace if they're getting a WM_PAINT message?

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI - Repaint

    Are you drawing anything yourself? If so, where do you do your drawing? Can you post some code?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Oct 2005
    Posts
    13

    Re: GDI - Repaint

    Sure.

    I Put this code, to draw all my buttons (I'm making a sudoku solving program) in the WM_CREATE of the main window:
    Code:
    for (count = 1 ; count <= 81 ; count++)
    {
    button[count].hButton = CreateWindowEx(WS_EX_CLIENTEDGE
    , "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_OWNERDRAW,
    (i * 55 - 55), (j * 55 - 55), //xy position, not very importent
     52, 52, ProcWnd, (HMENU)(500 + count), GetModuleHandle(NULL), NULL);
    
    //assign HDC
    button[count].hdc = GetDC(sudoku.button[count].hButton);
    }

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI - Repaint

    Why are you storing all those DC's?
    What do you do with them?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Oct 2005
    Posts
    13

    Re: GDI - Repaint

    Quote Originally Posted by Marc G
    Why are you storing all those DC's?
    What do you do with them?
    I send them to a function that draws them every change occures, It's like would should be in the WM_PAINT message, but since I can't track it, the drawing funciton is called every time a change is made in the button's information (According to the program's calculations, I don't think it's relevent to go into it).

  8. #8
    Join Date
    Oct 2005
    Posts
    13

    Re: GDI - Repaint

    I tried to insert this code in the main message loop:
    Code:
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
    		MainLoop(); //do the main loop of the program		
    
            TranslateMessage(&Msg); //translate the message so that WndProc can understand
    /////////////////Supposed to take care of the button drawing issue
    		for (i = 1 ; i <= 81 ; i++)
    			if ( (Msg.hwnd == button[i].hButton) && (Msg.message == WM_PAINT))
    				drawButton(button[i].hdc, i);
    /////////////////
            DispatchMessage(&Msg); //send the message to WndProc
        }
    But it still doesn't draw it. Perhaps it just doesn't really send the WM_PAINT message to the buttons.

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI - Repaint

    You are not reading my replies.

    All drawing in Windows should happen in response to WM_PAINT.

    So, if you want to redraw a button, you simply call InvalidateRect which will indirectly call your WM_PAINT handler.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  10. #10
    Join Date
    Oct 2005
    Posts
    13

    Re: GDI - Repaint

    I finally Got it! Thank you very much for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured