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. Transparent background

Transparent background

Scheduled Pinned Locked Moved C#
tutorialquestion
16 Posts 5 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.
  • C Offline
    C Offline
    Christian Wikander
    wrote on last edited by
    #1

    I want to give some controls a transparent background. Does anyone have a hint on how to accomplish this? I have found some examples that removes part of the area the control uses from the control's region. But since they check every pixel of the control they get very slow with large controls.

    L M 2 Replies Last reply
    0
    • C Christian Wikander

      I want to give some controls a transparent background. Does anyone have a hint on how to accomplish this? I have found some examples that removes part of the area the control uses from the control's region. But since they check every pixel of the control they get very slow with large controls.

      L Offline
      L Offline
      lmuth
      wrote on last edited by
      #2

      yourControl.BackColor = Color.Transparent; Seriously, it should work.

      C 1 Reply Last reply
      0
      • L lmuth

        yourControl.BackColor = Color.Transparent; Seriously, it should work.

        C Offline
        C Offline
        Christian Wikander
        wrote on last edited by
        #3

        It should work. Did you try? You'll be surprised! Edit: Seriously. For a UserControl, you cannot set the background to Transparent. If you try to set the background to Transparent on the standard Label, the label assumes the same color as the background, but if you place it on top of something else, that something else is hidden.

        L 1 Reply Last reply
        0
        • C Christian Wikander

          It should work. Did you try? You'll be surprised! Edit: Seriously. For a UserControl, you cannot set the background to Transparent. If you try to set the background to Transparent on the standard Label, the label assumes the same color as the background, but if you place it on top of something else, that something else is hidden.

          L Offline
          L Offline
          lmuth
          wrote on last edited by
          #4

          Before the edit, I was going to ask you exactly what you were trying to do. The "seriously" comment was just because the code otherwise looks like a snide comment on how to set transparency. I have used BackColor = Transparent with success before, but it does have the problem you mention. There's really no good way around it without doing a lot of fancy region work or drawing everything yourself (which is ok for informational controls such as labels but a pain for input controls). Depending on your exact needs, I've found that if all you're doing is non-input controls (labels, images, etc.), painting the whole thing yourself leads to far better results in terms of accuracy and speed. Of course, if you just need to match a dynamically changing background color to the parent control, you can do yourControl.BackColor = yourControl.Parent.BackColor, but I don't think that's quite what you're after.

          C 1 Reply Last reply
          0
          • L lmuth

            Before the edit, I was going to ask you exactly what you were trying to do. The "seriously" comment was just because the code otherwise looks like a snide comment on how to set transparency. I have used BackColor = Transparent with success before, but it does have the problem you mention. There's really no good way around it without doing a lot of fancy region work or drawing everything yourself (which is ok for informational controls such as labels but a pain for input controls). Depending on your exact needs, I've found that if all you're doing is non-input controls (labels, images, etc.), painting the whole thing yourself leads to far better results in terms of accuracy and speed. Of course, if you just need to match a dynamically changing background color to the parent control, you can do yourControl.BackColor = yourControl.Parent.BackColor, but I don't think that's quite what you're after.

            C Offline
            C Offline
            Christian Wikander
            wrote on last edited by
            #5

            Exactly. But if I paint the control myself, it will stil occupy the whole region. The areas that I do not paint are filled with noice. Or are there any tricks here? Let say that I want to paint a simple textstring (like a label). Is there a way to set the region according to the text without looping through every pixel in the control, checking if it's changed or not? I really hate this. I want my old VB labels back! ;-)

            L S 2 Replies Last reply
            0
            • C Christian Wikander

              Exactly. But if I paint the control myself, it will stil occupy the whole region. The areas that I do not paint are filled with noice. Or are there any tricks here? Let say that I want to paint a simple textstring (like a label). Is there a way to set the region according to the text without looping through every pixel in the control, checking if it's changed or not? I really hate this. I want my old VB labels back! ;-)

              L Offline
              L Offline
              lmuth
              wrote on last edited by
              #6

              If it works for you, you could simply call Graphics.DrawString from the OnPaint method of control that's underneath to paint the text. That way, that "label" would really just be painted on and not a control. That might cause some flicker issues, but it all depends on how often your program redraws. I don't quickly know of an easy way to set the region to the text.

              S 1 Reply Last reply
              0
              • L lmuth

                If it works for you, you could simply call Graphics.DrawString from the OnPaint method of control that's underneath to paint the text. That way, that "label" would really just be painted on and not a control. That might cause some flicker issues, but it all depends on how often your program redraws. I don't quickly know of an easy way to set the region to the text.

                S Offline
                S Offline
                spif2001
                wrote on last edited by
                #7

                Don't know if this can help you, but this article shows transparency in VB: http://addressof.com/blog/articles/293.aspx -spif2001

                1 Reply Last reply
                0
                • C Christian Wikander

                  Exactly. But if I paint the control myself, it will stil occupy the whole region. The areas that I do not paint are filled with noice. Or are there any tricks here? Let say that I want to paint a simple textstring (like a label). Is there a way to set the region according to the text without looping through every pixel in the control, checking if it's changed or not? I really hate this. I want my old VB labels back! ;-)

                  S Offline
                  S Offline
                  Sebastian Schneider
                  wrote on last edited by
                  #8

                  There is an excellent article on "Custom Shaped Forms" in the MSDN. That article deals with Forms taking non-rectangular shapes. The first part describes masking the Form with a Bitmap (which is not what you are after). In the second part, though, the author describes the creation of Forms with custom design not dependant on bitmaps, but using GraphicsPath instead. I am not sure if this is applicable, but if it is, you should be able to "freeform" your controls. As for painting the controls yourself and having noise on the non-painted areas: You obviously still USE the whole region. Since Windows does some real clever clipping calculations, the painting of these areas is left to the topmost control (which makes sense). If you do not repaint them, they will stay whatever they are. Cheers Sid

                  C 1 Reply Last reply
                  0
                  • S Sebastian Schneider

                    There is an excellent article on "Custom Shaped Forms" in the MSDN. That article deals with Forms taking non-rectangular shapes. The first part describes masking the Form with a Bitmap (which is not what you are after). In the second part, though, the author describes the creation of Forms with custom design not dependant on bitmaps, but using GraphicsPath instead. I am not sure if this is applicable, but if it is, you should be able to "freeform" your controls. As for painting the controls yourself and having noise on the non-painted areas: You obviously still USE the whole region. Since Windows does some real clever clipping calculations, the painting of these areas is left to the topmost control (which makes sense). If you do not repaint them, they will stay whatever they are. Cheers Sid

                    C Offline
                    C Offline
                    Christian Wikander
                    wrote on last edited by
                    #9

                    The MSDN article seems very helpful. I got some results right away. Not exactly what I wanted, but I think I'll be able to convert it to something useful. Thanks!

                    1 Reply Last reply
                    0
                    • C Christian Wikander

                      I want to give some controls a transparent background. Does anyone have a hint on how to accomplish this? I have found some examples that removes part of the area the control uses from the control's region. But since they check every pixel of the control they get very slow with large controls.

                      M Offline
                      M Offline
                      mav northwind
                      wrote on last edited by
                      #10

                      Creating really transparent controls can be quite tricky because of the way controls are painted. But basically you'll have to override your control's CreateParams to include the WS_EX_TRANSPARENT extended window style and override OnPaintBackground to do nothing. A transparent control would look like this:

                      public class TransparentControl : Control
                      {
                      public TransparentControl()
                      {
                      SetStyle(ControlStyles.UserPaint, true);
                      SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                      SetStyle(ControlStyles.SupportsTransparentBackColor, true);

                      BackColor = Color.Transparent;
                      

                      }

                      protected override CreateParams CreateParams
                      {
                      get
                      {
                      CreateParams cp = base.CreateParams;
                      cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                      return cp;
                      }
                      }

                      protected override void OnPaintBackground(PaintEventArgs pevent)
                      {
                      // do nothing
                      }
                      }

                      You can add your own OnPaint() override, of course, to perform your foreground painting. Regards, mav

                      C 2 Replies Last reply
                      0
                      • M mav northwind

                        Creating really transparent controls can be quite tricky because of the way controls are painted. But basically you'll have to override your control's CreateParams to include the WS_EX_TRANSPARENT extended window style and override OnPaintBackground to do nothing. A transparent control would look like this:

                        public class TransparentControl : Control
                        {
                        public TransparentControl()
                        {
                        SetStyle(ControlStyles.UserPaint, true);
                        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                        SetStyle(ControlStyles.SupportsTransparentBackColor, true);

                        BackColor = Color.Transparent;
                        

                        }

                        protected override CreateParams CreateParams
                        {
                        get
                        {
                        CreateParams cp = base.CreateParams;
                        cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                        return cp;
                        }
                        }

                        protected override void OnPaintBackground(PaintEventArgs pevent)
                        {
                        // do nothing
                        }
                        }

                        You can add your own OnPaint() override, of course, to perform your foreground painting. Regards, mav

                        C Offline
                        C Offline
                        Christian Wikander
                        wrote on last edited by
                        #11

                        Even better! Thanks again for all your help!

                        1 Reply Last reply
                        0
                        • M mav northwind

                          Creating really transparent controls can be quite tricky because of the way controls are painted. But basically you'll have to override your control's CreateParams to include the WS_EX_TRANSPARENT extended window style and override OnPaintBackground to do nothing. A transparent control would look like this:

                          public class TransparentControl : Control
                          {
                          public TransparentControl()
                          {
                          SetStyle(ControlStyles.UserPaint, true);
                          SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                          SetStyle(ControlStyles.SupportsTransparentBackColor, true);

                          BackColor = Color.Transparent;
                          

                          }

                          protected override CreateParams CreateParams
                          {
                          get
                          {
                          CreateParams cp = base.CreateParams;
                          cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                          return cp;
                          }
                          }

                          protected override void OnPaintBackground(PaintEventArgs pevent)
                          {
                          // do nothing
                          }
                          }

                          You can add your own OnPaint() override, of course, to perform your foreground painting. Regards, mav

                          C Offline
                          C Offline
                          Christian Wikander
                          wrote on last edited by
                          #12

                          I tried to inherit from Label instead of Control, but I'm a bit puzzled by the result. This results in a black box where I had a transparent box when inheriting from Control. I then try to override OnPaint() as well, but no there is no change in behaviour. I figured it would have something to do with the CreateParamas class returned, but I can't find very few differences between the Style and ExStyle properties when inheriting from Control versus inheriting from Label. The only difference is to the Style property where the Label does not set WS_TABSTOP, but sets CCS_TOP, CCS_NORESIZE and CCS_NOPARENTALIGN. Any idéa to why I get this result?

                          M 1 Reply Last reply
                          0
                          • C Christian Wikander

                            I tried to inherit from Label instead of Control, but I'm a bit puzzled by the result. This results in a black box where I had a transparent box when inheriting from Control. I then try to override OnPaint() as well, but no there is no change in behaviour. I figured it would have something to do with the CreateParamas class returned, but I can't find very few differences between the Style and ExStyle properties when inheriting from Control versus inheriting from Label. The only difference is to the Style property where the Label does not set WS_TABSTOP, but sets CCS_TOP, CCS_NORESIZE and CCS_NOPARENTALIGN. Any idéa to why I get this result?

                            M Offline
                            M Offline
                            mav northwind
                            wrote on last edited by
                            #13

                            Not really, I guess Label is performing its own PaintBackground actions. mav

                            C 1 Reply Last reply
                            0
                            • M mav northwind

                              Not really, I guess Label is performing its own PaintBackground actions. mav

                              C Offline
                              C Offline
                              Christian Wikander
                              wrote on last edited by
                              #14

                              Ofcourse I'm overriding OnPaintBackground(). I've also looked into the code behind the Label control using Lutz Roeders Reflector. The Label does not contain an implementation for OnPaintBackground(). As I understand it, no painting should go on from the Label's point of view if I override all OnPaint* methods with empty ones?

                              M 1 Reply Last reply
                              0
                              • C Christian Wikander

                                Ofcourse I'm overriding OnPaintBackground(). I've also looked into the code behind the Label control using Lutz Roeders Reflector. The Label does not contain an implementation for OnPaintBackground(). As I understand it, no painting should go on from the Label's point of view if I override all OnPaint* methods with empty ones?

                                M Offline
                                M Offline
                                mav northwind
                                wrote on last edited by
                                #15

                                Don't know why it behaves like this when inheriting from Label. I've created my own TransparentLabel (drawing the text myself isn't too much work ;)) and it works well. Also using the TransparentControl as base class for a semi-transparent aqua-bubble control works well... mav

                                C 1 Reply Last reply
                                0
                                • M mav northwind

                                  Don't know why it behaves like this when inheriting from Label. I've created my own TransparentLabel (drawing the text myself isn't too much work ;)) and it works well. Also using the TransparentControl as base class for a semi-transparent aqua-bubble control works well... mav

                                  C Offline
                                  C Offline
                                  Christian Wikander
                                  wrote on last edited by
                                  #16

                                  I'm just lazy. Even the most simple control like the label contains much functionality that you don't think about. Text alignment, auto size etc. If I am to implement all or most of it, it will have to do quite a bit of coding compared to inheriting and adding the custom functions I like. And sadly my work doesn't stop with the Label. Other controls that I would like to be transparent that I can come up with on the top of my head is the Panel, RadioButton, CheckBox... ;-) Thanks anyway. This has been an interesting experience. :-D

                                  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