Recipe 4.17. Creating a Nonrectangular Form


Problem

You want to display a form that is nonrectangular; that is, you want some of the form to be invisible.

Solution

Sample code folder: Chapter 04\PartialInvisibility

Use the form's transparencyKey property to identify a color that will be invisible. The sample code in this recipe uses fuchsia for its "invisible color," but you can choose any color that meets your display requirements.

Create a new Windows Forms application. Change Form1's FormBorderStyle property to None, its StartPosition property to CenterScreen, and its transparencyKey property to Fuchsia. Then add the following source code to the form's code template:

 Private Sub Form1_Click(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Click    ' ----- Any click closes the form.    Me.Close() End Sub Private Sub Form1_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles Me.Paint    ' ----- Draw a nice logo form.    e.Graphics.Clear(Color.Fuchsia)    e.Graphics.FillRectangle(Brushes.Gold, 0.0F, _       Me.ClientRectangle.Height / 3.0F, _       CSng(Me.ClientRectangle.Width), _       Me.ClientRectangle.Height / 3.0F)    e.Graphics.FillPolygon(Brushes.Gold, New PointF() { _       New Point(Me.ClientRectangle.Width / 4, 0), _       New Point(Me.ClientRectangle.Width / 2, _       Me.ClientRectangle.Height / 2), _       New Point(Me.ClientRectangle.Width / 4, _       Me.ClientRectangle.Height), _       New Point(0, Me.ClientRectangle.Height / 2)})    Dim largerFont = New Font(Me.Font.Name, 20)    e.Graphics.DrawString("My Nice Program", _       largerFont, Brushes.Black, 20, _       (Me.ClientRectangle.Height / 2) - _       (largerFont.Height / 2))    End Sub 

When you run the program, it appears similar to the display in Figure 4-18. (We left the development environment behind the form so that you could see the invisibility.)

Figure 4-18. A form with transparent portions


Discussion

The initial release of Visual Basic 2005 included a bug that prevented the transparency color from properly appearing as transparent in some cases. Specifically, if your form included an image that contained the transparency color, and the workstation was using more than 24 bits of color for its display, the image appeared as opaque. To get around this problem, you need to set transparency on the image manually before you draw it:

 Private Sub Form1_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles Me.Paint     ' ----- This code assumes that the form's     '       TransparencyKey property is "Fuchsia".     Dim logoImage As Bitmap = Bitmap.FromFile( _        "C:\MyLogo.bmp")     logoImage.MakeTransparent(Color.Fuchsia)     e.Graphics.DrawImage(logoImage, 0, 0) End Sub 

The Microsoft Knowledge Base number for this article is 822495.

See Also

Recipe 9.10 discusses invisibility colors and the transparencyKey property in more detail.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

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