Heather
Heather

Reputation: 325

Bitmap disappears when window is resized

I have a simple GUI that is supposed to display an image when I am on a certain tab. I have the WM_PAINT message inside the tab process as shown below

case WM_PAINT:
    {
            PAINTSTRUCT psLOGO;
            RECT rcLOGO;
            HDC hdcLOGO;
            //Prepares for painting window
            hdcLOGO = BeginPaint(hwndMonitor, &psLOGO);
            //Retrieves the coordinates of the windows client area
            GetClientRect(hwndMonitor, &rcLOGO);
            //creates a copy of the memory device context 
            HDC hdcDoubleLOGO = CreateCompatibleDC(hdcLOGO);
            HBITMAP Logo = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));         //Get a bitmap of the picture to be updated
            HBITMAP bmOldLOGO = (HBITMAP)SelectObject(hdcDoubleLOGO, Logo);                                 //Get a handle to the image being replaced
            BitBlt(hdcLOGO, 0, 0, rcLOGO.right, rcLOGO.bottom, hdcDoubleLOGO, 0, 0, SRCCOPY);   //Bit block transfer of the bitmap color data

            SelectObject(hdcDoubleLOGO, bmOldLOGO);
            DeleteDC(hdcDoubleLOGO);
            EndPaint(hwndMonitor, &psLOGO);
            DeleteObject(Logo);
        break;
    }

hwndMonitor is the handle for that particular tab page

The image shows when I open the tab, but if I resize the window, or if I minimize and reopen the GUI, the image will disappear

I have to go to another tab and back to that tab to get the image back

Am I doing something wrong in my WM_PAINT message?

Upvotes: 1

Views: 710

Answers (1)

dwo
dwo

Reputation: 3636

You have to react to the WM_SIZE message also. Resizing a window does not release a paint message.

On WM_SIZE just call:

InvalidateRect(hwnd,&rect,TRUE);
UpdateWindow(hwnd);

rect is a rectangle with the current window size. Invalidate marks the rectangle for a repaint, and UpdateWindow secures it is repainted immediately.

Upvotes: 1

Related Questions