470,624 Members | 2,246 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

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.backcolor = form1.backcolor
3.) make form1.transparencykey = 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.backcolor = form1.backcolor
3.) make form1.transparencykey = 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?
-----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
TransparencyKey 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 OnPaintBackground event and
not drawing anything, but I always get a visible
background.

.

Jul 19 '05 #1
5 9209
You need to set the controls Region() for the implementation you are after.
The label is a problem though.

"Paul Schnitter" <ps********@lucent.com> wrote in message
news:13****************************@phx.gbl...
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.backcolor = form1.backcolor
3.) make form1.transparencykey = 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.backcolor = form1.backcolor
3.) make form1.transparencykey = 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?
-----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
TransparencyKey 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 OnPaintBackground event and
not drawing anything, but I always get a visible
background.

.

Jul 19 '05 #2
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.asp>, 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 TestTrafficeLigh 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TrafficLight1.BackColor = Me.BackColor
Label1.BackColor = Me.BackColor
Me.TransparencyKey = 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.

Jul 19 '05 #3
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:
http://www.bobpowell.net/transcontrols.htm
'protected override CreateParams
CreateParams
'{
' get
' {
' CreateParams cp=base.CreateParams;
'
cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT
' return cp;
' }
'}
Get
Dim cp As CreateParams
cp = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or
&H20 'WS_EX_TRANSPARENT
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
-----Original Message-----
Hello Paul,

I checked the TrafficLight control contains in the article "Creating Visual Basic .NET Controls from Scratch"
<http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dnadvnet/html/vbnet08142002.asp>, 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 TestTrafficeLigh 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TrafficLight1.BackColor = Me.BackColor
Label1.BackColor = Me.BackColor
Me.TransparencyKey = 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.
.

Jul 21 '05 #4
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.
Jul 21 '05 #5
Hi Paul,

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

Rendering a Windows Forms Control
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconrenderingwindowsformscontrol.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.
Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Efkas | last post: by
8 posts views Thread by Grahammer | last post: by
8 posts views Thread by Brian Ward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.