开发者

Why the image disappears?

开发者 https://www.devze.com 2023-03-03 15:51 出处:网络
i have the following code ... case WM_PAINT: { hdc = BeginPaint(hwnd,&paintSt); temphdc = hdc; GetClientRect(h开发者_运维问答wnd,&aRect);

i have the following code ...

    case WM_PAINT:
    {
        hdc = BeginPaint(hwnd,&paintSt);
        temphdc = hdc;
        GetClientRect(h开发者_运维问答wnd,&aRect);
        if(hBitmap!=NULL)
        {               
            HDC memDC = CreateCompatibleDC(hdc);
            if(memDC!=NULL)
            {
                BITMAP bmp;
                GetObject(hBitmap,sizeof(bmp),&bmp);
                SelectObject(memDC,hBitmap);
                SetStretchBltMode(hdc,HALFTONE);
                StretchBlt(hdc,0,0,aRect.right,aRect.bottom,
                           memDC,0,0,bmp.bmWidth,bmp.bmHeight,
                           SRCCOPY);
                DeleteObject(&bmp);
                ReleaseDC(hwnd,memDC);
            }

        }           
        // the code for painting 
        EndPaint(hwnd,&paintSt);
    }
    break;

hBitmap is a global variable which is assigned at some place in the code.... Image is displayed but disappears whenever I minimize the window....

can anyone explain this ?

thanks in advance,


Your cleanup code is all wrong, you are leaking handles badly. Should be readily visible in TaskMgr.exe, Processes tab. View + Select Columns and tick GDI Objects. This code stops working when the GDI object handle count reaches 10,000. Yes, likely to happen when you resize the window since there will be a flurry of paint requests.

Don't delete the BITMAP, it is just as struct. Restore the old bitmap handle you got back from SelectObject() before you delete the memDC. Don't use ReleaseDC, DeleteDC is required. Pay attention to the return value of these functions, they tell you when you messed up but that can't work if you never check with an assert.

GDI programming is painful with these explicit cleanup rules. Consider a class library to take care of this kind of drudgery, they never get it wrong.


I guess somehow hBitmap is changing to null while minimize. Posting the code where you are assigning and referring hBitmap will help to identify the issue I think.

0

精彩评论

暂无评论...
验证码 换一张
取 消