5,693,062 members and growing! (19,466 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate License: The Code Project Open License (CPOL)

Making Transparent Controls with C# and .NET 3.5

By Nildo Soares de Araujo

This article provides an approach to a Transparent Control that draws an ellipse with real transparent background and a brush that supports transparent colors.
C#, Windows, .NET (.NET 3.5, .NET), GDI+, Dev

Posted: 11 Jun 2008
Updated: 11 Jun 2008
Views: 10,630
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 4.08 Rating: 4.28 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 11.1%
3
3 votes, 33.3%
4
5 votes, 55.6%
5
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


Nildo Soares de Araujo
C# enthusiast. Experienced 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.
Occupation: CEO
Company: Digital Automation Ltda
Location: Brazil Brazil

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralQuestionmemberYves7:09 20 Nov '08  
GeneralRe: QuestionmemberNildo Soares de Araujo13:22 20 Nov '08  
GeneralI used to do PLC and all that controls systems stuff, then I found C#mvpSacha Barber22:55 12 Jun '08  
GeneralRe: I used to do PLC and all that controls systems stuff, then I found C#memberMember 43244855:06 13 Jun '08  
GeneralRe: I used to do PLC and all that controls systems stuff, then I found C#mvpSacha Barber5:47 13 Jun '08  
GeneralTitlememberJean-Paul Mikkers11:05 12 Jun '08  
GeneralRe: TitlememberMember 43244853:45 13 Jun '08  
QuestionSeems goodmembervikas maan0:23 12 Jun '08  
AnswerRe: Seems goodmemberMember 43244856:34 12 Jun '08  
GeneralRe: Seems goodmemberMember 432448510:41 12 Jun '08  
GeneralRe: Seems goodmemberMember 43244854:24 13 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jun 2008
Editor: Sean Ewington
Copyright 2008 by Nildo Soares de Araujo
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project