Can opacity/transparency be adjusted within components?

  • Thread starter Thread starter Patrick Dugan
  • Start date Start date
P

Patrick Dugan

I know that I can change the opacity of an entire form, but is there a
way to control opacity to specific controls or components?

For example I want to create a form that is semi-transparent (maybe
looks like a sheet of green glass) but has elements or graphics on top
that I want to be opaque. Perhaps I want the sheet of glass to have
writing or other graphics "painted" on the surface. Is there some way
to mix varying transparencies within a form?
 
That is interesting but not what I was wanted. I want the form to be
transparent so that the desktop background appears through it while
other elements on that same form are opaque.
 
Hi Patrick,

Any controls on a form that have the same BackColor property as the
TransparencyKey property of the Form will be displayed Transparent. All
other controls will be displayed Opaque. So, firstly set your Form's
Transparency Key property to a particular color. Then set your Form's
BackColor to the same color as the TransparencyKey color. Now, to display
any control as opaque, make sure it's BackColor is different from the Form's
BackColor.

HTH,

Cerebrus.
 
Thanks for the reply. I'm not trying to make the form (or parts of it)
completely transparent. I want to adjust the opacity level (0% through
100%) on the form and use different opacity levels for other controls or
graphics within that form. Transparent colors are all or nothing and do not
contain a percentage of transparency. Since there are no opacity settings
for anything besides the form I assume that this is simply not possible.
 
Thanks Chris. I guess I'm not explaining what I am trying to do very well.
If I create a form and give it an opacity of 50% the form will appear
translucent on the desktop. Any control on that form will also be 50%
translucent.
I want the form itself to be 50% translucent but a control or picturebox on
that form to be completely opaque. I cannot make a control opaque while the
form is translucent but I want to. As an example think of an old green
glass coke bottle. The bottle itself is translucent but the graphics on the
bottle are opaque. I can see through the green glass but not through the
graphics. I want the form (or the background of that form) to be
translucent (not transparent) but have graphics on top that are opaque.
 
Sorry, I re-read it and the link I posted does not help. I don't know
how to do what you want.
 
I found this code which can be put in the sub New of a form:

If (Me.Site Is Nothing) OrElse (Me.Site.DesignMode = False)
Then
Me.frontForm = New Form
Dim tempList As New ArrayList(Me.Controls.Count)

For Each c As Control In Me.Controls
tempList.Add(c)
Next

For Each c As Control In tempList
Me.Controls.Remove(c)
Me.frontForm.Controls.Add(c)
Next

Me.Controls.Clear()
Me.AddOwnedForm(Me.frontForm)
Me.frontForm.BackColor = Color.Magenta
Me.frontForm.TransparencyKey = Color.Magenta
Me.frontForm.Show()
Me.SendToBack()
Me.frontForm.BringToFront()
Me.frontForm.Bounds = Me.Bounds
Me.frontForm.ShowInTaskbar = False
Me.frontForm.FormBorderStyle =
Windows.Forms.FormBorderStyle.None
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
End If


then set the form's opacity to something. It gives the desired effect,
but a way to move the form needs to be found.
 
What is the "frontform" in "Me.frontForm" ? I have tried changing the name
of the form to "frontForm" but that isn't recognized. The editor doesn't
recognize the "frontForm" associated with "Me" Is there an import I need to
add? I added "system.windows.forms.form" but it made no difference either.
I'd like to test that code but it won't compile at the moment.
 
I'm sorry. That is just another variable declared at the top of the
form:

Dim frontFomr As Form

The code just creates another form (frontForm), sets its background to
Magenta and makes that color transparent with the transparency key. It
then positions form directly over the top of the semi transparent form.
So there are two forms. The one on the bottom is the semi-transparent
one, the one on the top (frontForm) is completely transparent except
for the controls.

It's really kind of a hack.
 
Holy **** it works! Thanks! I have some code that can move the form:


Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
' This allows you to move the form around and still allow other mouse
capture events to come through...
Dim msg As New Message
Const WM_SYSCOMMAND As Long = &H112
Const MOUSE_MOVE As Long = &HF012
msg.HWnd = Me.Handle
msg.Msg = WM_SYSCOMMAND
msg.WParam = New IntPtr(MOUSE_MOVE)
Me.Capture = False
Call Me.WndProc(msg)
End Sub


However when I use this the form itself moves but the controls do not. Very
weird to see that. Still a neat trick though. Thanks!
 
Back
Top