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. 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;
class CMainWnd : public CFrameWnd {
     CMainWnd() {
     Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,

        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 (x1,y1,x2,y2);

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

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


myframe a;

How can i stop flickering my window while i move the rectangle on the screen?

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.

Roshan DavisCommented:
Put this line of code in your CFrameWnd's OnCreate function

SetClassLong(m_hWnd, GCL_HBRBACKGROUND, (LONG)::GetStockObject(NULL_BRUSH));

Good Luck
Use a memory DC in OnPaint

Change invalidate(true) to
mycpp2001Author Commented:
hi roshman,
    I tried the one you mentioned. Is it possible to avoid flickering with out using MemDc?

Probably not

If you don't use a memory DC you need to be sure to paint each pixel only once (probably very hard or nearly impossible, for complex drawings)

The problem is say you fill the background, then draw a line or text on top of it.  The pixels where the line/text overlaps the background are first drawn in the background color, then in the text/line color, so will flicker if the screen happens to refresh between the time of the background update and the text/line drawing

PAQed - no points refunded (of 20)

Community Support Moderator

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
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.