Click here to Skip to main content
Licence CPOL
First Posted 12 Apr 2009
Views 18,655
Bookmarked 19 times

How to create a transparent control in VB 2008

By | 12 Apr 2009 | Article
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.

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)

About the Author

Puzzler99



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCopyFromScreen PinmemberNuha Toma13:12 17 Sep '10  
GeneralGreat Works Pinmemberkisukortechai4:09 28 Apr '10  
GeneralMy vote of 1 PinmemberBooGhost9:55 9 Oct '09  
GeneralMy vote of 2 PinmemberZaegra20:21 21 Apr '09  
GeneralRe: My vote of 2 PinmemberPuzzler9917:45 23 Apr '09  
GeneralRe: My vote of 2 Pinmembera codeproject fan19:22 21 Sep '09  
GeneralMy vote of 1 PinmemberDean Moe17:01 15 Apr '09  
GeneralRe: My vote of 1 Pinmemberunlistedemail24@gmail.com6:37 17 Apr '09  
AnswerRe: My vote of 1 [modified] PinmemberDean Moe12:37 17 Apr '09  
GeneralYour explanation of Color.Transparent... PinmvpDave Kreskowiak9:02 14 Apr '09  
GeneralRe: Your explanation of Color.Transparent... Pinmemberunlistedemail24@gmail.com6:47 17 Apr '09  
GeneralRe: Your explanation of Color.Transparent... PinmvpDave Kreskowiak10:57 17 Apr '09  
GeneralYou are a god PinmemberDaniele Di Sarli4:10 12 Apr '09  
GeneralRe: You are a god Pinmemberunlistedemail24@gmail.com6:42 17 Apr '09  
GeneralToo short PinmemberJouketje3:22 12 Apr '09  
GeneralRe: Too short PinmemberGandalf715:36 13 Apr '09  
GeneralRe: Too short PinmemberJouketje4:49 14 Apr '09  
GeneralRe: Too short [modified] Pinmemberunlistedemail24@gmail.com6:44 17 Apr '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Apr 2009
Article Copyright 2009 by Puzzler99
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid