Visual C#. NET 2003 Unleashed
|
The next section of this chapter deals with the concept of shaped forms. A shaped form is a kind of OwnerDraw control that enables you to provide an advanced (and attractive) interface that doesn't conform to the standard Windows rectangle form factor. Introduction to Shaped Forms
A shaped form is essentially just a Windows Form that doesn't appear to be rectangular. There are actually two ways in which you can create a shaped form using Windows Forms: an easy way and a hard way. I will cover the easy way here and let you do some experimenting to play with the hard way. The hard way involves using the GraphicsPath class to physically change the clipping region of your form at runtime by overriding some of your forms events (essentially making it an OwnerDraw). The easy way gives you the most creativity and gets you up and running as soon as possible. It is, however, not quite as powerful or flexible as the hard way, so you will have to decide for yourself which method you need. Most people's needs can be met with the method shown next. Creating a Sample Shaped Form
Building shaped forms relies on the ability for a Windows Form to set its TRansparencyKey. This property basically tells .NET that anytime the color indicated by the transparencyKey shows up on the form, it should be treated as transparent, and whatever is underneath the window in that position will show through. Many people create odd-shaped forms by opening up their favorite paint program and drawing the surface of the form. The area outside the form that they don't want to show up is painted a different (usually an extremely bright and unusual) color. Then the transparency key of the form is set to the color of the outside area of the image. With that in place, all they do is set the background image of the form to the image with the bright color, and the form appears to be a custom shape. There are some minor details to concern yourself with, but that process is basically all you have to do. To get started, create a new Windows Forms application called ShapedForms. To set up the background, set the background image of the form to an image you have created. Create the shape that you want your form to be, fill in the surrounding area with a bright color, and remember that color. The background image of your form will tile and repeat by default, so you need to shrink your form so that it reveals only one of your images and you don't see the extra tiles. Now set the TRansparencyKey property of the form to the same color as the one you used to fill the "hidden" area of your shaped form. I can't draw with a mouse, so I borrowed a news container panel from my friends at WarCry.com and filled in the transparent area with white. TIP Before you run the application, make sure to set the FormBorderStyle to None. If you don't set the FormBorderStyle to None, Windows will wrap your shaped form in a big square box equipped with a title bar and any other controls you have specified, shattering the illusion of a form shape. Remember that shaped forms like the ones in this chapter rely on transparency and borderless forms.
If you run the application now, you will see your nice, fancy shaped form. However, there are two problems: You can't close the form and you can't move the form because you don't have a title bar. To close the form, you can add a simple button to the form that closes it, or you can add another clickable image to do that if you want to make the Close button fit in with the style of your form. To make the form move, you need to trap the MouseDown and MouseMove events so that you can allow the form to move when the user holds down the left mouse button anywhere on the form. MSDN comes to the rescue with the code in Listing 17.7 that illustrates these two event handlers. Listing 17.7. The MouseDown and MouseMove Event Handlers
private void button1_Click(object sender, System.EventArgs e) { this.Close(); } private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { mouse_Offset = new Point(-e.X, -e.Y); } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point mousePos = Control.MousePosition; mousePos.Offset(mouse_Offset.X, mouse_Offset.Y); Location = mousePos; } } This code tracks the position of the mouse when it was clicked. Whenever the mouse moves, the location of the form is then moved to the location of the mouse, offset so that the form doesn't jerk the top-left corner of the form to the mouse location. When it's all done and set to go, you might end up with a form that looks something like the one shown in Figure 17.5. I've moved the form into position over a window so that you can see the transparency effect and how the form appears to be rounded in multiple places. Figure 17.5. The shaped form with the transparency effect in action.
|
|