Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-03
7
Medium Priority
?
468 Views
Last Modified: 2013-11-20
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. 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 {
   public:
     CMainWnd() {
     Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,
     rectDefault,NULL,NULL);
        }

        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;
             y1=p.y;
          x2 = x1+100;
          y2 = y1+100;
          Invalidate(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;

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

0
Comment
Question by:mycpp2001
5 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 8056900
Put this line of code in your CFrameWnd's OnCreate function


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


Good Luck
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 8064261
Use a memory DC in OnPaint

Change invalidate(true) to
InvalidateRect(NULL,FALSE)
UpdateWindow();
0
 

Author Comment

by:mycpp2001
ID: 8092525
hi roshman,
    I tried the one you mentioned. Is it possible to avoid flickering with out using MemDc?

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 8142341
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

0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11812356
PAQed - no points refunded (of 20)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Loops Section Overview

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question