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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Semi Transparent Controls

Semi Transparent Controls

Scheduled Pinned Locked Moved Visual Basic
cssgraphicstutorialquestiondiscussion
10 Posts 3 Posters 1 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.
  • E Offline
    E Offline
    errorfunktion
    wrote on last edited by
    #1

    Hi, If you paint on a form using a color with alpha level of less then 255 the form background is partially visible. I was trying to achieve the same effect with controls. I have a control derived from the control class. I have used this code: SetStyle(ControlStyles.SupportsTransparentBackColor) BackColor = Color.Transparent and the foreground is painted with say: color.fromarg(50,255,0,0) This works nicely with graphics that I have painted on the form background it is visible through the control. But other controls with a lower Z order should logically be visible through the control as well but they are not. Any thoughts on how to get it to work? thanks, Gary

    D 1 Reply Last reply
    0
    • E errorfunktion

      Hi, If you paint on a form using a color with alpha level of less then 255 the form background is partially visible. I was trying to achieve the same effect with controls. I have a control derived from the control class. I have used this code: SetStyle(ControlStyles.SupportsTransparentBackColor) BackColor = Color.Transparent and the foreground is painted with say: color.fromarg(50,255,0,0) This works nicely with graphics that I have painted on the form background it is visible through the control. But other controls with a lower Z order should logically be visible through the control as well but they are not. Any thoughts on how to get it to work? thanks, Gary

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      errorfunktion wrote:

      But other controls with a lower Z order should logically be visible through the control as well but they are not.

      That's a good assumption IF transparent was actually "transparent". It's not. When you set the background of a control to Transparent, you're telling it to take on the background settings of it's parent container. It does NOT make the control transparent so you can see other controls behind it!!

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      E 1 Reply Last reply
      0
      • D Dave Kreskowiak

        errorfunktion wrote:

        But other controls with a lower Z order should logically be visible through the control as well but they are not.

        That's a good assumption IF transparent was actually "transparent". It's not. When you set the background of a control to Transparent, you're telling it to take on the background settings of it's parent container. It does NOT make the control transparent so you can see other controls behind it!!

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        E Offline
        E Offline
        errorfunktion
        wrote on last edited by
        #3

        Hi Dave, thanks for the fast response. I still don't understand. If the form has a gray background, on top of it is drawn a green circle. On top of this is my control. If my control background is gray does that mean that the form in some way paints the green circle on my control? If what you say is true that would be the only explanation, otherwise I would not see the circle. If this is the case can I employ this mechanism somehow to cause other controls to behave in this manner?

        T 1 Reply Last reply
        0
        • E errorfunktion

          Hi Dave, thanks for the fast response. I still don't understand. If the form has a gray background, on top of it is drawn a green circle. On top of this is my control. If my control background is gray does that mean that the form in some way paints the green circle on my control? If what you say is true that would be the only explanation, otherwise I would not see the circle. If this is the case can I employ this mechanism somehow to cause other controls to behave in this manner?

          T Offline
          T Offline
          Tom Deketelaere
          wrote on last edited by
          #4

          from what I know of the subject is that when you use a paint (or graphics) to draw a circle onto the background of a form that circle 'becomes a part of the background' (don't really know how else to put it) but when you put a control on the form that control is not a part of the background therefor you can see the circle but not the control other than taking a printscreen and putting that printscreen cropped on the background of you upper control I wouldn't know how to do it (and I probably would give up the print screen methode since it will be very buggy and maybe even impossible (but I'v never try'd anything like this))

          If my help was helpfull let me know, if not let me know why. The only way we learn is by making mistaks.

          E 1 Reply Last reply
          0
          • T Tom Deketelaere

            from what I know of the subject is that when you use a paint (or graphics) to draw a circle onto the background of a form that circle 'becomes a part of the background' (don't really know how else to put it) but when you put a control on the form that control is not a part of the background therefor you can see the circle but not the control other than taking a printscreen and putting that printscreen cropped on the background of you upper control I wouldn't know how to do it (and I probably would give up the print screen methode since it will be very buggy and maybe even impossible (but I'v never try'd anything like this))

            If my help was helpfull let me know, if not let me know why. The only way we learn is by making mistaks.

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

            This is simply not true. There is one computer screen and hence only one set of pixels. If the form background is gray and one draws a green circle those pixels become green. The Form Background is still gray. If a control on top of the form obscures the circle and then moved the form paint event repaints the circle. The circle is by no means part of the background. Also Dave, It's not true that color.transparent simply gives the control the container background. If I manually give the control the same background as the form the circle is not visible. Meaning that color.transparent clearly does something more. What it does I don't know.

            D 1 Reply Last reply
            0
            • E errorfunktion

              This is simply not true. There is one computer screen and hence only one set of pixels. If the form background is gray and one draws a green circle those pixels become green. The Form Background is still gray. If a control on top of the form obscures the circle and then moved the form paint event repaints the circle. The circle is by no means part of the background. Also Dave, It's not true that color.transparent simply gives the control the container background. If I manually give the control the same background as the form the circle is not visible. Meaning that color.transparent clearly does something more. What it does I don't know.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              errorfunktion wrote:

              It's not true that color.transparent simply gives the control the container background.

              Wanna bet??

              errorfunktion wrote:

              If I manually give the control the same background as the form the circle is not visible.

              No kidding. That's exactly what is expected. If you have a background image on the form, the control with the Transparent background WILL show the background image of the form that contains it. Any controls between the background of the form and the Transparent control will NOT show through. The background of the control will take a snippet of the form's background image and make that snippet the background image of the control.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              E 1 Reply Last reply
              0
              • D Dave Kreskowiak

                errorfunktion wrote:

                It's not true that color.transparent simply gives the control the container background.

                Wanna bet??

                errorfunktion wrote:

                If I manually give the control the same background as the form the circle is not visible.

                No kidding. That's exactly what is expected. If you have a background image on the form, the control with the Transparent background WILL show the background image of the form that contains it. Any controls between the background of the form and the Transparent control will NOT show through. The background of the control will take a snippet of the form's background image and make that snippet the background image of the control.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                E Offline
                E Offline
                errorfunktion
                wrote on last edited by
                #7

                Why the sarcasm Dave? After all you agree with me. :) Color.Transparent also tells the control to get its background image from it's container, and hence it does more than simply set it's own background to the same color. Semantics. Never mind. Meanwhile I've made some progress. I've created a derived button class called MyButton. I also have a regular old button, I've written this code in it's event handler: Dim img as New Bitmap(TransparentControl.Width,TransparentControl.Height) Dim GraphicsObject as Graphics=Graphics.FromImage(img) 'I now use a translation transform to account for the diffrence in position 'of the transparent control and the MyButton object: GraphicsObject.TranslateTransform( _ MyButton1.Location.X-TransparentControl.Location.X, _ MyButton1.Location.Y-TransparentControl.Location.Y) 'I now call the derived Button's OnPaint Event using the Graphics object I Just created. MyButton1.OnPaint(New PaintEventArgs( _ GraphicsObject,TransparentControl.DisplayRectangle)) 'And Finally I set the Transparent control background image to this image. TransparentControl.BackgroundImage=img The derived buttons shape (It's white body) is properly located and visible on the back of my control. But the buttons text caption is not translated for some reason. Is the the caption not drawn in the OnPaint Event or something?

                D 1 Reply Last reply
                0
                • E errorfunktion

                  Why the sarcasm Dave? After all you agree with me. :) Color.Transparent also tells the control to get its background image from it's container, and hence it does more than simply set it's own background to the same color. Semantics. Never mind. Meanwhile I've made some progress. I've created a derived button class called MyButton. I also have a regular old button, I've written this code in it's event handler: Dim img as New Bitmap(TransparentControl.Width,TransparentControl.Height) Dim GraphicsObject as Graphics=Graphics.FromImage(img) 'I now use a translation transform to account for the diffrence in position 'of the transparent control and the MyButton object: GraphicsObject.TranslateTransform( _ MyButton1.Location.X-TransparentControl.Location.X, _ MyButton1.Location.Y-TransparentControl.Location.Y) 'I now call the derived Button's OnPaint Event using the Graphics object I Just created. MyButton1.OnPaint(New PaintEventArgs( _ GraphicsObject,TransparentControl.DisplayRectangle)) 'And Finally I set the Transparent control background image to this image. TransparentControl.BackgroundImage=img The derived buttons shape (It's white body) is properly located and visible on the back of my control. But the buttons text caption is not translated for some reason. Is the the caption not drawn in the OnPaint Event or something?

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  errorfunktion wrote:

                  Why the sarcasm Dave? After all you agree with me.

                  That's not how it sounded to me...

                  errorfunktion wrote:

                  I've created a derived button class called MyButton. I also have a regular old button, I've written this code in it's event handler:

                  Which event??

                  errorfunktion wrote:

                  Dim img as New Bitmap(TransparentControl.Width,TransparentControl.Height) Dim GraphicsObject as Graphics=Graphics.FromImage(img) 'I now use a translation transform to account for the diffrence in position 'of the transparent control and the MyButton object: GraphicsObject.TranslateTransform( _ MyButton1.Location.X-TransparentControl.Location.X, _ MyButton1.Location.Y-TransparentControl.Location.Y) 'I now call the derived Button's OnPaint Event using the Graphics object I Just created. MyButton1.OnPaint(New PaintEventArgs( _ GraphicsObject,TransparentControl.DisplayRectangle))

                  You're going to have to explain this a bit better. Where is this code?? Does your button class derive from Button, or from UserControl??

                  errorfunktion wrote:

                  'And Finally I set the Transparent control background image to this image. TransparentControl.BackgroundImage=img

                  What TransparentControl? Where did this come from??

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  E 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    errorfunktion wrote:

                    Why the sarcasm Dave? After all you agree with me.

                    That's not how it sounded to me...

                    errorfunktion wrote:

                    I've created a derived button class called MyButton. I also have a regular old button, I've written this code in it's event handler:

                    Which event??

                    errorfunktion wrote:

                    Dim img as New Bitmap(TransparentControl.Width,TransparentControl.Height) Dim GraphicsObject as Graphics=Graphics.FromImage(img) 'I now use a translation transform to account for the diffrence in position 'of the transparent control and the MyButton object: GraphicsObject.TranslateTransform( _ MyButton1.Location.X-TransparentControl.Location.X, _ MyButton1.Location.Y-TransparentControl.Location.Y) 'I now call the derived Button's OnPaint Event using the Graphics object I Just created. MyButton1.OnPaint(New PaintEventArgs( _ GraphicsObject,TransparentControl.DisplayRectangle))

                    You're going to have to explain this a bit better. Where is this code?? Does your button class derive from Button, or from UserControl??

                    errorfunktion wrote:

                    'And Finally I set the Transparent control background image to this image. TransparentControl.BackgroundImage=img

                    What TransparentControl? Where did this come from??

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    E Offline
                    E Offline
                    errorfunktion
                    wrote on last edited by
                    #9

                    Well I got it working. Here's a couple of screenshots: http://rapidshare.com/files/53533392/tccontrol1.bmp.html http://rapidshare.com/files/53531028/tcontrol2.bmp.html In the forms paint event I have drawn a circle in the upper left corner. I have also placed a button in the upper left corner. The arrow shaped thingy is my control. All You need is a derived control in my case: Public Class TransparentControl Inherits Control 'Set These Options: Public Sub New() SetStyle(ControlStyles.SupportsTransparentBackColor, True) BackColor = Color.Transparent 'It might be necessary to override the base 'backcolor property 'Add some code to the paint event to draw the control and use a color with alpha level less then 255. 'As you can see in the first screen capture the form background is visible, but the control is obscured. ' To make the button also visible through the control you can use the button's draw to bitmap method and set that bitmap as ' the controls background image. ' Right now this code is in the button's paint event handler and it makes things a bit twitchy. There's probably a better position for it. Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint Dim control_rect As New Rectangle(Me.TransparentControl1.Location, Me.TransparentControl1.Size) Dim button_rect As New Rectangle(Me.Button1.Location, Me.Button1.Size) 'Checks If the control area's recangle interesects with the button's If control_rect.IntersectsWith(button_rect) Then 'Find the area of the intersection control_rect.Intersect(button_rect) 'Offset the area of intersection to account for the different positions control_rect.Offset(-Me.TransparentControl1.Location.X, -Me.TransparentControl1.Location.Y) Dim im As New Bitmap(Me.TransparentControl1.Width, Me.TransparentControl1.Height) Me.Button1.DrawToBitmap(im, control_rect) Me.TransparentControl1.BackgroundImage = im End If End Sub That's It. Thanks for the help Dave Gary

                    D 1 Reply Last reply
                    0
                    • E errorfunktion

                      Well I got it working. Here's a couple of screenshots: http://rapidshare.com/files/53533392/tccontrol1.bmp.html http://rapidshare.com/files/53531028/tcontrol2.bmp.html In the forms paint event I have drawn a circle in the upper left corner. I have also placed a button in the upper left corner. The arrow shaped thingy is my control. All You need is a derived control in my case: Public Class TransparentControl Inherits Control 'Set These Options: Public Sub New() SetStyle(ControlStyles.SupportsTransparentBackColor, True) BackColor = Color.Transparent 'It might be necessary to override the base 'backcolor property 'Add some code to the paint event to draw the control and use a color with alpha level less then 255. 'As you can see in the first screen capture the form background is visible, but the control is obscured. ' To make the button also visible through the control you can use the button's draw to bitmap method and set that bitmap as ' the controls background image. ' Right now this code is in the button's paint event handler and it makes things a bit twitchy. There's probably a better position for it. Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint Dim control_rect As New Rectangle(Me.TransparentControl1.Location, Me.TransparentControl1.Size) Dim button_rect As New Rectangle(Me.Button1.Location, Me.Button1.Size) 'Checks If the control area's recangle interesects with the button's If control_rect.IntersectsWith(button_rect) Then 'Find the area of the intersection control_rect.Intersect(button_rect) 'Offset the area of intersection to account for the different positions control_rect.Offset(-Me.TransparentControl1.Location.X, -Me.TransparentControl1.Location.Y) Dim im As New Bitmap(Me.TransparentControl1.Width, Me.TransparentControl1.Height) Me.Button1.DrawToBitmap(im, control_rect) Me.TransparentControl1.BackgroundImage = im End If End Sub That's It. Thanks for the help Dave Gary

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      You may want to play around with this some more. Like moving other windows over the top of your controls and away so that they repaint. Moving your window off-screen and back on again. minimizing and restoring your window. Anything else you can think of. Your control has to stand up to being repainted in every situation you can think of.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      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