| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> Create Forms in Any Shape! |
![]() | Visual Basic |
Create Forms in Any Shape!Oct 20 2007 Give Your VB.NET Application a Unique "Look and Feel"VB.NET makes it easy to create an actual form, one that can contain buttons and other components, in just about any shape you can imagine. But there are a few 'conditions' that you have to understand. In this article, two different programs to create a form in the shape of an irregular star will be coded. The first has a plain background and the second uses a background based on your favorite photo. The basic method is as follows:
For the first program, I've decided to start with the complete code. We'll discuss some of the interesting side effects after that. To get started, create a standard Windows Application and set FormBorderStyle to None. (I used the name StarForm for my form.) Set the TransparencyKey to an obscure color. (I used 191, 1, 191.) Add a Button component to the form. (In the program below, it's called CloseTheForm.) Public Class StarForm When you run this program, you get a result like the one shown below. I've displayed the form on top of the program source so you can see the effect better. -------- You'll need a way to close the form because setting FormBorderStyle to None eliminates the control buttons that you might normally use. Without a Button component (or something else) to close the form, you would have to cancel the application to end it. And just one note on coding technique. Finding just the right X and Y coordinate values for all of the Point objects in the array to create the irregular star can be frustrating. The easy way is to code a Click event for the mouse to display them in the right format. Then run the application and then copy each value from the Immediate window into the GDI code. This code will display the X and Y coordinate values for each Point. Private Sub StarForm_MouseClick( _ The TransparencyKey is a color value that is ignored by Windows and just "doesn't exist" on the form. This means that you can click areas that are transparent and whatever is visible underneath will register the click. Because this is implemented at the operating system level, it's only supported starting with Windows 2000. Since you have sixteen million colors to choose from, it's not hard to pick a unique one. The way I did it was to pick purple and then adjust each color number by 1 to get 191, 1, 191. The Alpha value must be 255 (0 means full transparency and 255 means opaque). The color number of TransparencyKey must be unique because anything with that same color number will be considered to be transparent. To show this effect, I set the transparency key to KnownColor.ButtonFace (and adjusted the button so it will show up better). The illustration below shows that the button is transparent along with the rest of the form. -------- In fact, it's possible to have a phantom "floating component" where the actual form doesn't seem to be there at all. If you want "just a button" on your desktop, this is how to create it. Simply eliminate the code that draws the GDI+ graphic. Here's our application with a floating button on my Windows Vista desktop. Notice the button is floating on top of Windows Explorer. -------- The form works now, but there are still some problems. On the next page, we solve them. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


