Drawing Program with Controls

The basic application (shown in Figure 24-1) allows the user to create rectangles, ellipses, or triangles of any color, and then resize them or drag them around the form to a new location. Rather than coding all the logic to manage the hit testing, selection, and layering, you can make use of a convenient shortcut by turning each shape into a custom control. Because each control has its own built-in smarts for handling user interaction like mouse clicks and key presses, this approach simplifies your life considerably.

Figure 24-1. A vector-based drawing application

The basic concept in this application is to let the shapes draw themselves. Essentially, each shape is an owner-drawn control that paints its surface. The support for dragging, resizing, and changing shape colors is built into the form code, but it's not terribly difficult to implement. Really, all the form needs to do is react to the user's mouse actions, and set properties like Location and Size accordingly. It's easy to react to an event when a specific shape is clicked, because the shape controls inherit all the basic mouse notification events from the Control class.

Using this application, you could easily add more shape types and enhance the drawing functionality with additional features. You'll see some of these enhancements in the revised, non-control-based version of this application.

+1 0

Post a comment