Delteing an item from memoryDc.......................

Posted on 2003-03-14
Medium Priority
Last Modified: 2013-11-20
my question regards using bitmap and memorydc to avoid window flickering.  I have  created a memorydc that stores a bitmap which in turn contains a user defined rectangle (using CreateCompatbleDC & CreateCompatibleBitmap). How can i remove the rectangle that is drawn on memorydc and place another new rectactangle in the MemoryDc? This is because when i zoom the rectagle  i want to get the zoomed image only, but now iam getting zoomed rectangle drawn on previous one. My coding is given below.

#include <afxwin.h>
#include  <stdio.h>
int x1 =100,y1=100,x2=200,y2=200;
CRect rcClient;
CDC dcMem;

class CMainWnd : public CFrameWnd {
      CMainWnd() {
          Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,
         r1.left   =  100;
          r1.top    =  100;
         r1.bottom =  200;
         r1.right  =  200;
         CPaintDC dc(this);
            CBitmap bmMem;
            CBitmap* oldBitmap;
            CBrush cb(RGB(100,100,100));
            rcClient.top = 0;
            rcClient.left = 0;
            rcClient.bottom = 700;
         rcClient.right  = 700;
            bmMem.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
                oldBitmap = dcMem.SelectObject(&bmMem);

     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=dcMem.SelectObject (&newpen);
            newbrush.CreateSolidBrush (col);
            oldbrush = dcMem.SelectObject (&newbrush);
                   dcMem.Rectangle (r1);      
               BitBlt(dc,rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), dcMem, 0, 0, SRCCOPY);


       void OnLButtonDown(UINT c, CPoint p) {
            //Zoom out the rectangle
               x1 += 20;
            y1 += 20;
            y2 -= 20;
            x2 -= 20;
            r1.left = x1;
            r1.top  = y1;
            r1.bottom = y2;
            r1.right = x2;
       void OnRButtonDown(UINT,CPoint p){
       // Zoom in the rectangle
            x1 -= 20;
            y1 -= 20;
            y2 += 20;
            x2 += 20;
            r1.left = x1;
            r1.top  = y1;
            r1.bottom = y2;
            r1.right = x2;

     bool OnEraseBkgnd(CDC* pDC){
            return true;


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


myframe a;

 Let knoe how to delete an item drawn on memorydc before writing new item.

Question by:hemanexp26
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 75 total points
ID: 8136927
You can always erase a memory DC bay drawing a rect with teh background color in a bitmap just before you draw the new rectangle.

Just add to the OnPaint function

CRect rcClient;
CBrush brBackground(::GetSysColor(COLOR_WINDOW))

dcMem.FillRect(rcClient, &brBackground);

before actually drawing the rectangle.

Author Comment

ID: 8210372
Thanx KurtVon. It is working.

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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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