Programming C++Builder How to Handle Multiple Drawing Objects in CBuilder?
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/ on line 149

How to handle multiple drawing objects in your application?

In a real application, the graphics always comes complex, you have to handle more than one drawing objects to achieve your purpose. This article tells you how to do it.

Various drawing methods (rectangle, shape, line, and so on) are typically available on the toolbar and button panel. Applications can respond to clicks on speed buttons to set the desired drawing objects. This section describes how to:

  1. Keep track of which drawing tool to use.
  2. Change the tool with speed buttons.
  3. Use drawing tools.

How to Keep track of which drawing tool to use?

A graphics program needs to keep track of what kind of drawing tool (such as a line, rectangle, ellipse, or rounded rectangle) a user might want to use at any given time. Typically, you would use the C++ enumerated type to list the available tools. Since an enumerated type is also a type declaration, you can use C++'s type-checking to ensure that you assign only those specific values.

For example, the following code declares an enumerated type for each drawing tool available in a graphics application:

typedef enum {dtLine, dtRectangle, dtEllipse, dtRoundRect} TDrawingTool;

A variable of type TDrawingTool can be assigned only one of the constants dtLine, dtRectangle, dtEllipse, or dtRoundRect.
By convention, type identifiers begin with the letter T, and groups of similar constants (such as those making up an enumerated type) begin with a 2-letter prefix (such as dt for "drawing tool".

In the following code, a field added to a form keeps track of the form's drawing tool:

enum TDrawingTool {dtLine, dtRectangle, dtEllipse, dtRoundRect};
class TForm1 : public TForm
__published:  // IDE-managed Components
  void __fastcallFormMouseDown(TObject *Sender, TMouseButton Button,
  TShiftState Shift, int X, int Y);
  void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift, int X,
  int Y);
  void __fastcall FormMouseUp(TObject *Sender, TMouseButton Button,
  TShiftState Shift, int X, int Y);
private: // User declarations
public:  // User declarations

  __fastcallTForm1(TComponent* Owner);
  bool Drawing;  //field to track whether button was pressed
  TPoint Origin, MovePt;  // fields to store points
  TDrawingTool DrawingTool;  // field to hold current tool

How to change the drawing tools?

Each drawing tool needs an associated OnClick event handler. Suppose your application had a toolbar button for each of four drawing tools: line, rectangle, ellipse, and rounded rectangle. You would attach the following event handlers to the OnClick events of the four drawing-tool buttons, setting DrawingTool to the appropriate value for each:

void __fastcallTForm1::LineButtonClick(TObject *Sender)  // LineButton
  DrawingTool = dtLine;

void __fastcallTForm1::RectangleButtonClick(TObject *Sender) // RectangleButton
  DrawingTool = dtRectangle;

void __fastcallTForm1::EllipseButtonClick(TObject *Sender)  // EllipseButton
  DrawingTool = dtEllipse;

void __fastcallTForm1::RoundedRectButtonClick(TObject *Sender) // RoundRectBtn
  DrawingTool = dtRoundRect;

How to use the drawing tools?

Add comment

Security code

Programming - C++Builder