Archived

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

emileej

Paint fails

Recommended Posts

emileej    130
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
emileej    130
It simply doesnt paint anything. I just get a standard window - total blank, but I can still drag it around and minimize etc.

Share this post


Link to post
Share on other sites
amag    152
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
emileej    130
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
RPTD    340
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
emileej    130
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
porthios    130
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 this post


Link to post
Share on other sites