473,883 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "constructo r" 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 10614
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.UserContr ol

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeCompo nent()

SetStyle(Contro lStyles.Support sTransparentBac kColor, True)

'Add any initialization after the InitializeCompo nent() call

Me.BackColor = Color.Transpare nt

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.Disp ose()

End If

End If

MyBase.Dispose( disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.Componen tModel.IContain er

'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

components = New System.Componen tModel.Containe r()

End Sub

#End Region

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

Dim g As Graphics = e.Graphics

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

Dim gBm As Graphics = Graphics.FromIm age(bm)

gBm.FillRectang le(Brushes.Blue , 0, 0, Me.Width, Me.Height)

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

gBm.FillRectang le(Brushes.Blue , 50, 50, 20, 20)

bm.MakeTranspar ent(Color.Blue)

g.DrawImage(bm, 0, 0)

End Sub

End Class
Ken
---------------------
"Grahammer" <po********@127 .0.0.1> wrote in message
news:em******** ******@TK2MSFTN GP11.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 "constructo r" 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******** ******@TK2MSFTN GP11.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 "constructo r" 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.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in
message news:%2******** *******@tk2msft ngp13.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.5072 05$6C4.482901@p d7tw1no...

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in message news:%2******** *******@tk2msft ngp13.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******** ******@TK2MSFTN GP09.phx.gbl...

"Phrederik" <po********@127 .0.0.1> wrote in message
news:HSvxb.5072 05$6C4.482901@p d7tw1no...

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in
message news:%2******** *******@tk2msft ngp13.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.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in
message news:%2******** ********@tk2msf tngp13.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******** ******@tk2msftn gp13.phx.gbl...

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in message news:%2******** ********@tk2msf tngp13.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
9513
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 control. My control draws a shape (circle, square, rectangle ...)
7
27566
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 transparent when leaving the mouse out of the rollover-box - background-color: none; seems not to work. Isn't it possible to do this with CSS?
1
20191
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 I prefer don't insert a background in MyComponent, and using the MyApp one as general background with MyComponent positionned, but I am not able to have MyComponent background transparent.
3
3115
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 Whiteboard. The Form contains 3
5
6469
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 expert guidance. I just want to load a GIF with a transparent background into a container with a transparent background. So I can move the GIFs over and under each other. You know, as in basic game stuff. I can load the transparent GIF no...
4
2576
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 lblP1JoyRight.BackColor = clrLabelMove
2
7594
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 form are made transparent. What results is the label's backcolor appearing as blocks of 'control' colored background. Setting the label's backcolor to color.transparent does not make it transparent (evidently the text is repainted with a...
1
3098
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 picture box set to an icon. The UserControl constructor sets SetStyle( ControlStyles.SupportTransparentBackColor, true); this.BackColor = Color.Transparent; The control does nothing except display the icon.
8
10188
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 however shows white as the PictureBox moves but I want it to be transparent. The PictureBox BackColor is set to Transparent .. but no affect. I have used PhotoShop to make the image background transparent .. again no affect. If I make the image a...
0
9943
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10419
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3237
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.