- Visual Studio 2010
- MS-LPL
- Direct2D
- MFC
- Graphics
- Drawing
- How to
- 12/13/2011
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
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.
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)
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; }
- 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