RAD Studio |  |

Keeping Track of Which Drawing Tool to Use
See Also
Collapse All

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.

You could assign numbers to each kind of tool, but then you would have to remember what each number stands for. You can do that more easily by assigning mnemonic constant names to each number, but your code won"t be able to distinguish which numbers are in the proper range and of the right type. Fortunately, Delphi provides a means to handle both of these shortcomings. You can declare an enumerated type.

An enumerated type is really just a shorthand way of assigning sequential values to constants. Since it's also a type declaration, you can use Delphi's type-checking to ensure that you assign only those specific values.

To declare an enumerated type, use the reserved work type, followed by an identifier for the type, then an equal sign, and the identifiers for the values in the type in parentheses, separated by commas.

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

Copy Code

    type
    TDrawingTool = (dtLine, dtRectangle, dtEllipse, dtRoundRect);

Copy Code

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

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").

The declaration of the TDrawingTool type is equivalent to declaring a group of constants:

Copy Code

    const
      dtLine = 0;
      dtRectangle = 1;
      dtEllipse = 2;
      dtRoundRect = 3;

The main difference is that by declaring the enumerated type, you give the constants not just a value, but also a type, which enables you to use the Delphi language's type-checking to prevent many errors. A variable of type TDrawingTool can be assigned only one of the constants dtLine..dtRoundRect. Attempting to assign some other number (even one in the range 0..3) generates a compile-time error.

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

Copy Code

    type
    TDrawingTool = (dtLine, dtRectangle, dtEllipse, dtRoundRect);
    TForm1 = class(TForm)
    ...{ method declarations }
    public
    Drawing: Boolean;
    Origin, MovePt: TPoint;
    DrawingTool: TDrawingTool;{ field to hold current tool }
    end;

Copy Code

    enum TDrawingTool {dtLine, dtRectangle, dtEllipse, dtRoundRect};
    class TForm1 : public TForm
    {
    __published:  // IDE-managed Components
      void __fastcall FormMouseDown(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:
    public:
      __fastcall TForm1(TComponent* Owner);
      bool Drawing;  //field to track whether button was pressed
      TDrawingTool DrawingTool;  // field to hold current tool
    };

See Also

Changing the Tool with Speed Buttons

Using Drawing Tools

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!