Transparent background on UserControls?

G

Grahammer

Is it possible to set the background of a usercontrol as transparent?

I tried setting the background image of the usercontrol to a transparent
GIF, but that caused MAJOR problems.

I'm making some controls that aren't rectangular and it won't be pretty if I
end up with a grey rectangle behind each one.

http://msdn.microsoft.com/library/e...urControlTransparentBackground.asp?frame=true

This link says that it should be simple to create a usercontrol with a
transparent background, and Herfried has suggested the same, but the control
still has a grey background. Basically I'm creating a button with four
images (normal, mouseover, pushed and disabled) and I want the transparent
parts of the image to show the form/controls/etc. that are below the button.
This will allow for round, oval or any other shaped button that I can draw.
The code is done and working, except for this transparency issue.

To test I've done the following:
- Created a new UserControl
- Added the following two lines to the Public Sub New() subroutine (I'm
assuming that this is the "constructor" that is mentioned in the MSDN
support page)
- Save all files
- Did a build on the control
- Added the control to a Form.
- Ran the project.

....the user control is still "control" grey and is not transparent...

Why is this happening? I didn't have this problem when using VB6. Is there a
work around?
 
K

Ken Tucker [MVP]

Hi,

Here is another link that might help.
http://msdn.microsoft.com/library/d...apedWindowsFormsControlsInVisualStudioNET.asp

Here is some sample code.

Public Class UserControl1

Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

SetStyle(ControlStyles.SupportsTransparentBackColor, True)

'Add any initialization after the InitializeComponent() call

Me.BackColor = Color.Transparent

End Sub

'UserControl1 overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

components = New System.ComponentModel.Container()

End Sub

#End Region

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim g As Graphics = e.Graphics

Dim bm As New Bitmap(Me.Width, Me.Height)

Dim gBm As Graphics = Graphics.FromImage(bm)



gBm.FillRectangle(Brushes.Blue, 0, 0, Me.Width, Me.Height)

gBm.FillEllipse(Brushes.Red, 0, 0, Me.Width, Me.Height)

gBm.FillRectangle(Brushes.Blue, 50, 50, 20, 20)

bm.MakeTransparent(Color.Blue)

g.DrawImage(bm, 0, 0)

End Sub

End Class


Ken
---------------------
 
H

Herfried K. Wagner [MVP]

* "Grahammer said:
Is it possible to set the background of a usercontrol as transparent?

I tried setting the background image of the usercontrol to a transparent
GIF, but that caused MAJOR problems.

I'm making some controls that aren't rectangular and it won't be pretty if I
end up with a grey rectangle behind each one.

http://msdn.microsoft.com/library/e...urControlTransparentBackground.asp?frame=true

As an alternative, you can set up a region of appropriate shape (by
composing a 'GraphicsPath') and assign it to the control's 'Region'
property.
 
M

Mick Doherty

Transparency is not the solution. Transparent controls are not really
transparent. They just paint their transparent areas with the corresponding
area of their parent. Any controls which are between your transparent
control and it's parent will not be seen.

As Herfried suggested, you want to modify your controls region. I have not
done this yet in VB.Net, but you may find a performance issue if you create
the region on the fly. In VBClassic I used to create a region and save it as
a resource which could be loaded at runtime. You may be able to do something
similar in VB.Net.
 
P

Phrederik

"Mick Doherty"
Transparency is not the solution. Transparent controls are not really
transparent. They just paint their transparent areas with the corresponding
area of their parent. Any controls which are between your transparent
control and it's parent will not be seen.

As Herfried suggested, you want to modify your controls region. I have not
done this yet in VB.Net, but you may find a performance issue if you create
the region on the fly. In VBClassic I used to create a region and save it as
a resource which could be loaded at runtime. You may be able to do something
similar in VB.Net.

Won't work for me... My user control has no "set" shape as it depends
totally on the image property applied by it's user. The shape may not even
be constand between button states.

I've seen the other ideas posted though and will see what I can do.

I really don't understand how MS could have broken something so useful.
 
G

Grahammer

Phrederik said:
"Mick Doherty"
message news:%[email protected]... it

Won't work for me... My user control has no "set" shape as it depends
totally on the image property applied by it's user. The shape may not even
be constand between button states.

I've seen the other ideas posted though and will see what I can do.

I really don't understand how MS could have broken something so useful.

....OK... Either I define a perimeter for my usercontrol (which I cannot do
for this implimentation) or I build a .COM control with VB6 and use it in my
..Net apps (which really stinks, since .Net has all the useful mouse
enter/leave/hover/etc. events and I have to manually emulate these in VB6)

Does anyone know if MS will be fixing this problem?... or is it another case
of "do it our way or don't do it" ???
 
M

Mick Doherty

As I said, I have not done this yet, so I don't know if you will have a
performance issue. The GDI+ API's have been integrated into DotNet so there
will be a vast improvement of speed compared to VBClassic, but whether it
will be good enough, I cannot say. I would try to implement this before
condemning it.
In VBClassic you had to create a MaskImage and so this will not really be
much different, you will just be creating a region instead. Taking this into
account, a VB6 Com control is not going to give you any advantage over a
DotNet Control. Even if the Image is set by the user, the 'possibly' slow
region creating process will only need to be done once per image, so you
would only get a slow Paint when the image is first set by the user.
 
G

Grahammer

"Mick Doherty"
As I said, I have not done this yet, so I don't know if you will have a
performance issue. The GDI+ API's have been integrated into DotNet so there
will be a vast improvement of speed compared to VBClassic, but whether it
will be good enough, I cannot say. I would try to implement this before
condemning it.
In VBClassic you had to create a MaskImage and so this will not really be
much different, you will just be creating a region instead.

Actually, no... You didn't have to make any mask image, you simply had to
tell VB what colour was to be interpreted as transparent. It worked in my
old apps and I even had this button able to consider mouseover when in the
rectangular region OR over the visible only part of the image.

I'd just use this control except that I'm trying to reduce my reliance on
all the timers,etc. that were required to jury rig a "mouseout" event in
VB6. Now that we have all the events we need, we don't have the
functionality to use them.
 
M

Mick Doherty

You're right, I was thinking of shaped controls rather than Transparent
controls.
I guess you could always create a shaped control which monitors the
MouseEvents of it's container and responds if the event happens within a
specified region around the control as well as within the control itself.
I also hope that proper transparency is fixed (although I doubt it will be
anytime soon) since this causes so many problems, especially with XP's
Visual Styles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top