Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dog bark java program 15 111
sum67 challenge 35 110
no14 challenge 14 72
unix example issues 18 99
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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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