Browse DevX
Sign up for e-mail newsletters from DevX


Shaped .NET Windows Forms : Page 2

.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

Create an Elliptical Form
For my first example I'm going to create an elliptical form. I'll start out by creating a new Visual Basic .NET Windows Form project. Visual Studio .NET will create a default rectangular form. An elliptical form requires a simple graphics path that you can create with a single call to the AddEllipse() method of a GraphicsPath object. GraphicsPaths have a number of methods that you can use to add additional segments to the vector-based definition of the path. I'll show you more advanced uses of that later in this article. For now, however, all you want is a single ellipse that defines the entire path. This example shows you how to create the path and how to add a small ellipse:

Dim oPath As New GraphicsPath() oPath.AddEllipse(0, 0, 200, 100)

This defines an ellipse starting at position 0,0 (the top-left corner of the form or drawing surface), and has a width and height of 200 and 100 pixels respectively.

Note that you need to import the System.Drawing.Drawing2D namespace in order to compile this code snippet successfully:

Imports System.Drawing.Drawing2D

You do not need to add a project reference to this assembly because all Windows Forms applications automatically reference this namespace.

There are several overloads for the AddEllipse() method. One of them allows you to pass a Rectangle object rather than individual coordinates. This is handy if you want to add an ellipse of the same size as the form since each Windows Form has a ClientRectangle member:

Dim oPath As New GraphicsPath() oPath.AddEllipse(Me.ClientRectangle)

You can use this path, turn it into a Region, and assign it to the form. The following example utilizes the form's Load event:

Private Sub Form1_Load( ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim oPath As New GraphicsPath() oPath.AddEllipse(Me.ClientRectangle) Me.Region = New Region(oPath) End Sub

Before I proceed I would like to point out that the Visual Basic .NET IntelliSense feature will not show the Region or the ClientRectangle members of the form. This is an IDE limitation and you'll find that the code above compiles without a problem.

DevX Note: You can see the Region and ClientRectangle members in VB.NET if you change your options. To do that, select Options from Visual Studio's Tools menu, and then open the Text Editor item. Select All Languages (or just Basic if you want to limit the effects to VB.NET) and uncheck the "Hide advanced members" checkbox. Click OK to close the Options dialog. The change takes effect immediately.

The C# IntelliSense, on the other hand, exposes these members as expected. Beyond that, however, the differences between the two languages are minimal. Consider the following example, which is the C# version of the simple elliptical form:

private void Form1_Load(object sender, System.EventArgs e) { GraphicsPath oPath = new GraphicsPath(); oPath.AddEllipse(this.ClientRectangle); this.Region = new Region(oPath); }

Figure 1: Our first example: an elliptical form.
Since the differences between the two languages are so insignificant in this case, I will stick to VB.NET examples in this article. I am sure the C# developers among you will have no problem figuring out your version of the code.

Now that you've assigned the region to the form in the Load event, you can run the application. Figure 1 shows the result. (I added a "Close" button.) One of the first things you notice is that the form looks somewhat "odd." Like every other Windows form, it has a title bar. However, the ellipse slices right through that, cutting it off at both ends. That's not particularly professional.

Figure 2: During design time, all shaped forms appear rectangular.
Another detail you may have noticed is that the form appears rectangular during design time (see Figure 2). That's normal. It makes positioning controls on a shaped form a bit tricky, but beyond that you can design and program your form like any other form including setting the size, dropping controls, and wiring up event code.

Let's fix the title bar problem. In general, most shaped forms don't have a title bar drawn by the operating system. Some shaped forms have fancy, twisted title bars. Those are usually created by a graphic artist and placed on the form as a bitmap. Some applications can run in different modes where the title bar is hidden when the form runs in shaped mode, but it is visible in a more conventional rectangular mode. Windows Media Player is a good example for that (see Figure 3 and Figure 4).

Figure 3: Windows Media Player running in shaped mode.
Figure 4: Windows Media Player in "rectangular mode". You can clearly see that the shaped version is a sub-set that is cut out of the rectangular window.

As you can see in this instance, shaped forms are usually "cut out" of the larger window below the title bar. You can do the same thing using the elliptical form. The following code snipped adjusts for the size of the title bar as well as window borders:

' Calculate the dimensions of the ellipse Dim iElTop, iElLeft, iElHeight, iElWidth _ As Integer iElTop = SystemInformation.BorderSize.Height + _ SystemInformation.CaptionHeight + 2 iElLeft = SystemInformation.BorderSize.Width + 2 iElHeight = Height - iElTop - _ SystemInformation.BorderSize.Height - 3 iElWidth = Me.Width - iElLeft - _ SystemInformation.BorderSize.Width - 3 ' Create the graphics path and ' set its dimensions oPath = New GraphicsPath() oPath.AddEllipse(iElLeft, iElTop, _ iElWidth, iElHeight)

The code used here looks a bit more sophisticated than it really is. Most of the code deals with using the SystemInformation class (and its static methods) to query information such as the height of the title bar.

If you run this code you will see a form that is still elliptical but slightly smaller than the previous version. It looks better—still not very good—but better, since the title bar is no longer visible. This introduces another problem though: You cannot move the form anymore because the only way in Windows to move a form is by grabbing it on the title bar.

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