Objective:
Describes how to add a basic direct2d (d2d) object to a Visual C + + Microsoft Foundation Class Library (MFC) project, and then build the project into an application that outputs "Hello, World" on a gradient background.
This walkthrough demonstrates how to accomplish these tasks:
- Create an MFC application.
- Creates a solid color brush and a linear gradient brush.
- Modify the gradient brush so that it changes when the window is resized.
- Implements the Direct2D drawing handler.
- Verify the results.
System Prerequisites:
To complete this walkthrough, you need Visual Studio.
Creating an MFC application
- On The file menu, point to New, and then click Project.
- In the left pane of the New Project dialog box, under installed templates , expand Visual C + +, and then select MFC. In the middle pane, select MFC application. In the name box, type MFCd2dwalkthrough. Click OK.
- In the MFC Application Wizard , click Finish without changing any settings.
Create a solid color brush and a linear gradient brush
- in Solution Explorer , "Mfcd2dwalkthrough" Project, in the header file folder, open Mfcd2dwalkthroughview class to create three data variables.
cd2dtextformat* m_ Ptextformat; Cd2dsolidcolorbrush* M_pblackbrush; Cd2dlineargradientbrush* m_plineargradientbrush;
Save the file and close it.
- in the source Files folder, open the MFCD2DWalkthroughView.cpp. In
CMFCD2DWalkthroughView the constructor for the class, add the following code.
//EnableD2D support for this window:EnableD2dsupport ();//InitializeD2D Resources:m_pblackbrush =NewCD2dsolidcolorbrush (GetRenderTarget (),D2d1::colorf (D2d1::colorf::black)); M_ptextformat =New CD2dtextformat (GetRenderTarget (), _t ("Verdana"),50); M_ptextformat->get ()Settextalignment (Dwrite_text_alignment_center); M_ptextformat->get ()Setparagraphalignment (Dwrite_paragraph_alignment_center);D2d1_gradient_stop gradientstops[2]; gradientstops[0].color = D2d1::colorf (D2d1::colorf::white); gradientstops[0].position = 0.F; Gradientstops[1 ". Color = d2d1::colorf (d2d1::colorf::indigo); Gradientstops[1].position = 1.F; M_pLinearGradientBrush = new Cd2dlineargradientbrush ( GetRenderTarget (), GradientStops, ARRAYSIZE (gradientstops), D2D1:: Lineargradientbrushproperties (D2D1::P oint2f (0, Span data-mce-= "" >0), D2D1::P oint2f (0, 0)));
Save the file and close it.
Modify the gradient brush so that it changes when the window is resized
-
On the Project menu, click Class Wizard.
-
In the MFC Class Wizard , under class name , select CMFCD2DWalkthroughView .
-
On the Messages tab, in the message box, select WM_SIZE , and then click Add Handler. This action OnSize adds a message handler to the CMFCD2DWalkthroughView class.
-
In the existing Handlers box, select OnSize . Click Edit code to display the CMFCD2DWalkthroughView::OnSize method. At the end of this method, add the following code.
M_plineargradientbrush->setendpoint (CPoint (CX, CY));
Save the file and close it.
Implement DIRECT2DDrawing Handlers
-
On the Project menu, click Class Wizard.
-
In the MFC Class Wizard , under class name , select CMFCD2DWalkthroughView .
-
On The Messages tab, click Add Custom Message.
-
In the Add Custom Message dialog box, in the custom Windows message box, type AFX_WM_DRAW2D . In the message handler name box, type OnDraw2D . Select the registered messages option, and then click OK. This action AFX_WM_DRAW2D adds message handlers for messages to the CMFCD2DWalkthroughView class.
-
" existing handlers " box, select ondraw2d . Click "edit Code" to display cmfcd2dwalkthroughview::ondraw2d method. For cmfcd2dwalkthroughview::ondrawd2d
afx_msg LRESULT cmfcd2dwalkthroughview::ondraw2d (WPARAM WPARAM, LPARAM LPARAM) { chwndrendertarget* Prendertarget = (chwndrendertarget*) LParam; Assert_valid (prendertarget); CRect rect; GetClientRect (rect); Prendertarget->fillrectangle (rect, m_plineargradientbrush); Prendertarget->drawtext (_t ("Hello, world!" return
Save the file and close it.
Validation results
- Build and run the application. It should have a gradient rectangle that changes when you resize the window for a large hour. "Hello world!" Should appear in the center of the rectangle.
Reference Link: https://msdn.microsoft.com/zh-cn/library/gg482848.aspx
DIRECT2D development: Adding DIRECT2D objects to an MFC project