?
Solved

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

Posted on 2003-03-12
2
Medium Priority
?
473 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.
  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.

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
2 Comments
 
LVL 11

Accepted Solution

by:
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;

        GetClientRect(rcClient);
        dcMem.CreateCompatibleDC(dc);
        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);

        dcMem.SelectObject(oldBrush);
        dcMem.SelectObject(oldPen);
        dcMem.SelectObject(oldBitmap);
     }

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

Author Comment

by:mycpp2001
ID: 8125335
Thanks KurtVon. It is working.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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

741 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