Window flickering!!!!!!!!!!!!!!!!!!!!!!!!!!

my problem is as follows. In my program i have drawn a rectangle
in OnPaint method. When i move the rectangle by using OnMouseMove
method the window is getting flickered a lot. Iam not using MemDc
or anything else.
  I tried InvalidateRect(). After using InvalidateRect() flickering is minimized but i want flicker free window. My program is given below.

#include <afxwin.h>
#include  <stdio.h>
COLORREF col =  RGB(255,0,0);
int f =0;
int x1 =100,y1=100,x2=200,y2=200;
CRect r1;
class CMainWnd : public CFrameWnd {
      CMainWnd() {
          Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,
               r1.left   =  100;
    =  100;
          r1.bottom =  200;
          r1.right  =  200;

        void OnPaint(){
         CPaintDC dc(this);
         CBrush newbrush;
         CBrush* oldbrush;
         CPen newpen;
         CPen* oldpen;
         col = RGB(0,0,255);
         newpen.CreatePen (PS_SOLID,1,col);
         oldpen=dc.SelectObject (&newpen);
         newbrush.CreateSolidBrush (col);
         oldbrush = dc.SelectObject (&newbrush);
         dc.Rectangle (r1);

      void OnLButtonDown(UINT c, CPoint p) {
            f =1;
        void OnLButtonUp(UINT,CPoint p){
          f = 0;
      void OnMouseMove(UINT flag, CPoint p) {
        if ((flag == MK_LBUTTON)&&(p.x >= x1)
          &&(p.x <=x2) && (p.y >= y1) && (p.x <= y2) )  {
                r1.left =  p.x;
 =  p.y;
          r1.bottom =  p.y + 100;
          r1.right =  p.x + 100;
              x1 = p.x;
                x2 = x1+100;
                y2 = y1+100;


class myframe : public CWinApp {
      BOOL InitInstance()
            m_pMainWnd = new CMainWnd();
            return  TRUE;


myframe a;


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, you said it yourself.  If you don't want flickering you pretty much have to use a memory DC.  So first override OnEraseBackground to return TRUE without calling the base function.  Then change the OnPaint function to something like

    void OnPaint(){
        CPaintDC dc(this);
        CDC dcMem;
        CBitmap bmMem;
        CBitmap* oldBitmap;
        CBrush newbrush;
        CBrush* oldbrush;
        CPen newpen;
        CPen* oldpen;
        CRect rcClient;

        bmMem.CreateCompatibleBitmap(dc, rcClient.Width(), rcClient.Height());
        oldBitmap = dcMem.SelectObject(&bmMem);
        dcMem.PatBlt(0, 0, rcClient.Width(), rcClient.Height(), WHITENESS);

        col = RGB(0,0,255);
        newpen.CreatePen (PS_SOLID,1,col);
        oldpen=dcMem.SelectObject (&newpen);
        newbrush.CreateSolidBrush (col);
        oldbrush = dcMem.SelectObject (&newbrush);
        dcMem.Rectangle (r1);

        dc.BitBlt(rcClient.left,, rcCLient.Width(), rcClient.Height(), dcMem, 0, 0, SRCCOPY);


By the way, I'm surprised the program didn't crash on you.  You need to be sure to always reselect old GDI objects into a dc before destroying it.

This was entered by hand, so watch for typos.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mycpp2001Author Commented:
Thanks KurtVon. It is working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.