Skip to content

Latest commit

 

History

History
 
 

MFC Direct2D Hello World sample

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

MFC Direct2D Hello World sample

Requires

  • Visual Studio 2010

License

  • MS-LPL

Technologies

  • Direct2D
  • MFC

Topics

  • Graphics
  • Drawing
  • How to

Updated

  • 12/13/2011

Description

Introduction

Shows how to create a very simple MFC Direct2D application, or how to convert an existing MFC application to Direct2D.

This code is the illustration of this blog article: MFC and Direct2D, part 1/N

Building the Sample

To build this sample, open the solution (.sln) file titled Helloworld.sln (C++ project)  from Visual Studio 2010 SP1 (any SKU). Press F7 or go to Build->Build Solution from the top menu after the sample has loaded.

The Service Pack 1 for Visual Studio 2010 is required.

Runing the Sample

To run this sample after building it, press F5 (run with debugging enabled) or Ctrl-F5 (run without debugging enabled) from Visual Studio 2010 SP1 (any SKU). (Or select the corresponding options from the Debug menu.)

Description

Description : see blog article on www.blogmfc.com

http://www.blogmfc.com/n2011/12/14/mfc-and-direct2d-part-1n/

The blog article shows how to convert an MFC application which uses GDI to a Direct2D MFC application, and focuses on explaining how the things work under the hood.

The code sample uses a simple MFC AppWizard generated application. Only the code of the View is modified (file HelloWorldApp.cpp / .h)

C++
Edit|Remove
cplusplus
using namespace D2D1; 
 
CHelloWorldView::CHelloWorldView() 
{ 
    // Initialize Direct2D 
    EnableD2DSupport(); 
 
    // Create D2D graphic resources 
    m_pBlueBrush = new CD2DSolidColorBrush(GetRenderTarget(), ColorF(ColorF::RoyalBlue)); 
 
    m_pTextFormat = new CD2DTextFormat(GetRenderTarget(), _T("Gabriola"), 50); 
    m_pTextFormat->Get()->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); 
    m_pTextFormat->Get()->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); 
} 
 
 
 
// AFX_WM_DRAW2D event handler 
afx_msg LRESULT CHelloWorldView::OnDraw2d(WPARAM wParam, LPARAM lParam) 
{ 
    CHwndRenderTarget* pRenderTarget = (CHwndRenderTarget*)lParam; 
    ASSERT_VALID(pRenderTarget); 
 
    // Clear window background 
    pRenderTarget->Clear(ColorF(ColorF::Beige)); 
 
    // Draw text 
    CRect rect; 
    GetClientRect(rect); 
    pRenderTarget->DrawText(_T("Hello, World!"), rect, m_pBlueBrush, m_pTextFormat); 
    return TRUE; 
} 

Source Code Files

  • helloworldview.cpp - the code of the View window which displays contents on the screen. The code of this file is modified to use Direct2D instead of GDI
  • helloworldview.h - header file for helloworldview.cpp