Solved

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

Posted on 2001-06-24
4
301 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
[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
4 Comments
 
LVL 1

Accepted Solution

by:
iProgram earned 100 total points
ID: 6222547
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
ID: 6222557
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
ID: 6227549
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
ID: 9490124
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

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

624 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