Archived

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

Paint fails

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites