7.9 Changing Form Opacity

 <  Day Day Up  >  

You want to change the opacity of a form to make it more transparent when it appears.


Technique

Set the Opacity property of a Windows Form to a value between 0% and 100%. At 100% opacity, the Windows Form is entirely opaque , but at 0%, the form is completely transparent. When programmatically changing the opacity of a form, you must use a double data type between 0.0 and 1.0:

 
 private void Form1_MouseEnter(object sender, System.EventArgs e) {     this.Opacity = 1; } private void Form1_MouseLeave(object sender, System.EventArgs e) {     this.Opacity = 0.80; } 

If you only want portions of the form to be transparent, use the TransparencyKey property. You define this property using a color. If any portion of the form matches that color , it will be transparent. Figure 7.6 shows a form with 80% opacity. We placed a Panel control on the form and set its background color to a specific color. This color is also the form's TransparencyKey , which means the form is transparent where the Panel control is located.

Figure 7.6. A Windows Form with 80% opacity and a Panel control whose background color is the same as the form's TransparencyKey property.

graphics/07fig06.jpg

Comments

When Windows 2000 was released, a big user -interface innovation was the ability to create transparent or layered windows. The Opacity property of a Windows Form controls how opaque or transparent a window is. At 100%, the form appears in its default state, which means there are no transparent areas on the form. At 0%, the form is entirely transparent, which also means that you cannot interact with the form at all.

You can also create transparent regions within your form by using the TransparencyKey property. Although the Opacity property uses a numerical value to control how transparent or opaque a form is, the TransparencyKey property uses a Color value. When the form encounters that color, it does not draw the corresponding pixel. You might have seen some applications that use bitmaps to display their user interfaces, most notably several types of media players. You can easily create this type of user interface by creating a bitmap with certain portions set to a predefined color you want to use as the transparency key. Next, change the BackgroundImage property on the Windows Form to the bitmap file you created. Finally, change the TransparencyKey property of the Windows Form to the same color as the color you want to be transparent in the background bitmap. You might also want to remove the form's title bar by setting the FormBorderStyle to none. Figure 7.7 shows a Windows Form that uses a background bitmap and transparency key to create an irregularly shaped Windows Form.

Figure 7.7. You can create irregular shaped Windows Forms using a bitmap and transparency key.

graphics/07fig07.jpg

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net