Keeping Track of Which Drawing Tool to Use

From RAD Studio
Jump to: navigation, search

Go Up to Handling Multiple Drawing Objects in Your Application


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:

type
  TDrawingTool = (dtLine, dtRectangle, dtEllipse, dtRoundRect);
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:

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:

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

TForm1 = class(TForm)
 { Method declarations here }
public
  Drawing: Boolean;
  Origin, MovePt: TPoint;
  DrawingTool: TDrawingTool;{ field to hold current tool }
end;
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: // User declarations

public: // User declarations
	__fastcall TForm1(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
};

See Also