Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

windows forms - transparence question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matthias S. - 28 Mar 2008 09:59 GMT
hi there,

i'm using .net framework 2.0 and i need a panel to be 50%
transparent/transluctant. i was desperately looking for an opacity
property with no luck. any ideas on how to roll your own?

thanks in advance!

matthias
--
Morten Wennevik [C# MVP] - 28 Mar 2008 12:26 GMT
> hi there,
>
[quoted text clipped - 5 lines]
>
> matthias

Hi Matthias,

Assembling various code bits from the net the I've created a
TransparentPanel class that may work for you.  

public class TransparentPanel : Panel
{
   private int _alfa;

   public int Alfa
   {
       get { return _alfa; }
       set { _alfa = value; }
   }

   public TransparentPanel()
   {
       SetStyle(ControlStyles.Opaque, true);
   }

   protected override CreateParams CreateParams
   {
       get
       {
           CreateParams createParams = base.CreateParams;
           createParams.ExStyle |= 0x00000020;
           return createParams;
       }
   }

   protected override void OnPaint(PaintEventArgs e)
   {
       using (SolidBrush brush = new SolidBrush(Color.FromArgb(Alfa,
BackColor)))
       {
           e.Graphics.FillRectangle(brush, ClientRectangle);
       }
   }
}

It does not appear to like being tweaked in the designerwindow much, but
when adding it manually it does display the background in a faded manner.

   public Form1()
   {
       InitializeComponent();

       Controls.Add(new TextBox());

       TransparentPanel p = new TransparentPanel();
       p.Alfa = 64;
       p.BackColor = Color.DarkOrange;
       Controls.Add(p);
       p.BringToFront();
   }

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Peter Duniho - 28 Mar 2008 18:54 GMT
> [...]
> It does not appear to like being tweaked in the designerwindow much, but
> when adding it manually it does display the background in a faded manner.

Try placing it on top of something else.  You'll find that while the  
"background" is painted with the alpha, it's not actually transparent.  :(

Unfortunately, I'm not aware of a way to do this using the existing Forms  
namespace.  Even with the BackColor property set to Transparent, it's not  
really transparent.  It just inherits the parent's BackColor.  And Windows  
clips underlying controls, making the assumption that your own control is  
opaque, so even if you configure the control to be completely owner-drawn,  
including the background, any underlying controls are never drawn  
underneath your own.

It's possible that there's a way to do this using WPF.  My understanding  
is that WPF uses something more akin to Java's idea of "lightweight"  
controls (i.e. controls that aren't individually tied to an OS window  
handle object), and if so it wouldn't be constrained by how Windows  
works.  But I don't know enough about WPF to say how this would actually  
be done.

Pete
Morten Wennevik [C# MVP] - 31 Mar 2008 08:14 GMT
> > [...]
> > It does not appear to like being tweaked in the designerwindow much, but
[quoted text clipped - 19 lines]
>
> Pete

It should, although it wouldn't render correctly unless I added it
programmatically.  Putting it over the parent form would render the parent
and its controls in a faded manner, the color of the panel, using the
provided alfa level.  However, partially covered controls on the parent form
would pop above the panel when focused so its uses are limited.

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.