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

hi,
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 {
   public:
      CMainWnd() {
          Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,
          rectDefault,NULL,NULL);
               r1.left   =  100;
          r1.top    =  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) )  {
          InvalidateRect(r1,true);    
                r1.left =  p.x;
          r1.top =  p.y;
          r1.bottom =  p.y + 100;
          r1.right =  p.x + 100;
              x1 = p.x;
                y1=p.y;
                x2 = x1+100;
                y2 = y1+100;
          InvalidateRect(r1,true);
        }
     }

     DECLARE_MESSAGE_MAP()
};


class myframe : public CWinApp {
      public:
      BOOL InitInstance()
      {
            m_pMainWnd = new CMainWnd();
            m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
            m_pMainWnd->UpdateWindow();
            return  TRUE;
      }
};

BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
         ON_WM_PAINT()
         ON_WM_LBUTTONDOWN()
         ON_WM_LBUTTONUP()
         ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

myframe a;

Thanx.

mycpp2001Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
KurtVonConnect With a Mentor Commented:
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;

        GetClientRect(rcClient);
        dcMem.CreateCompatibleDC(dc);
        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.top, rcCLient.Width(), rcClient.Height(), dcMem, 0, 0, SRCCOPY);

        dcMem.SelectObject(oldBrush);
        dcMem.SelectObject(oldPen);
        dcMem.SelectObject(oldBitmap);
     }

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.
0
 
mycpp2001Author Commented:
Thanks KurtVon. It is working.
0
All Courses

From novice to tech pro — start learning today.