Reader Level:
ARTICLE

Drawing Transparent Controls in GDI+

On August 04, 2010 Articles | GDI+ & Graphics
In this article you will learn how you can draw transparent controls.
  • 0
  • 0
  • 5748

This article has been excerpted from book "Graphics Programming with GDI+"

How can I draw transparent controls? This is one of the commonly asked questions on discussion forums.

Drawing transparent controls involves two steps. First we set a form's style to enable support for transparent controls. We do this by calling the SetStyle method of the form, passing ControlStyles.SupportTransparentBackColor as the first argument, and setting the second argument (which in turn sets the SupportTransparentBackColor bit) to true. Next we set the Control's BackColor property to a transparent color. Either we can use Color.Transparent, or we can create a Color object using an alpha component value less than 255 to provide custom semitransparent.

Listing 15.4 sets the background color of controls to transparent.

LISTING 15.4: Setting the background color of controls to transparent

            /* Code for transparent controls */
            this.SetStyle(
            ControlStyles.SupportTransparentBackColor, true);
            button1.BackColor = Color.Transparent;
            btnBrowse.BackColor = Color.Transparent;
            btnClose.BackColor = Color.Transparent;
            panel1.BackColor = Color.FromArgb(70, 0, 0, 255);


The output of Listing 15.4 looks like Figure 15.2

NOTE

Not all control support transparent color. For example, if you set the BackColor property of text box to Color.Transparent, you will get an exception.

Figure 15.2.jpg

FIGURE 15.2: Designing transparent controls

book.gif

 

Dinesh Beniwal

 I am working as VP Content Manager, responsible for content publishing, content development, and social relation. You can follow me on twitter @dbeniwal21

  • 4rank
  • 4M readers
COMMENT USING

Trending up