MFC Support for Direct2D – Part 1

A few time ago, I begun writing a series of wrapper classes to make easier the using of Direct2D interfaces. Meanwhile, I discovered that MFC Library, beginning with Visual Studio 2010 SP1, offers an excelent built-in support. That’s pretty cool!
So, let’s use Direct2D (D2D) MFC classes for making a simple image viewer.

Image viewer using Direct2D MFC classes

  1. In the view’s class definition, add data members for render target and Direct2D bitmap.
  2. Create the render target in the view’s WM_CREATE message handler.
  3. In the view’s WM_SIZE message handler resize the render target.
  4. Override CView::OnUpdate and load the bitmap from the current document’s file.
  5. Prevent painintg in WM_ERASEBKGND message handler.
  6. Finally, override CView::OnDraw and draw the bitmap.

Notes

  • CD2DBitmap::Create uses WIC (Windows Imaging Component) interfaces, so beside common image formats like BMP, JPEG, GIF, PNG, and TIFF, can deal also with any other image format for which a WIC-compliant decoder is installed in system.
  • Next article will show an even easier mode of using Direct2D MFC support.

Demo project

Download: Image Viewer - Using Direct2D MFC Classes.zip (879)

Using Direct2D MFC Classes - Demo Project

Using Direct2D MFC Classes – Demo Project

Resources and related articles

One thought on “MFC Support for Direct2D – Part 1

  1. Craftsman

    Hi
    It’s useful guide but this line always returns S_FALSE

    HRESULT hr = m_renderTarget.Create(m_hWnd);

    Why?

    PS Even in your sample project.

    Reply

Leave a Reply