• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Paint fails

## Recommended Posts

When I need my window repainted I use InvaludateRect(hWnd,NULL,TRUE); when I paint my window I CreateCompatibleBitmap for double buffering - remembering to DeleteObject afterwards. When my application has repainted a couple of times the paint suddently fails without crashing the program. This is where I go "hmm..."

##### Share on other sites
how does it fail without crashing the program?

##### Share on other sites
It simply doesnt paint anything. I just get a standard window - total blank, but I can still drag it around and minimize etc.

##### Share on other sites
Anyone? This is a really nasty problem :/

##### Share on other sites
Why do you CreateCompatibleBitmap everytime you paint your window? I usually only create it on a WM_SIZE (don''t know if that''s good or bad, but it works for me).
Anyway show us the code and we might spot something, this is otherwise too much of a guesswork.

##### Share on other sites
Here is the code:
//From my WndProc        case WM_PAINT:        {            PAINTSTRUCT ps;            HDC dc=BeginPaint(hwnd,&ps);            curr->Render(dc);            EndPaint(hwnd,&ps);        }        break;//From the Render() function:void CPage::Render(HDC dc){    RECT rt;    GetClientRect(hWnd,&rt);    HDC hdc=CreateCompatibleDC(dc);    HBITMAP bmp=CreateCompatibleBitmap(dc,rt.right-rt.left,rt.bottom-rt.top);    SelectBitmap(hdc,bmp);    FillRect(hdc,&rt,(HBRUSH)COLOR_BACKGROUND);//- Here lots of other FillRect, LineTo etc. are done on ''hdc'' -    GetClientRect(hWnd,&rt);    BitBlt(dc,rt.left,rt.top,rt.right,rt.bottom,hdc,0,0,SRCCOPY);    DeleteObject(bmp);}

##### Share on other sites
1) you allocated a DC and a Bitmap but you only delete the Bitmap later on, so the DC gdi-mem-leaks.

2) you have not stored the original object away the first time you call SelectObject. you have to keep the object returned by the first SelectObject (cause it''s the default object) and set it back before you delete. otherwise the system refused to delete the Bitmap silently and it gdi-mem-leaks again.

so here the correct thing:

HBITMAP vOld = SelectObject(dc, newbmp);
...
SelectObject(dc, vOld);
DeleteObject(vOld);
DeleteDC(dc)

##### Share on other sites
Lol! Sure! Thanks
My brain is so fried from long hours that I just went into a mental deadlock on the problem
Note: The ''dc'' is the HDC gained from BeginPaint(); I shouldnt delete that, should I?

##### Share on other sites
quote:
Original post by emileej
Note: The ''dc'' is the HDC gained from BeginPaint(); I shouldnt delete that, should I?

He''s referring to the one you created with HDC hdc = CreateCompatibleDC(dc);

##### Share on other sites
Ye I figured, but thanks for the highlight - thats the stuff I need when running low on brain capacity

• ### Forum Statistics

• Total Topics
627734
• Total Posts
2978846

• 10
• 10
• 21
• 14
• 12