Click here to Skip to main content
15,886,815 members
Articles / Desktop Programming / MFC

Double Buffered Graphics using DirectDraw

Rate me:
Please Sign up or sign in to vote.
4.78/5 (13 votes)
23 Sep 20013 min read 196.9K   7K   77  
This article explains how my class CDXSurfaceMgr can be used to facilitate Double Buffered drawing.
// mfcdxView.cpp : implementation of the CMfcdxView class
//

#include "stdafx.h"
#include "mfcdx.h"

#include "mfcdxDoc.h"

#include "..\DXSurfaceMgr.h"
#include "mfcdxView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMfcdxView

IMPLEMENT_DYNCREATE(CMfcdxView, CView)

BEGIN_MESSAGE_MAP(CMfcdxView, CView)
	//{{AFX_MSG_MAP(CMfcdxView)
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcdxView construction/destruction

CMfcdxView::CMfcdxView()
{
	// TODO: add construction code here

}

CMfcdxView::~CMfcdxView()
{
}

BOOL CMfcdxView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdxView drawing

void CMfcdxView::OnDraw(CDC* pDC)
{
	CMfcdxDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	HDC& hdc = dxMgr_.BeginPaint(GetSysColor(COLOR_3DFACE));
	if(NULL !=hdc)
	{
		::SetBkMode(hdc,TRANSPARENT);

		CRect client; GetClientRect(&client);

		filllines(hdc, client, RGB(128, 128, 128));

		CDC* cdc = CDC::FromHandle(hdc);

		CPen p(PS_DASHDOTDOT, 1, RGB(128, 128, 128));
		CPen* op = cdc->SelectObject(&p);
		
		cdc->MoveTo(30, 87);
		cdc->LineTo(client.Width()-20, client.Height() - 30);

		cdc->SetBkMode(TRANSPARENT);
		cdc->TextOut(20, 20, "double buffering in directx", 27);
		cdc->SelectObject(op);

		dxMgr_.EndPaint();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdxView printing

BOOL CMfcdxView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMfcdxView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMfcdxView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdxView diagnostics

#ifdef _DEBUG
void CMfcdxView::AssertValid() const
{
	CView::AssertValid();
}

void CMfcdxView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMfcdxDoc* CMfcdxView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMfcdxDoc)));
	return (CMfcdxDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMfcdxView message handlers

int CMfcdxView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if(!dxMgr_.Initialise(m_hWnd)) return -1;
	
	return 0;
}

BOOL CMfcdxView::OnEraseBkgnd(CDC* pDC) 
{
	return -1;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions