Browse DevX
Sign up for e-mail newsletters from DevX


Shaped .NET Windows Forms : Page 3

.NET Windows Forms make it very easy to create forms that are non-rectangular by assigning a vector based outline as the region of the form. This, paired with appropriate graphics art, makes for very compelling form designs.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Making It Behave Right
Most shaped windows solve the movement problem by allowing the user to move the form by clicking anywhere on the form's background and dragging it to its new position. Listing 1 shows the implementation of that solution. Note that this code is useful for all shaped forms you might ever create. Therefore, I've created a new form class called ShapedForm, which all forms will subsequently inherit from in all the remaining examples in this article. I implemented the move functionality using the form's native mouse events. When the MouseDown event occurs on the form it sets a flag indicating that it's in "moving mode." At this point the form also records the mouse's location within the form. The MouseMove event checks whether the form is in "moving mode," and if it is, the form gets moved to the new mouse location. The original mouse position within the form has to be considered, otherwise, the form's top, left corner would jump to the mouse position. Once the MouseUp event occurs, "moving mode" gets turned off so future mouse move events will not move the form anymore.

The ShapedForm class not only implements the move behavior, but also provides standard functionality that allows you to create a simple path object by overriding the SetInitialFormShape() method. This method is expected to create the oFormPath member of the form (which is a GraphicsPath object). If that object is present, the form will automatically assume its shape. The form also remembers the previous shape (most likely the plain rectangular shape), and features methods to reset the form's shape to its previous appearance. Making It Look Good
At this point you have a shaped form that behaves the way you'd expect it to. However, the form still doesn't look very professional. One reason is that the form doesn't have a border. Unfortunately there is no easy way to add a border to a shaped form. Windows itself only renders borders for rectangular forms. And as with most shaped forms, drawing a border isn't easy in our elliptical example.

Figure 5 shows a new elliptical form with a blue background and a simple border rendered around it. The border is a very simple 3D effect. I kept things simple in order to keep the example easy to follow (and I'm a lousy artist). The border is rendered in the Paint event of the form. I use GDI+ to use an elliptical graphics path (similar to the path used to create the form, but slightly smaller) and have it render repeatedly in different colors. At the top left corner I start with a light blue shade. Then I use a dark blue pen to render the ellipse shifted several pixels to the bottom and to the right, and finally I render the ellipse in the center of the form using a medium-blue brush to create the desired form background. This results in a three-dimensional border effect. (You may have to look very closely at the image to actually see it). In a real form you should experiment with a number of different ellipses using different colors, and perhaps even paths that utilize a gradient fill (see the GDI+ sidebar).

Listing 2 shows the sample code that generates the form shown in Figure 5. This form is a subclass of our ShapedForm class in order to exhibit the behavior we programmed above. Note: Another way to graphically display a border is to provide it as part of a background image. In fact, most professional applications that use shaped forms seem to take that approach. I'll explore that possibility later.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date