Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2003-03-12
Medium Priority
Last Modified: 2013-11-20
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 {
      CMainWnd() {
          Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,
               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) )  {
                r1.left =  p.x;
          r1.top =  p.y;
          r1.bottom =  p.y + 100;
          r1.right =  p.x + 100;
              x1 = p.x;
                x2 = x1+100;
                y2 = y1+100;


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


myframe a;


Question by:mycpp2001
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
LVL 11

Accepted Solution

KurtVon earned 60 total points
ID: 8119495
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;

        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);


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.

Author Comment

ID: 8125335
Thanks KurtVon. It is working.

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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 …

610 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