How do I make the background of a control transparent?

  • Thread starter Thread starter Paul Schnitter
  • Start date Start date
P

Paul Schnitter

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?
 
You need to set the controls Region() for the implementation you are after.
The label is a problem though.
 
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/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.
 
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
 
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.
 
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/default.asp?url=/library/en-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.
 
Back
Top