Direct2d Quick Start

Source: Internet
Author: User
Tags pen stroke
This article is part of the msdn direct2d translation. Thank you! Original article: http://msdn.microsoft.com/en-us/library/windows/desktop/dd535473 (V = vs.85). aspx reprint please note the Source: direct2d is used to create 2D graphics local instant mode application interface. This article describes how to use direct2d to draw in a typical Win32 application.
This article includes the following content:
  • Draw a simple rectangle
  • Step 1: Include the direct2d header file
  • Step 2: Create id2d1factory
  • Step 3: Create id2d1hwndrendertarget
  • Step 4: Create a brush
  • Step 5: Draw a rectangle
  • Step 6: release resources
  • Create a simple direct2d Application
  • Related Topics
Draw a simple rectangle
Switch (Message) {Case wm_paint: {paintstruct pS; beginpaint (hwnd, & PS); // obtain the size of the drawing area. rect RC; getclientrect (hwnd, & rc); // Save the original object hgdiobj original = NULL; original = SelectObject (PS. HDC, getstockobject (dc_pen); // creates a paint brush. hpen blackpen = createpen (ps_solid, 3, 0); // select the paint brush. selectObject (PS. HDC, blackpen); // draw a rectangle. rectangle (PS. HDC, RC. left + 100, RC. top + 100, RC. right-100, RC. bottom-100); deleteobject (blackpen); // restore the original object SelectObject (PS. HDC, original); endpaint (hwnd, & PS);} return 0 ;//... process other messages.
To draw a rectangle with GDI, capture the wm_paint message, as shown in the following code.
Step 1: Include the direct2d header file
In addition to the header files required by Win32 applications, it also contains the d2d1. h header file.
Step 2: Create id2d1factory
One of the first tasks of all direct2d routines is to create an id2d1factory.
ID2D1Factory* pD2DFactory = NULL;HRESULT hr = D2D1CreateFactory(    D2D1_FACTORY_TYPE_SINGLE_THREADED,    &pD2DFactory    );
The id2d1factory interface is the starting point for using direct2d; Use id2d1factory to create direct2d resources. When creating id2d1factory, you can specify whether it is multi-thread or single-thread. This routine creates a single-threaded factory.
Generally, your application should create a factory and save it until the application ends. Step 3: Create id2d1hwndrendertarget
After creating a factory, use it to create a rendering target.
// Obtain the size of the drawn area. rect RC; getclientrect (hwnd, & rc); // create direct2d rendering target id2d1hwndrendertarget * PRT = NULL; hresult hR = pd2dfactory-> createhwndrendertarget (d2d1: rendertargetproperties (), d2d1:: hwndrendertargetproperties (hwnd, d2d1: sizeu (RC. right-RC. left, RC. bottom-RC. top), & PRT );
The rendering target is a device that can complete drawing operations and create drawing resources related to devices such as paint brushes. Different rendering targets are rendered to different devices. The preceding routine uses id2d1hwndrendertarget to render it to a certain area of the screen.

If possible, the rendering target uses GPU to accelerate rendering operations and create drawing resources. Otherwise, the rendering target uses the CPU to process rendering commands and create resources. (When creating a rendering target, you can use d2d1_render_target_type to mark and modify this behavior .) The createhwndrendertarget function has three parameters. The first parameter is a d2d1_render_target_properties structure that specifies the remote display option to force the rendering target to be rendered to the software or hardware and specify the DPI. This example uses the d2d1: rendertargetproperties helper function to receive default rendering target attributes. The second parameter is a d2d1_hwnd_render_target_properties structure that specifies the handle of the window to be rendered, the initial size (pixels) of the rendering target, and its description options. In this example, d2d1: hwndrendertargetproperties is used.
Auxiliary Function to specify a window handle and initial size, and use the default description option. The third parameter is the rendering target pointer address. When a rendering target is created and hardware acceleration is available, resources are allocated to the GPU of the computer. It helps improve performance by creating a rendering target and saving it as long as possible. The application creates a rendering target and saves it to the end of the program or receives the d2derr_recreate_target error. When you receive this error, You need to recreate the rendering target (including any resources it creates ).
Step 4: Create a paint brush
Like a factory, rendering targets can create rendering resources. In this example, create a paint brush with the rendering target.
ID2D1SolidColorBrush* pBlackBrush = NULL;if (SUCCEEDED(hr)){                            pRT->CreateSolidColorBrush(        D2D1::ColorF(D2D1::ColorF::Black),        &pBlackBrush        ); }
A painting brush is an object that draws an area, just like a pen stroke or a filled geometric object. In this example, the paint brush uses the defined solid black to draw an area. Direct2d also provides the actual type of Painting Brush: gradient painting brush is used to draw linear or radial gradient, bitmap painting brush uses bitmap and pattern for painting. Some drawing APIs provide paint brushes that draw outlines and fill shapes. Direct2d is different: it does not provide a paint brush, but uses a paint brush to draw the outline and fill the shape. When creating an outline, you can use the brush of the id2d1strokestyle interface to control the stroke path. A paint brush can only be used by the rendering target created for it or by other rendering targets in the same resource area. Generally, you should create a paint brush and save it until the rendering target of the image is created. However, id2d1solidcolorbrush is an exception. Because it is relatively easy to create, you can create an id2d1solidcolorbrush object every time you draw a framework without causing obvious performance loss. You can also use only one id2d1solidcolorbrush object to change its color each time you use it. Step 5: Draw a rectangle
Next, use the rendering target to draw a rectangle.
pRT->BeginDraw();pRT->DrawRectangle(    D2D1::RectF(        rc.left + 100.0f,        rc.top + 100.0f,        rc.right - 100.0f,        rc.bottom - 100.0f),        pBlackBrush);HRESULT hr = pRT->EndDraw(); 
The drawrectangle function receives two parameters: the rectangle to be drawn and the painting brush used to draw the rectangular outer frame. You can also specify stroke width, fill pattern, line node, and end "hat" options. Before issuing any drawing instruction, you must call the begindraw function and call the enddraw function. The enddraw function returns an hresult monk, indicating whether the painting is successful.
Step 6: release resources
When no more frames need to be drawn or receive the d2derr_recreate_target error, release the rendering target and all devices it creates.
SafeRelease(pRT);SafeRelease(pBlackBrush);
When the application uses direct2d resources (for example, when the program is about to exit), release the direct2d factory object.
SafeRelease(pD2DFactory);
Create a simple direct2d Application 

The code in this article shows the basic elements of a direct2d application. In short, this article omitted the application framework and the error tolerance Processing code that a good application should have. Create a simple direct2d application with complete code and better design principles. For more information, see creating a simple direct2d application.

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.