?
Solved

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

Posted on 2003-03-03
7
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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