This sample demonstrates how to create a simple graphical editor.
You can find the Graphex sample project at:
- Start > Programs > Embarcadero RAD Studio > Samples and then navigate to either:
- Subversion Repository for Delphi: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/Delphi/VCL/Graphex/
- Subversion Repository for C++: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/CPP/VCL/Graphex/
The application creates a simple graphic editor that provides basic graphic functionality: drawing shapes (lines, rectangles, ellipses, round rectangles), changing the type, width, and color of lines and hatches. It also provides basic File actions: creating, saving and opening an image, and Edit actions: copying, cutting, and pasting.
How to Use the Sample
- Navigate to Start > Programs > Embarcadero RAD Studio > Samples and open graphex.dproj.
- Press F9 or choose Run > Run.
The class for the form that is displayed when the user chooses File > New from the graphic editor.
The class for the main form that represents the editor itself.
- TNewBMPForm is the class for the form that is displayed when you want to create a new image. You can use this form to choose the size of the new canvas.
- TForm1 is the class for the main form. It contains toolbars, a menu bar, and a TImage component as the drawing surface. It also contains dialog components for displaying open, save, and color dialogs. The toolbars are actually panels with customized TSpeedButton components.
- 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 copy images to the clipboard, TClipboard.Assign is used.
- Before pasting the content of the clipboard on canvas, HasFormat is used to verify if it is in bitmap format.
- To access the drawing surface of the TImage component, the TImage.Canvas property is used.
- To paint different shapes on the canvas, a variety of properties, methods, and types are used: TCanvas.Brush, TCanvas.Pen, TCanvas.Draw, TBrushStyle, TPenStyle, TCanvas.MoveTo, TCanvas.Rectangle, TCanvas.LineTo, TCanvas.Ellipse, TCanvas.RoundRect.