DIRECT2D development: Adding DIRECT2D objects to an MFC project

Source: Internet
Author: User

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
    1. On The file menu, point to New, and then click Project.
    2. 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.
    3. In the MFC Application Wizard , click Finish without changing any settings.

Create a solid color brush and a linear gradient brush

  1. 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.

  2. 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
  1. On the Project menu, click Class Wizard.

  2. In the MFC Class Wizard , under class name , select CMFCD2DWalkthroughView .

  3. 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.

  4. 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
  1. On the Project menu, click Class Wizard.

  2. In the MFC Class Wizard , under class name , select CMFCD2DWalkthroughView .

  3. On The Messages tab, click Add Custom Message.

  4. 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.

  5. " 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
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.