Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double-buffering for flicker free animation 1

Status
Not open for further replies.

qednick

Programmer
Jul 26, 2002
516
0
0
US
Hi all,
I'm trying to put a simple program together without using MFC whatsoever. In other words, by using the box-standard WIndowsAPI calls. However, I've run into a stumbling block when it comes to double-buffering to avoid flicker on the window during drawing. Funny thing is I haven't had any problems at all doing this with MFC but when I use standard API calls in the equivalent everything goes bananas.
Here's the code I'm using:

[tt]
// get the HDC for this window
HDC hDC = ::GetWindowDC(m_hWnd);
RECT rc;

// get the window rectangle
::GetClientRect(m_hWnd,&rc);

// create compatible HDC & HBITMAP and
// select the bmp into the offscreen HDC

HDC gwDC = ::CreateCompatibleDC(hDC);
HBITMAP gwBMP = ::CreateCompatibleBitmap(hDC,rc.right-rc.left,
rc.bottom-rc.top);
HBITMAP oldBMP = (HBITMAP)::SelectObject(gwDC,gwBMP);

// draw something to the offscreen HDC
::InflateRect(&rc,-50,-50);
::Rectangle(gwDC,rc.left,rc.top,rc.right,rc.bottom);

// BitBlt the image from the offscreen HDC
// to the window's HDC

::BitBlt(hDC,0,0,rc.right-rc.left,
rc.bottom-rc.top,gwDC,0,0,SRCCOPY);

// clean up everything
::ReleaseDC(m_hWnd,hDC);
::SelectObject(gwDC,oldBMP);
::DeleteObject(gwBMP);
::DeleteDC(gwDC);[/tt]

It seems to me that I must be missing a step or doing something incorrect. Using the MFC equivalents this works great but when I use the above code in a simple dialog it draws to the top left of the title bar rather than the client area AND it tends to lock up also.

What's going on and how do I correct it?

[bugeyed]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
The problem is that you use GetWindowDC to obtain a handle to the device context. This will return the DC for the ENTIRE window (which allows you to draw outside the client area).

Use GetDC to obtain a handle to the client area device context.
Greetings,
Rick
 
Hi Lazy, thanks for that! I can't believe I missed that one - guess I've been using MFC too much

[hammer]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
OK, it's still not working correctly!! The drawing now occurs in the correct place but the whole thing just kind of hangs as if it's re-drawing to the window several dozen times per second!!
What is there in the code above that could possibly cause this to happen - perhaps I can't see the wood from the trees!
[cry]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Where is the code snipped you posted located in your code?
Is it a WM_PAINT handler?

Also; Why are you creating the DC and after use deleting it? Wouldn't it be better for performance to keep de mem dc alive as long as the application is alive?

Can you post the complete code that handles the WM_PAINT message of your window? I can't see why this snippet of code would cause your app to "hang". I think it's something else that causes the hanging problem.
Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top