Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / Visual Basic

How to create a transparent control in VB 2008

Rate me:
Please Sign up or sign in to vote.
2.53/5 (8 votes)
12 Apr 2009CPOL 49.3K   21   18
How to create a transparent control in VB 2008

Introduction

This article provides the code required to implement control transparency in VB 2008.

Background

The transparency option in VB 2008 is not as useful as it could be because the Transparency setting causes the application to hide all of the graphics under the control, all the way through to the form background.

Using the code

The code provided below can be copied directly into your project. It works by using the “CopyFromScreen” method of the Graphics object to copy a snapshot of the screen area directly behind the given control into a bitmap. This bitmap is then assigned as the BackImage of the control.

VB
Public Sub SetBackgroundTransparent(ByRef theCtl As Control)

    'This routine makes a control appear to be transparent
    'Transparency in VB2008 is implemented in a bit of an odd way. 
    'When the backcolor is set to Transparent, the control 
    'erases everything behind it except the form
    'so if you have a number of layered controls, 
    'you appear to see a "hole" in the display
    'This routine works by taking a snapshot of the area behind 
    'the control and copying this as a bitmap to the
    'backimage of the control.
    'The control isn't truly transparent, but it appears to be.

    'Incoming variable:  the control to be made transparent

    Dim intSourceX As Integer
    Dim intSourceY As Integer
    Dim intBorderWidth As Integer
    Dim intTitleHeight As Integer
    Dim bmp As Bitmap
    Dim MyGraphics As Graphics

    'get the X and Y corodinates of the control, 
    'allowing for the border and titlebar dimensions
    intBorderWidth = (Me.Width - Me.ClientRectangle.Width) / 2
    intTitleHeight = ((Me.Height - (2 * intBorderWidth)) - _
                       Me.ClientRectangle.Height)

    intSourceX = Me.Left + intBorderWidth + theCtl.Left

    intSourceY = Me.Top + intTitleHeight + intBorderWidth + theCtl.Top

    'Hide the control otherwise we end up getting a snapshot of the control
    theCtl.Visible = False

    'Give the UI some time to hide the control
    Application.DoEvents()

    'Take a snapshot of the screen in the region behind the control 
    bmp = New Bitmap(theCtl.Width, theCtl.Height)
    MyGraphics = Graphics.FromImage(bmp)
    MyGraphics.CopyFromScreen(intSourceX, intSourceY, 0, 0, bmp.Size)
    MyGraphics.Dispose()

    'use this clip as a the back image of the label
    theCtl.BackgroundImageLayout = ImageLayout.None
    theCtl.BackgroundImage = bmp
    'show the control with its new backimage
    theCtl.Visible = True
    'Give the UI some time to show the control
    Application.DoEvents()

End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCopyFromScreen Pin
Nuha Toma17-Sep-10 13:12
Nuha Toma17-Sep-10 13:12 
GeneralGreat Works Pin
Аslam Iqbal28-Apr-10 4:09
professionalАslam Iqbal28-Apr-10 4:09 
GeneralMy vote of 1 Pin
DanWalker9-Oct-09 9:55
DanWalker9-Oct-09 9:55 
GeneralMy vote of 2 Pin
Zaegra21-Apr-09 20:21
Zaegra21-Apr-09 20:21 
GeneralRe: My vote of 2 Pin
Puzzler9923-Apr-09 17:45
Puzzler9923-Apr-09 17:45 
GeneralRe: My vote of 2 Pin
a codeproject fan21-Sep-09 19:22
a codeproject fan21-Sep-09 19:22 
GeneralMy vote of 1 Pin
Dean Moe15-Apr-09 17:01
Dean Moe15-Apr-09 17:01 
GeneralRe: My vote of 1 Pin
Puzzler9917-Apr-09 6:37
Puzzler9917-Apr-09 6:37 
AnswerRe: My vote of 1 [modified] Pin
Dean Moe17-Apr-09 12:37
Dean Moe17-Apr-09 12:37 
GeneralYour explanation of Color.Transparent... Pin
Dave Kreskowiak14-Apr-09 9:02
mveDave Kreskowiak14-Apr-09 9:02 
GeneralRe: Your explanation of Color.Transparent... Pin
Puzzler9917-Apr-09 6:47
Puzzler9917-Apr-09 6:47 
GeneralRe: Your explanation of Color.Transparent... Pin
Dave Kreskowiak17-Apr-09 10:57
mveDave Kreskowiak17-Apr-09 10:57 
GeneralYou are a god Pin
Daniele Di Sarli12-Apr-09 4:10
Daniele Di Sarli12-Apr-09 4:10 
GeneralRe: You are a god Pin
Puzzler9917-Apr-09 6:42
Puzzler9917-Apr-09 6:42 
GeneralToo short Pin
Jouke van der Maas12-Apr-09 3:22
Jouke van der Maas12-Apr-09 3:22 
GeneralRe: Too short Pin
Dean Moe13-Apr-09 15:36
Dean Moe13-Apr-09 15:36 
GeneralRe: Too short Pin
Jouke van der Maas14-Apr-09 4:49
Jouke van der Maas14-Apr-09 4:49 
GeneralRe: Too short [modified] Pin
Puzzler9917-Apr-09 6:44
Puzzler9917-Apr-09 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.