Link to home
Start Free TrialLog in
Avatar of mycpp2001
mycpp2001

asked on

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.

ASKER CERTIFIED SOLUTION
Avatar of KurtVon
KurtVon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mycpp2001
mycpp2001

ASKER

Thanks KurtVon. It is working.