Flicker Free Drawing
Site Map Feedback

Download:

Up Controls Files Moving Data

Use Video Page Swapping with Device Contexts

This is a slightly optimised version of the popular CMemDC class that everyone seems to invent and re-invent.
For the fastest redraw put a bitmap in your Dialog, Control or View header:
  CBitmap Bitmap;
In the .cpp file:
#include "MemDC.h"
and then call the following just once:
  Bitmap.CreateCompatibleBitmap(pDC, Rect.Width(), Rect.Height());
If you don't have pDC use GetDC() which returns the DC of the screen.
If you're using OnPaint() use the following two lines:
  CPaintDC  PaintDC(this); // device context for painting
  CMemDC dc(PaintDC, &Bitmap);
Then draw everything to dc - and that's it!
You don't need the Bitmap - just create the CMemDC with:
  CPaintDC  PaintDC(this); // device context for painting
  CMemDC dc(PaintDC);
it'll be slower for complex animations, but fine for most Controls. The same applies to OwnerDraw Buttons, create the MemDC object as follows:
void CMemDCTesterDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) {
  CMemDC dc(lpDrawItemStruct, &Bitmap);
Theres also a constructor for Views:
void CMyView::OnDraw(CDC* pDC) {
  CMemDC dc(pDC, &Bitmap);
There is more about drawing on the Graphics section of this site.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1509) Last modified: Fri, 05 Jun 2009 16:41:27 +0100