How do I make the background of a control transparent?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Schnitter

    How do I make the background of a control transparent?

    Update:

    My custom control is based on the article
    "Creating Visual Basic .NET controls from scratch"
    in "Adventures in .NET" on MSDN.

    It is designed to be a replacement for the VB6 shape
    control.

    My control draws a shape (circle, square, rectangle ...)
    on the background. If

    1.) put a MyShapeControl on the form
    2.) make shape1.backcolo r = form1.backcolor
    3.) make form1.transpare ncykey = form1.backcolor
    4.) put a backgoundimage on the form

    You can see right through
    the background of my control to whatever is behind the
    form. Also click events, dragdrops, etc. are transfered
    to whatever is below the form.

    That is NOT the same behavior as the stock .NET controls,
    contrary to the documentation. If I

    1.) put a label control on the form
    2.) make label1.backcolo r = form1.backcolor
    3.) make form1.transpare ncykey = form1.backcolor
    4.) put a backgoundimage on the form

    the label control background is not transparent.

    I don't understand why they changed the VB6 behavior or
    why they think this new behavior is good.

    All I want is a shape control who's background is
    transparent to the other controls on my form. How do I do
    that?
    [color=blue]
    >-----Original Message-----
    >The System.Windows. Forms.Form class has a TransparencyKey
    >property that sets a specific background color to be
    >transparent.
    >
    >The System.Windows. Forms.Control class does not have that
    >property. How do I add the functionality of the
    >TransparencyKe y property to a Control?
    >
    >Unless I am missing something, it seems that all .NET
    >controls are missing a very important property by not
    >allowing them to have a transparent background.
    >
    >I have tried overriding the OnPaintBackgrou nd event and
    >not drawing anything, but I always get a visible
    >background.
    >
    >.
    >[/color]
  • Mick Doherty

    #2
    Re: How do I make the background of a control transparent?

    You need to set the controls Region() for the implementation you are after.
    The label is a problem though.

    "Paul Schnitter" <pschnitter@luc ent.com> wrote in message
    news:139401c373 09$bd17f670$a10 1280a@phx.gbl.. .[color=blue]
    > Update:
    >
    > My custom control is based on the article
    > "Creating Visual Basic .NET controls from scratch"
    > in "Adventures in .NET" on MSDN.
    >
    > It is designed to be a replacement for the VB6 shape
    > control.
    >
    > My control draws a shape (circle, square, rectangle ...)
    > on the background. If
    >
    > 1.) put a MyShapeControl on the form
    > 2.) make shape1.backcolo r = form1.backcolor
    > 3.) make form1.transpare ncykey = form1.backcolor
    > 4.) put a backgoundimage on the form
    >
    > You can see right through
    > the background of my control to whatever is behind the
    > form. Also click events, dragdrops, etc. are transfered
    > to whatever is below the form.
    >
    > That is NOT the same behavior as the stock .NET controls,
    > contrary to the documentation. If I
    >
    > 1.) put a label control on the form
    > 2.) make label1.backcolo r = form1.backcolor
    > 3.) make form1.transpare ncykey = form1.backcolor
    > 4.) put a backgoundimage on the form
    >
    > the label control background is not transparent.
    >
    > I don't understand why they changed the VB6 behavior or
    > why they think this new behavior is good.
    >
    > All I want is a shape control who's background is
    > transparent to the other controls on my form. How do I do
    > that?
    >[color=green]
    > >-----Original Message-----
    > >The System.Windows. Forms.Form class has a TransparencyKey
    > >property that sets a specific background color to be
    > >transparent.
    > >
    > >The System.Windows. Forms.Control class does not have that
    > >property. How do I add the functionality of the
    > >TransparencyKe y property to a Control?
    > >
    > >Unless I am missing something, it seems that all .NET
    > >controls are missing a very important property by not
    > >allowing them to have a transparent background.
    > >
    > >I have tried overriding the OnPaintBackgrou nd event and
    > >not drawing anything, but I always get a visible
    > >background.
    > >
    > >.
    > >[/color][/color]


    Comment

    • Tian Min Huang

      #3
      RE: How do I make the background of a control transparent?

      Hello Paul,

      I checked the TrafficLight control contains in the article "Creating Visual
      Basic .NET Controls from Scratch"
      <http://msdn.microsoft.com/library/de...-us/dnadvnet/h
      tml/vbnet08142002.a sp>, and found that it has the same behavior with the
      .NET stock controls say, Label control. The following is what I did:

      1. Open the TestTrafficeLig h project.

      2. Add a Label control in Form1.

      3. Point BackgroupImage of From1 to a jpg file.

      4. Add the following code:

      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      TrafficLight1.B ackColor = Me.BackColor
      Label1.BackColo r = Me.BackColor
      Me.Transparency Key = Me.BackColor
      End Sub

      5. Run and test, I found that the background of both the Label1 and
      TrafficLight1 are transparent.

      Would you please check it on your side. I look forward to your response.

      Have a nice day!

      Regards,

      HuangTM
      Microsoft Online Partner Support
      MCSE/MCSD

      Get Secure! ¨C www.microsoft.com/security
      This posting is provided ¡°as is¡± with no warranties and confers no rights.

      Comment

      • Paul Schnitter

        #4
        RE: How do I make the background of a control transparent?

        Thanks for responding,

        I did what you requested. The TrafficLight background is
        now totally transparent. That is, transparent right
        through the form. Whatever is behind the form shows
        through. The procedure had no effect on the label. The
        label's backgound was opaque with the background color =
        the forms's transparencykey color.

        That is not the results I want anyway. What I want is a
        Shape control like the one that exists in VB6. The
        background is transparent with respect to whatever is
        below it in the Z-order, on the same form.

        I have found the beginning's of a solution on
        http://www.bobpowell.net/transcontrols.htm. I had to
        translate from C++ or C#.

        Protected Overrides ReadOnly Property
        CreateParams() As CreateParams
        ' From:

        'protected override CreateParams
        CreateParams
        '{
        ' get
        ' {
        ' CreateParams cp=base.CreateP arams;
        '
        cp.ExStyle|=0x0 0000020; //WS_EX_TRANSPARE NT
        ' return cp;
        ' }
        '}
        Get
        Dim cp As CreateParams
        cp = MyBase.CreatePa rams
        cp.ExStyle = cp.ExStyle Or
        &H20 'WS_EX_TRANSPAR ENT
        Return cp
        End Get
        End Property


        This gives the the background i want. However I have a
        problem when the shape moves or resizes. The background
        doesn't always repaint properly, especially when two shape
        controls ovelap.

        - Paul
        [color=blue]
        >-----Original Message-----
        >Hello Paul,
        >
        >I checked the TrafficLight control contains in the [/color]
        article "Creating Visual [color=blue]
        >Basic .NET Controls from Scratch"
        ><http://msdn.microsoft.com/library/default.asp?[/color]
        url=/library/en-us/dnadvnet/h[color=blue]
        >tml/vbnet08142002.a sp>, and found that it has the same [/color]
        behavior with the [color=blue]
        >.NET stock controls say, Label control. The following is [/color]
        what I did:[color=blue]
        >
        >1. Open the TestTrafficeLig h project.
        >
        >2. Add a Label control in Form1.
        >
        >3. Point BackgroupImage of From1 to a jpg file.
        >
        >4. Add the following code:
        >
        > Private Sub Form1_Load(ByVa l sender As System.Object, [/color]
        ByVal e As [color=blue]
        >System.EventAr gs) Handles MyBase.Load
        > TrafficLight1.B ackColor = Me.BackColor
        > Label1.BackColo r = Me.BackColor
        > Me.Transparency Key = Me.BackColor
        > End Sub
        >
        >5. Run and test, I found that the background of both the [/color]
        Label1 and [color=blue]
        >TrafficLight 1 are transparent.
        >
        >Would you please check it on your side. I look forward to [/color]
        your response.[color=blue]
        >
        >Have a nice day!
        >
        >Regards,
        >
        >HuangTM
        >Microsoft Online Partner Support
        >MCSE/MCSD
        >
        >Get Secure! ¨C www.microsoft.com/security
        >This posting is provided ¡°as is¡± with no warranties and [/color]
        confers no rights.[color=blue]
        >
        >.
        >[/color]

        Comment

        • Tian Min Huang

          #5
          RE: How do I make the background of a control transparent?

          Hi Paul,

          Thanks for your update. I am checking this issue and will update you with
          my informatoin.

          Have a nice day!

          Regards,

          HuangTM
          Microsoft Online Partner Support
          MCSE/MCSD

          Get Secure! ¨C www.microsoft.com/security
          This posting is provided ¡°as is¡± with no warranties and confers no rights.


          Comment

          • Tian Min Huang

            #6
            RE: How do I make the background of a control transparent?

            Hi Paul,

            It seems to me that the problem you are facing is concerning repaint. Did
            you handle the OnPaint, OnPaintBackgrou nd, and OnResize events? I believe
            the following MSDN article is helpful:

            Rendering a Windows Forms Control

            l/cpconrenderingw indowsformscont rol.asp

            Hope this helps.

            Regards,

            HuangTM
            Microsoft Online Partner Support
            MCSE/MCSD

            Get Secure! ¨C www.microsoft.com/security
            This posting is provided ¡°as is¡± with no warranties and confers no rights.


            Comment

            Working...