By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,726 Members | 2,465 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,726 IT Pros & Developers. It's quick & easy.

Transparent background on UserControls?

P: n/a

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/en...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?
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

Here is another link that might help.
http://msdn.microsoft.com/library/de...lStudioNET.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.SupportsTransparentBackColo r, 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
---------------------
"Grahammer" <po********@127.0.0.1> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...

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/en...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?

Nov 20 '05 #2

P: n/a
* "Grahammer" <po********@127.0.0.1> scripsit:
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/en...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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
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.

"Grahammer" <po********@127.0.0.1> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...

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/en...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?

Nov 20 '05 #4

P: n/a

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:%2***************@tk2msftngp13.phx.gbl...
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.
Nov 20 '05 #5

P: n/a

"Phrederik" <po********@127.0.0.1> wrote in message
news:HSvxb.507205$6C4.482901@pd7tw1no...

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
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.


....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" ???
Nov 20 '05 #6

P: n/a
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.

"Grahammer" <po********@127.0.0.1> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl...

"Phrederik" <po********@127.0.0.1> wrote in message
news:HSvxb.507205$6C4.482901@pd7tw1no...

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:%2***************@tk2msftngp13.phx.gbl...
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.


...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" ???

Nov 20 '05 #7

P: n/a

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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.
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.
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" ???

Nov 20 '05 #8

P: n/a
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.

"Grahammer" <po********@127.0.0.1> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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.

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.