Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Seeking: Panel Opacity & Border

Seeking: Panel Opacity & Border

Scheduled Pinned Locked Moved C#
csharpcomgraphicsquestion
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A adrianna_r

    Hello, Beginning with the hard bit first, I have recently started looking for a way to control a panel's background's opacity, and have not encountered a fully explained C# solution (though there are quite a lot of VB ones, which I sadly only managed to gape at without understanding a thing). I have seen the way to make it completely transparent[^], and I have encountered a way to create opacity for a PictureBox's image[^]... but attempts to combine the two have failed miserably. I assume that once I get the transparent background thing going, drawing a non-transparent border (perhaps using some of the code from here[^]) would be easy enough to figure out (though if it clashes with the opacity goal, then I would like to know how/why). I'd appreciate any tips/links/code bits/etc. Thanks.

    -M.

    M Offline
    M Offline
    Martin 0
    wrote on last edited by
    #2

    Hello, I had the same problem, by using a backgroundimage on a Form. There are some solution (property "TransparencyKey") that are not working for every graphic card. This works: Bitmap bm = new Bitmap(...); Color transparent = bm.GetPixel(0,0); bm.MakeTransparent(transparent); this.BackgroundImage = bm; What I found out is, that you can not change the opacity after you made it transparent. (Ok, you can but transparency doesn't work after that.) After you change opacity you have to "MakeTransparent" again. I know it's not much, but maybe it helps. All the best, Martin

    A 1 Reply Last reply
    0
    • M Martin 0

      Hello, I had the same problem, by using a backgroundimage on a Form. There are some solution (property "TransparencyKey") that are not working for every graphic card. This works: Bitmap bm = new Bitmap(...); Color transparent = bm.GetPixel(0,0); bm.MakeTransparent(transparent); this.BackgroundImage = bm; What I found out is, that you can not change the opacity after you made it transparent. (Ok, you can but transparency doesn't work after that.) After you change opacity you have to "MakeTransparent" again. I know it's not much, but maybe it helps. All the best, Martin

      A Offline
      A Offline
      adrianna_r
      wrote on last edited by
      #3

      Hmm... I think there may have been a misunderstanding. What I'm trying to do is create a panel with opacity, so that when I move it around over other controls, the other controls are visible under it, but the panel's background is still shown, just faded according to its opacity value. Something to the effect of looking at the world through a piece of colored cellophane... If I understand your suggestion correctly, that code will make my panel transparent, but I want it to still be visible, but faded.

      -M.

      M E 2 Replies Last reply
      0
      • A adrianna_r

        Hmm... I think there may have been a misunderstanding. What I'm trying to do is create a panel with opacity, so that when I move it around over other controls, the other controls are visible under it, but the panel's background is still shown, just faded according to its opacity value. Something to the effect of looking at the world through a piece of colored cellophane... If I understand your suggestion correctly, that code will make my panel transparent, but I want it to still be visible, but faded.

        -M.

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #4

        Hello,

        adrianna_r wrote:

        If I understand your suggestion correctly, that code will make my panel transparent, but I want it to still be visible, but faded.

        Only the color of the specified bixel will be transparent. Martin

        A 1 Reply Last reply
        0
        • A adrianna_r

          Hmm... I think there may have been a misunderstanding. What I'm trying to do is create a panel with opacity, so that when I move it around over other controls, the other controls are visible under it, but the panel's background is still shown, just faded according to its opacity value. Something to the effect of looking at the world through a piece of colored cellophane... If I understand your suggestion correctly, that code will make my panel transparent, but I want it to still be visible, but faded.

          -M.

          E Offline
          E Offline
          ejuanpp
          wrote on last edited by
          #5

          Hi, Have you thought of creating another Form hosting your Panel ? Regards

          A 1 Reply Last reply
          0
          • E ejuanpp

            Hi, Have you thought of creating another Form hosting your Panel ? Regards

            A Offline
            A Offline
            adrianna_r
            wrote on last edited by
            #6

            I don't understand... why should I create an entire form just to have a customized panel? I'm just looking for a a piece of code to stick in the OnPaint method of a custom panel that'll make it's background have opacity... I've seen it done in VB, but was unable to translate it to C#.

            -M.

            E 1 Reply Last reply
            0
            • M Martin 0

              Hello,

              adrianna_r wrote:

              If I understand your suggestion correctly, that code will make my panel transparent, but I want it to still be visible, but faded.

              Only the color of the specified bixel will be transparent. Martin

              A Offline
              A Offline
              adrianna_r
              wrote on last edited by
              #7

              ...But I don't want full transparency on any bit of the panel, just opacity. I want it to look like in this Photoshopped-ed image[^]. (the blue panel isn't fully transparent, but you can still see stuff under it, such as that button)

              -M.

              M 2 Replies Last reply
              0
              • A adrianna_r

                ...But I don't want full transparency on any bit of the panel, just opacity. I want it to look like in this Photoshopped-ed image[^]. (the blue panel isn't fully transparent, but you can still see stuff under it, such as that button)

                -M.

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #8

                Ok, Only found VB.Net code: http://www.codeproject.com/vb/net/AlphaGradientPanel.asp[^] Martin

                A 1 Reply Last reply
                0
                • A adrianna_r

                  ...But I don't want full transparency on any bit of the panel, just opacity. I want it to look like in this Photoshopped-ed image[^]. (the blue panel isn't fully transparent, but you can still see stuff under it, such as that button)

                  -M.

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #9

                  Hello, Have you tried changeing the Alpha Level of the BackColor. this.BackColor=Color.FromArgb(128,this.BackColor); All the best, Martin -- modified at 7:43 Wednesday 29th November, 2006 Have it from here. Tried it, but doesn't work????? http://www.eggheadcafe.com/aspnet_answers/NETFrameworkdrawing/Feb2006/post25815823.asp[^]

                  A 1 Reply Last reply
                  0
                  • M Martin 0

                    Ok, Only found VB.Net code: http://www.codeproject.com/vb/net/AlphaGradientPanel.asp[^] Martin

                    A Offline
                    A Offline
                    adrianna_r
                    wrote on last edited by
                    #10

                    I saw that code... but I didn't understand it at all and I don't know how to convert it to C#. There are a lot of abilities in that thing that I don't need, and figuring out which line does what to extract the stuff I want requires VB knowledge that I just don't have. Not to mention the fact that I don't even know if that stuff even -has- a C# counterpart.

                    -M.

                    1 Reply Last reply
                    0
                    • M Martin 0

                      Hello, Have you tried changeing the Alpha Level of the BackColor. this.BackColor=Color.FromArgb(128,this.BackColor); All the best, Martin -- modified at 7:43 Wednesday 29th November, 2006 Have it from here. Tried it, but doesn't work????? http://www.eggheadcafe.com/aspnet_answers/NETFrameworkdrawing/Feb2006/post25815823.asp[^]

                      A Offline
                      A Offline
                      adrianna_r
                      wrote on last edited by
                      #11

                      I have tried that, yeah, without success :-\

                      -M.

                      1 Reply Last reply
                      0
                      • A adrianna_r

                        I don't understand... why should I create an entire form just to have a customized panel? I'm just looking for a a piece of code to stick in the OnPaint method of a custom panel that'll make it's background have opacity... I've seen it done in VB, but was unable to translate it to C#.

                        -M.

                        E Offline
                        E Offline
                        ejuanpp
                        wrote on last edited by
                        #12

                        Something like this?

                            public partial class CustomPanel : Panel
                            {
                                Color color;
                                public CustomPanel()
                                {
                                    InitializeComponent();
                                    color = Color.FromArgb(100, Color.Blue);
                                }
                                protected override CreateParams CreateParams
                                {
                                    get
                                    {
                                        CreateParams cp = base.CreateParams;
                                        cp.ExStyle |=  0x00000020; //WS_EX_TRANSPARENT
                                        return cp;
                                    }
                                }
                        
                                protected override void OnPaintBackground(PaintEventArgs e){}
                                
                                protected override void OnPaint(PaintEventArgs e)
                                {
                                   
                                    using (Brush brush = new SolidBrush(color))
                                    {
                                        e.Graphics.Clear(Color.Transparent);
                                        e.Graphics.FillRectangle(brush, this.ClientRectangle);
                                    }
                                }
                            }
                        
                        A 1 Reply Last reply
                        0
                        • E ejuanpp

                          Something like this?

                              public partial class CustomPanel : Panel
                              {
                                  Color color;
                                  public CustomPanel()
                                  {
                                      InitializeComponent();
                                      color = Color.FromArgb(100, Color.Blue);
                                  }
                                  protected override CreateParams CreateParams
                                  {
                                      get
                                      {
                                          CreateParams cp = base.CreateParams;
                                          cp.ExStyle |=  0x00000020; //WS_EX_TRANSPARENT
                                          return cp;
                                      }
                                  }
                          
                                  protected override void OnPaintBackground(PaintEventArgs e){}
                                  
                                  protected override void OnPaint(PaintEventArgs e)
                                  {
                                     
                                      using (Brush brush = new SolidBrush(color))
                                      {
                                          e.Graphics.Clear(Color.Transparent);
                                          e.Graphics.FillRectangle(brush, this.ClientRectangle);
                                      }
                                  }
                              }
                          
                          A Offline
                          A Offline
                          adrianna_r
                          wrote on last edited by
                          #13

                          Yes! I've no idea why it works, but it does. Many thanks!

                          -M.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups