Click here to Skip to main content
Licence CPOL
First Posted 11 Jun 2008
Views 67,007
Downloads 4,124
Bookmarked 54 times

Making Transparent Controls with C# and .NET 3.5

By Nildo Soares de Araujo | 11 Jun 2008
This article provides an approach to a Transparent Control that draws an ellipse with real transparent background and a brush that supports transparent colors.

1

2
1 vote, 5.3%
3
4 votes, 21.1%
4
14 votes, 73.7%
5
4.55/5 - 19 votes
1 removed
μ 4.47, σa 1.04 [?]
TransparentControl.gif

Introduction

This article provides an approach to a Transparent Control that draws an ellipse with real transparent background and a brush that supports transparent colors.

Using the Code

A sample project TranspControlTest is included in the ZIP file. In addition, you will find a file that contains the source code for the TranspControl.dll. To obtain a background transparent you just need to change the Control style. You can do this by setting the Control style to opaque and changing the CreateParamExStyle property to WS_EX_TRANSPARENT. That’s all.

public TranspControl()
{
     SetStyle(ControlStyles.SupportsTransparentBackColor, true);
      SetStyle(ControlStyles.Opaque, true);
      this.BackColor = Color.Transparent;
}

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x20;
        return cp;
}
}

The included source code and the application example will show what you can do with this transparent control

The control has the following basic properties to be considered:

  1. Opacity: The opacity controls the Control transparency. This affects the background and the fill color. This ranges between 1 and 100.
  2. Background color: Here you select the background color. Full background transparency you will get by selecting the color to Transparent color.
  3. Fill color: Here you will select the color to fill your drawing. Full transparency you will get by selecting the color to Transparent color.
  4. Fore color: This wills the color to draw the contour.

This solution works fine in static applications. It is not recommended for using as an animated control because it will flick during the Form refreshing. If you intend to use it for drawing of lines, shapes, frames, or text then you made the right choice, even so in animated applications.

No-flicking Approach

For animated control applications with filled shapes and transparent background I recommend to use this second solution. See the below image. There is an example of animation using both approaches. Execute the test program and see the animation.

ControlTests_.gif

I made a Circle component to demonstrate how to obtain the no-flicking shape with full transparent background. Please see the included Circle.dll source file. The code is written in a clear manner and speaks for itself.

To obtain full background transparency just changes the Circle background color property to Transparent color.

Hope this can be helpful.

License

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

About the Author

Nildo Soares de Araujo

CEO
Digital Automation Ltda
Brazil Brazil

Member
Nildo Soares de Araujo
C# enthusiast. Control Systems engineer with expertise in industrial automation. Developed a complete library of Control Systems components such as PID, Integral Control, n-order Filters and many others functions written in SCL language.

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
QuestionGetting problem while setting transparent backcolor to Picturebox Pinmembervinay_K4:40 2 Feb '12  
Questionhow to transparent tow picturebox! Pinmembermahboobeh mohamadi22:14 20 Oct '10  
AnswerRe: how to transparent tow picturebox! PinmemberNildo Soares de Araujo8:10 25 Oct '10  
GeneralHELP PLEASE PingroupGoor10:26 6 Dec '09  
GeneralRe: HELP PLEASE [modified] PinmemberNildo Soares de Araujo17:14 15 Apr '10  
GeneralTranspControl - Rev. 1 PinmemberNildo Soares de Araujo6:12 25 Feb '09  
QuestionHow to show opaque background in case of animated transparent control? Pinmembervaibhav_gawali23:59 18 Feb '09  
AnswerRe: How to show opaque background in case of animated transparent control? PinmemberNildo Soares de Araujo3:25 19 Feb '09  
GeneralRe: How to show opaque background in case of animated transparent control? Pinmembervaibhav_gawali1:44 20 Feb '09  
GeneralRe: How to show opaque background in case of animated transparent control? PinmemberNildo Soares de Araujo10:08 20 Feb '09  
QuestionTransparent Control over WebBrowser PinmemberAdilson Carvalho7:46 12 Feb '09  
AnswerRe: Transparent Control over WebBrowser PinmemberNildo Soares de Araujo5:44 13 Feb '09  
Generalinvisible control when running video PinmemberMember 7885707:16 4 Feb '09  
GeneralRe: invisible control when running video PinmemberNildo Soares de Araujo4:20 8 Feb '09  
GeneralRe: invisible control when running video Pinmemberlazartefederico6:28 20 Apr '10  
GeneralRe: invisible control when running video PinmemberNildo Soares de Araujo9:28 21 Apr '10  
GeneralQuestion PinmemberYves7:09 20 Nov '08  
GeneralRe: Question PinmemberNildo Soares de Araujo13:22 20 Nov '08  
GeneralRe: Question PinmemberMember 78857019:07 8 Feb '09  
GeneralRe: Question PinmemberNildo Soares de Araujo6:22 9 Feb '09  
GeneralRe: Question PinmemberMember 7885706:14 10 Feb '09  
GeneralRe: Question PinmemberNildo Soares de Araujo8:40 10 Feb '09  
GeneralRe: Question PinmemberMember 415756123:45 20 Mar '09  
GeneralRe: Question PinmemberNildo Soares de Araujo10:28 25 Mar '09  
GeneralI used to do PLC and all that controls systems stuff, then I found C# PinmvpSacha Barber22:55 12 Jun '08  

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
Web02 | 2.5.120209.1 | Last Updated 11 Jun 2008
Article Copyright 2008 by Nildo Soares de Araujo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid