Advertisment

Rich Win Apps with WinForms

author-image
PCQ Bureau
New Update

Windows Forms or WinForms are part of .NET Framework and allow you to create rich Windows applications and graphics. After you have installed .NET Framework SDK (available in last month’s second PCQuest CD, look under the “X:\Beginner\Framework SDK” folder), fire up the documentation and browse to .NET Framework SDK>Building Applications>Creating Windows Forms Applications, to get started.

Advertisment

You can do all the conventional things that you do in VB or C++ (Win32, MFC) today with WinForms. This articles tries to cover some of the advanced things you can do with WinForms. Most of the things mentioned in this article, rely on advanced features of the OS. So they require Win 2000/XP to work. 

Partially transparent windows



The System.Windows.Forms.Form class has a property called ‘Opacity’ that allows you to control the degree of transparency of a window. It takes values from 0 to 1, with 1 being fully opaque (default). So to make it 50% transparent, just set the value of opacity to 0.5. Here is how a partially transparent window looks like:

Partially transparent ghost 



If you use VS.NET, you can set the opacity from the properties window itself. If you use Visual Notepad (Notepad used for writing Windows Forms applications), just use code one on CD).

Advertisment

Save the code as “Transparency.cs” and fire up the C# compiler on command line:

C:\Projects\> csc /Target:WinExe Transparency.cs

Remember to put the /Target switch which tells the C# compiler to output a windows application. Without this switch you will end up with an application that always spawns a console window while running. Also keep in mind that this feature is only supported under Win 2000 onwards. On Win 98, the opacity setting is completely ignored and you get a full opaque window.

Advertisment

Transparent regions



WinForms also allow you to make a selected region in a Window ‘fully transparent,’ that is, a region that is not rendered at all by the OS. Clicking in a ‘fully transparent’ region in the window, will yield focus to the background window. In simpler terms — you could create a window with “holes”… Lets see how it is done…

The king is actually

a skinned window

The Form class has another interesting property, called the “TransparencyKey”. Basically, it allows you to define a color which is not painted at all. Any thing drawn on the form with same color as TransparencyKey will be rendered “transparent”.

Advertisment

You will not receive any click events in the transparent window and the focus will be transferred to the window underneath it.

Here is code snippet that defines blue as a transparent color and then draws a filled rectangle in blue color in the OnPaint event handler. This renders the region enclosed by the rectangle as transparent. (see screenshot below left).

Window with a window 



Copy ‘code 2’ from Win_Code.txt on CD to get a code snippet that allows you to do this. 

Advertisment

Skins



TransparencyKey can be exploited to do some interesting things. One of the “in” things in GUI applications these days is the ability to allow users to fully customize the UI using ‘skins.’ A Skin is a bitmap (or a series of bitmaps) that define an application’s visual appearance. A lot of popular applications support ‘skinning’ including Windows Media Player and WinAmp. While some applications just allow you to skin the basic UI elements–toolbars, scrollbars and buttons etc, others go to the extent of allowing you define shape of an application’s interface. The latter can be done with ease using TransparencyKey. To achieve “skinning” you first create a bitmap which will define the look and feel of an application. You then set the TransparencyKey to the color in the bitmap that you want as transparent (typically the color of the background of the bitmap). 

Here, the code snippet

defines blue as a transparent colour and then draws a filled rectangle in blue color

Fully skinned window



You can set any bitmap as a skin — you are only limited by your imagination — if you ray-trace you can actually create your own fancy collection of skins…! Few additional notes about skinning — TransparencyKey is supported only on Windows 2000 and above. Also TransparencyKey is an easier way of doing what you could do with Region Windows under Win32.

Advertisment

For a window to look fully skinned, you need to create a form with no border or title bar. This can be done by setting the FormBorderStyle property of the Form to None. Here is a code snippet that illustrates skinning (see code 3 on CD):

So far so good, but when you run the application we just created you will run into a practical difficulty. Since the form we have created doesn’t have any title bar, you cannot move it around, which is not very user friendly to say the least. We can work around this problem in two ways. One way is to trap the MouseDown event and then move the form each time we catch the MouseMove event. The second way is to use an old trick from Win32 programming days and sends a WM_NCLBUTTONDOWN message to the form everytime we encounter the MouseDown event. To do this we’ll need to fall back on the Win32 API itself and use Platform Invoke (P-Invoke) to make the API calls. Lets see how it is done…

Faux Messenger window complete

with a gradient background

Advertisment

First we make two declarations — one for the Win32 SendMessage function and the other for the Win32 ReleaseCapture function (look at the Win32 API documentation for details on these functions) and declare the WM_NCLBUTTONDOWN message and HTCAPTION constants. We’ll also need to use System.Runtime.InteropServices namespace (see code 4 on CD): 

The next step is to call these functions in the MouseDown even. See code 5 on CD for this.

Now try to click anywhere in the skinned window, and try to move (“drag”) it around while holding the mouse down — it moves just like it would have with a title bar. 

As you can see that though WinForms does an excellent job of encapsulating the Win32 API for you, you can always call the underlying Win32 API easily if at all you need to.

GDI+ Gradients



One of the great things about .NET and WinForms is that allows you to tap into the rich GDI+ functionality without any significant effort. Again, if you have done Windows programming before you must be aware of concepts like Device Contexts, Pens and Brushes. One of the interesting Brush types that you get with GDI+ is a Gradient Brush. A Gradient Brush allows you to paint an area in two or more colors–the color changes, within the painted region, from one color to other very gradually. There are two types of Gradient Brushes–the linear and path. Let’s see linear Gradient Brush in action. In the code that we are going to try next, we try to create a window, whose background is gradient painted–and looks something similar to the MSN Messenger popup message window. 

To get the “authentic” thick edged look of the Messenger popup, we’ll not only need to create a borderless window, but also override the CreateParams method to specify some additional window attributes (those of you who have used MFC before might get déjà vu). Here is how it’ done (see code 6):

The code above is in addition to the code that goes in the Form’s constructor. Where we set the FormBorderStyle to FormBorderStyle.None and set the BackColor to White.

The next step is to place the window correctly–right above the system tray area–we do this in the OnActivated method (see code 7). This gives us our Messenger look alike–though sans the gradient background.

Ghost messenger window



Next step is to gradient paint our window. This is done in the OnPaint event. We first create a LinearBackgroundColor with two colors–White and Sky Blue. We then draw a gradient filled rectangle with same dimensions as that of the window using this gradient brush. We’ll let the code (Code 8 on CD) do the talking here.

Faux messenger window



You need to press Alt + F4 to close this window. You could write code to automatically close the window after a few seconds. Also the window comes on screen rather abruptly. You can also write code to gradually ‘pop-up’ the window like messenger does. This is actually quite simple–just look at the documentation for System.Windows. Forms.Timer class.



So we’ve see that Windows Forms provide the ease .NET framework with the power and flexibility of conventional Win32 APIs. With a little imagination — and even lesser code - you can create innovative and rich windows applications. 

Deepak Gulati, India Communities Specialist, Microsoft

Advertisment