BitBlt() BackBuffer on form s dc doesn't work.

At first I used my form 's normal HDC to draw on, but that caused flickering so I decided to make a buffer and found that bitblt what used for copying the buffer on the main DC, but of course it didnt work.

The problem must be in the function Initialize() or HDCUpdate().
I suppose anyone with experience can see the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <windows.h>
#include "main.h"
using namespace std;

HDC hdcBuffer;

int Initialize()
{
    hdcBuffer = CreateCompatibleDC(NULL);
    hdc = GetDC(hwnd);
}

int drawCircle(int X, int Y, int R, int Pen, int Fill)
{
    HPEN hNPen=CreatePen(PS_SOLID, 1,Pen);
    HPEN hOPen=(HPEN)SelectObject(hdcBuffer, hNPen);
    HBRUSH hOldBrush;
    HBRUSH hNewBrush;
    if(Fill){
        hNewBrush=CreateSolidBrush(Pen);
        hOldBrush=(HBRUSH)SelectObject(hdcBuffer, hNewBrush);
    } else {
        hNewBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
        hOldBrush=(HBRUSH)SelectObject(hdcBuffer, hNewBrush);
    }
    
    Ellipse(hdcBuffer, X-R, Y+R, X+R, Y-R);
    
    DeleteObject(SelectObject(hdcBuffer, hOPen));
    DeleteObject(SelectObject(hdcBuffer, hOldBrush));
    
    return 0;
}

int HDCClear()
{
    int Color = RGB(255, 255, 255);
    HPEN hNPen = CreatePen(PS_SOLID, 1, Color);
    HPEN hOPen = (HPEN)SelectObject(hdcBuffer, hNPen);
    HBRUSH hOldBrush;
    HBRUSH hNewBrush;
    hNewBrush=CreateSolidBrush(Color);
    hOldBrush=(HBRUSH)SelectObject(hdcBuffer, hNewBrush);
    
    Rectangle(hdcBuffer, 0, 0, planeWidth, planeHeight); 
    
    DeleteObject(SelectObject(hdcBuffer, hOPen));
    DeleteObject(SelectObject(hdcBuffer, hOldBrush));
    
    return 0;
}

int HDCUpdate()
{
    BitBlt(hdc, 0, 0, planeWidth, planeHeight, hdcBuffer, 0, 0, SRCCOPY);
    ReleaseDC(hwnd,hdc);
}
When you create a new DC, it has a 1x1 monochrome bitmap selected in it by default. If you want to make a drawing surface, you'll need to create a color bitmap and select it into the new DC.

You also don't want to 'Get' the window's DC long term like that. You want to have it locked for a short a time as possible.

A proper initialize/destroy group would probably look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Initialize(HWND window)
{
  HDC hdcWindow = GetDC(window);
  hdcBuffer = CreateCompatibleDC(NULL);
  bmpBuffer = CreateCompatibleBitmap( hdcWindow, __width__, __height__ );
  ReleaseDC(window,hdcWindow);

  bmpOld = (HBITMAP)SelectObject(hdcBuffer,bmpBuffer);
}

void Destroy()
{
  SelectObject(hdcBuffer,bmpOld);
  DeleteObject(bmpBuffer);
  DeleteDC(hdcBuffer);
}


Note the CreateCompatibleBitmap call. Use the window DC and not the buffer DC (because the buffer DC has a monochrome bitmap in it by default, that will make your new bitmap monochrome).
Thank you very much, it is flicker free now.
Topic archived. No new replies allowed.