Solved

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

Posted on 2001-06-24
4
279 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 92
Expand to include initial dialog with two choices. 9 76
sameEnds challenge 3 157
"Black Box" Testing of Control System Software 2 49
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ā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

19 Experts available now in Live!

Get 1:1 Help Now