VCL.Direct2D Sample
Language:
This sample demonstrates how to use Vcl.Direct2D components in a simple graphic editor.
Note: Direct2D components are only supported on Windows 7, so make sure you are using these version of Windows when testing this application.
Contents |
Location
You can find the Direct2D sample project at:
- Start > Programs > Embarcadero RAD Studio > Samples > Delphi > VCL > Direct2D
- Subversion Repository for Delphi: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/Delphi/VCL/Direct2D/
Description
This sample consists of a simple graphic editor for Windows 7. You can draw basic shapes like ellipses, rectangles, pies etc, but also add text. In addition you can open existing image files. You can set the colors, dimensions and pen width for the shapes and text. For text in particular, you can also change its formatting from the third tab.
The user interface of this graphic editor is created using ribbons and related components.
How to Use the Sample
- Navigate to Start > Programs > Embarcadero RAD Studio > Samples and open D2D.dproj
- Press F9 or choose Run > Run
Files
The project has one source file, uMain, which contains the class for the main form.
Classes
- TForm1 is the class of the main form. It has a ribbon component with three tabs, each of them containing controls for changing the formatting of text and shapes. It also provides implementation for all the event handlers.
Implementation
- When the combobox selected value changes, automatically the corresponding shape is drawn on canvas.
- The mouse wheel event handler determines the scrolling through the types of shapes.
- FPoints private field is an array that stores the anchor points for the chosen shape. The number of anchor points depends on the type of shape: rectangles have two, arcs have three etc.
- A TDirect2DCanvas object is created to be able to use specific Direct2D methods, like using antialiasing. In this application, you can choose to use antialiasing by checking the option in the View tab.
- For normal GDI drawing a standard TCanvas is used.
- To draw the selected shape in real time the pmNotXor pen mode is used. This draws the shape corresponding to the new mouse position and erases the previous one.
- To invoke the repainting of the form after each user interaction, Invalidate method is called.
Uses
- TDirect2DCanvas
- TForm.OnMouseWheel
- TRibbon
- TBalloonHint
- TRibbonQuickAccessToolbar
- TRibbonPage
- TDirect2DCanvas.BeginDraw
- TDirect2DCanvas.EndDraw
- TOpenPictureDialog
- TFileExit