Problem with GUI flickering/Bitmap

So im trying to fix the flickering in my program because it updates very quickly, im still new with drawing so im not too sure what im doing wrong here.

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
      
            PAINTSTRUCT ps;
            HDC         hdc;
            HDC         hdcmem;
            HBITMAP     bitmap;

            hdc = GetDC(hwnd);
            hdcmem = CreateCompatibleDC(hdc);

            bitmap = CreateCompatibleBitmap(hdc,300,300);

            //Start drawing

            HPEN hPenOld;

            //Red Rectangle
            HPEN hLinePen;
            COLORREF qLineColor;
            qLineColor = RGB(0,0,0);
            hLinePen = CreatePen(PS_SOLID,1,qLineColor);
            hPenOld = (HPEN)SelectObject(hdc,hLinePen);



            Rectangle(hdc,info.rec1,info.rec2,info.rec3,info.rec3);

            qLineColor = RGB(0,0,255);
            hLinePen = CreatePen(PS_SOLID,5,qLineColor);
            hPenOld = (HPEN)SelectObject(hdc,hLinePen);

            MoveToEx(hdc,info.posX,info.posY,NULL);
            LineTo(hdc,info.posX,info.posY);

            BitBlt(hdc,0,0,300,300,hdcmem,0,0,SRCCOPY);

            SelectObject(hdc,hPenOld);
            DeleteObject(hLinePen);
            DeleteDC(hdcmem);
            DeleteObject(bitmap);
            ReleaseDC(hwnd, hdc);
Last edited on
I can spot three things wrong with your code, assuming it's from your WM_PAINT handler.

1. You should use BeginPaint/EndPaint rather than GetDC/ReleaseDC

2. You're not selecting your bitmap into the memory DC

3. You're drawing on the windows DC rather than memory DC

Also, when you use this approach you are responsible for painting the whole of the window. If you haven't done so already, you need provide a WM_ERASEBKGND handler which does nothing. Otherwise you'll still get flickering. And you'll need to paint the whole of the window (or invalidated region, if it's a partial update)

And you're "red" rectangle looks black?

Andy
Last edited on
Also the sequence in which the pen handles are created and selected in and out of the device context looks dodgy and smells of resource mismanagement
1
2
3
4
5
6
7
8
9
10
11
			
hLinePen = CreatePen(PS_SOLID,1,qLineColor);       //ok to start
hPenOld = (HPEN)SelectObject(hdc,hLinePen);  //OK
 
//The next two lines are  very suspect
hLinePen = CreatePen(PS_SOLID,5,qLineColor); 
 hPenOld = (HPEN)SelectObject(hdc,hLinePen);  //original pen handle now lost
			
//these next two lines  are also not right - using wrong resources
SelectObject(hdc,hPenOld); 
DeleteObject(hLinePen);
@Andy

I replaced DC with Beginpaint, im not too sure how to go about fixing the last 2 issues though (again im new at this).
2. Use SelectObject to select the bitmap into the memory dc, in the same way you select the pens, keeping the old one to swap back later (for consistency).

3. The other SelectObject calls (with the pens) should use hdcmem, as should the various drawing calls. That is, all the calls between // Start drawing and the BitBlt.

BitBlt then blits the bitmap from the memory dc (dcmem) onto the window dc (dc). Note that the BitBlt line is fine except for the size.

Remember to use SelectObject to restore the old bitmap before deleting the memory dc and bitmap.

You also need to fix the problems guestgulkan mentioned. You need to delete all the pens you created, and restore the old pen (the one which was there at the beginning).

Etc

Andy
Last edited on
From MSDN ( http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx )

MSDN wrote:

WS_EX_COMPOSITED
0x02000000L
Paints all descendants of a window in bottom-to-top painting order using double-buffering. For more information, see Remarks. This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
Windows 2000: This style is not supported.


Using WS_EX_COMPOSITED when creating your window (in the Extended Window Style) will fix the issue, MSDN said.
Last edited on
Topic archived. No new replies allowed.