473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Transparent background on UserControls?


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
8 10568
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
* "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
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

"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

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul Schnitter | last post by:
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...
7
by: Thomas Wieser | last post by:
Hi, my problem: I have some tables with transparent backgrounds, which are changed in colours within a JavaScript DOM function to have a roll-over effect. Now, i can't get them back...
1
by: Efkas | last post by:
My application have some level : 1. MyButton class with Label inheritance 2. MyComponent as User Control loading and positionning some of MyButtons 3. MyApp loading and positionning MyComponent ...
3
by: Steve Koon | last post by:
Any thoughts on getting this project to work or suggesting another method would be appreciated. Steve ========================================== Project: A Windows Form which acts as a...
5
by: Mark Deibert | last post by:
I'm a former VB6 coder. Quit a few years ago. Now I'm back and trying to teach myself VB.NET. I don't remember having this much difficulty learning VB6. I'm totally stuck on something and need your...
4
by: jcrouse | last post by:
I am using the following code to move a label on a form at runtime: If myMousedown = lblP1JoyRight.Name Then If lblP1JoyRight.BackColor.Equals(Color.Transparent) Then bTransCk = True ...
2
by: Dean Slindee | last post by:
It appears that I have two routines that don't play well together! First routine: a form's background is shaded with a gradient color. Second routine: then, the background of all labels on the...
1
by: FredC | last post by:
I'm using VS 2003, C#.Net 2003. I built a simple windows form app. with the following attributes: - has a BackgroundImage set to a jpeg I built a very simple user control that contains a...
8
by: Brian Ward | last post by:
I am looking for a simple way to set the image transparency in a PictureBox. I have a moving PictureBox containing a graphic image .. moving by incrementing its Left property. The background...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.