Solved

How to draw a picture in the background without flickering on scrolling?

Posted on 2001-06-24
4
269 Views
Last Modified: 2013-11-20
I am trying to draw a picture in the background of my control, the picture is determined in a property called Picture from the type CPictureHolder. I need to draw this picture in a way to get rid of flickering while scrolling or resizing. I have tried several methods but seems that I am missing something! Is it possible to have this functionality??
0
Comment
Question by:Daed
4 Comments
 
LVL 1

Accepted Solution

by:
iProgram earned 100 total points
Comment Utility
You can draw it in OnPaint.
To get rid of flicking, you can use CMemDC to instead of CDC object in OnPaint().
See the example here:
http://codeguru.earthweb.com/bitmap/formview_background.shtml
And the following is CMemDC:
//Include it in your CPP file
//MemDc.h
#ifndef _MEMDC_H_
#define _MEMDC_H_


class CMemDC : public CDC
{
private:
     CBitmap m_bitmap; // Offscreen bitmap
     CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
     CDC* m_pDC; // Saves CDC passed in constructor
     CRect m_rect; // Rectangle of drawing area.
     BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.

public:
     CMemDC(CDC* pDC, CRect rect = CRect(0,0,0,0)) : CDC(), m_oldBitmap(NULL), m_pDC(pDC)
     {
          ASSERT(m_pDC != NULL); // If you asserted here, you passed in a NULL CDC.
         
          m_bMemDC = !pDC->IsPrinting();
         
          if (m_bMemDC){
               // Create a Memory DC
               CreateCompatibleDC(pDC);
               if ( rect == CRect(0,0,0,0) )
                    pDC->GetClipBox(&m_rect);
               else
                    m_rect = rect;

               m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
               m_oldBitmap = SelectObject(&m_bitmap);
               SetWindowOrg(m_rect.left, m_rect.top);
          } else {
               // Make a copy of the relevent parts of the current DC for printing
               m_bPrinting = pDC->m_bPrinting;
               m_hDC = pDC->m_hDC;
               m_hAttribDC = pDC->m_hAttribDC;
          }
     }
     
     ~CMemDC()
     {
          if(m_bMemDC)
          {
               // Copy the offscreen bitmap onto the screen.
               m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                    this, m_rect.left, m_rect.top, SRCCOPY);
               //Swap back the original bitmap.
               SelectObject(m_oldBitmap);
          }
          else
          {
               // All we need to do is replace the DC with an illegal value,
               // this keeps us from accidently deleting the handles associated with
               // the CDC that was passed to the constructor.
               m_hDC = m_hAttribDC = NULL;
          }
     }
     
     // Allow usage as a pointer
     CMemDC* operator->() { return this; }
     
     // Allow usage as a pointer
     operator CMemDC*() { return this; }
};

#endif
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
You should override Render() in your CPictureHolder-derived class.  Then draw it only when necessary.  Use ValidateRect() and/or ValidateRgn()to mask parts of the picture that are not being changed by the user-activity.

As IProgram said, using a memory DC might help.

-- Dan
0
 

Author Comment

by:Daed
Comment Utility
I tried using the CMemDC in OnPaint(), but my problem is that i need to write over the background picture, and this happens on DrawItem of another class, so when using the CMemDC there also, the new writtings are drawn over the picture (on a white rect) and hide it. How can i write over the background picture without any mixing in the colors and no flickering as well!
0
 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : iProgram  

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 109
bigDiff challenge 17 74
firstChar challenge 13 83
Not needed 13 53
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: 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…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now