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.
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.
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.