BitBlt Problem GDI
I am having trouble with using BitBlt in this program. You resize the window and the ellipse resizes with it. Of course, with the normal hdc method, It is choppy and flickery. I tried the BitBlt method, but that doesn't work either (probably because im doing it wrong). Can someone fix my what Im donig wrong? thanx
bool sizing; //global
case WM_PAINT:
{
RECT rect;
GetClientRect(hwnd, &rect);
hdc = BeginPaint(hwnd, &ps);
mem = CreateCompatibleDC(hdc);
SelectObject(mem, GetStockObject(HOLLOW_BRUSH));
if(sizing)
{
Ellipse(mem开发者_开发技巧,rect.left, rect.top, rect.right, rect.bottom);
}
BitBlt(hdc, rect.left, rect.top, rect.left - rect.right, rect.top -rect.bottom , mem, rect.left, rect.top, SRCCOPY);
DeleteDC(mem);
EndPaint(hwnd, &ps);
break;
}
case WM_SIZE:
sizing = true;
break;
It looks like you're trying to draw to a memory-based bitmap, and then bitblt that to the screen, to avoid flicker?
First issue here is dealing with flicker: first you need to override WM_ERASEBKGND as Hans points out - otherwise Windows will erase the background with whatever the window brush is (from RegisterClass), and that erasing is the usual cause of flicker.
The next problem here is that you're using an 'empty' DC: CreateCompatibleDC gives you a DC - which is just a drawing context - but the context contains a 1 pixel by 1 pixel bitmap. To draw offscreen, you need a DC and a bitmap. Do take time to read the MSDN page for CreateCompatible - it calls out this exact issue.
If you're new to this, think of a bitmap as the actual canvas that you draw on - the DC is just the support structure to do that drawing. As your code stands, you've got the easel and paint brushes set up - but you're not painting on anything.
The usual approach here is:
- CreateCompatibleDC to create the DC
- CreateCompatibleBitmap to create the bitmap that you will actually draw to
- SelectObject your new bitmap into the memory DC
- Draw to the memory DC - which draws on the bitmap that you've selected into it
- BitBlt from the memory DC (ie, your bitmap, which is selected into it) to the one from WM_PAINT
- Clean up: SelectObject the original bitmap back into the memory DC, and delete the bitmap and DC.
精彩评论