translucent panel...

B

Ben Callister

i need to be able to adjust the opacity of a Windows Forms 'Panel' control.
currently, this is available on Forms, but not on Panel controls. can
someone please show me a trick to make this happen with panels or any other
control that i can use without having to display another Window/Form?

much appreciated,
ben
 
L

Luke Zhang [MSFT]

Hi,Ben!

Thank you for your posting! From your post,my understanding on this issue
is How to set the "opacity"of a windows form control "panel". If I'm off
base,please feel free to let me know.

Actually there's no such a relevant atrribute for a panel's opacity in
vs.net. But we can do this using GDI+. we could contruct a custom control
derived from "Panel" class adding a special OnPaint method, and use this
custom control in your windows applications.

Here is a sample:

Option Explicit On
Option Strict On

Imports System.ComponentModel
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Public Class TransPanel
Inherits System.Windows.Forms.Panel

#Region " Component Designer generated code "

Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)

Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)

End Sub

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Component 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 Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region

Private _opacity As Double = 0.1

<Category("Appearance"), DefaultValue(GetType(Double), "0.1"),
Description("Gets or Sets the opacity level of the panel.")> _
Public Property Opacity() As Double
Get
Return _opacity
End Get
Set(ByVal Value As Double)
If Value < 0 OrElse Value > 1 Then
Throw New ArgumentException("Value must be between 0.00 and 1.00")
End If
_opacity = Value
Me.Invalidate()
End Set
End Property

Protected Overrides Sub OnMove(ByVal e As System.EventArgs)
Me.Invalidate()
End Sub

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

If Me.BackgroundImage Is Nothing Then
If Not (Parent.BackgroundImage Is Nothing) Then
e.Graphics.DrawImage(Parent.BackgroundImage, _
New Rectangle(0, 0, Me.Width, Me.Height), _
Me.Left, Me.Top, Me.Width, Me.Height, GraphicsUnit.Pixel)
Else
e.Graphics.FillRectangle(New SolidBrush(Parent.BackColor), _
New Rectangle(0, 0, Me.Width, Me.Height))
End If
End If

Dim attributes As New ImageAttributes

Dim matrixElements As Single()() = { _
New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 0.0F, CSng(_opacity), 0.0F}, _
New Single() {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}}

Dim matrix As ColorMatrix
matrix = New ColorMatrix(matrixElements)
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap)

Dim bitmap As Bitmap = New Bitmap(Me.Width, Me.Height,
PixelFormat.Format24bppRgb)
Dim g As Graphics = Graphics.FromImage(bitmap)
Dim brush As Brush = New SolidBrush(Me.BackColor)
g.FillRectangle(brush, New RectangleF(0, 0, Me.Width, Me.Height))
brush.Dispose()
e.Graphics.DrawImage(bitmap, _
New Rectangle(0, 0, Me.Width, Me.Height), _
0, 0, Me.Width, Me.Height, GraphicsUnit.Pixel, attributes)

End Sub

End Class


Please let me know if you have any other concerns, or need anything else.



Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
B

Bob Powell [MVP]

Try setting your form BackColor to a value created by Color.FromARGB such
as:

panel1.BackColor=Color.FromArgb(128,Color.Blue);

Unfortunately, neither my solution nor Lukes addresses the opacity of child
controls.

The Opacity system is really only available to toplevel windows because it
uses the LayeredWindow API under the covers.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Ben Callister

hi Bob-

when i try doing exactly as suggested below in my form's Load( ) event, it
indeed sets the background of the panel to blue, but its not translucent at
all. am i doing something wrong?

and are you saying that there is no way (yours or Luke's) to accomplish what
i need to do?

thanks and please advise,
ben
 
L

Linda Liu [MSFT]

Hi Ben,
Yes, the opacity is really only available to top level windows. The
solution using GDI+ to solve translucent panel problem is an optional
method. It could simulate opacity in some conditions but not all.
For example, if there's a button behind the "translucent"
panel, the button which should be visible is not visible actually. If the
"translucent" panel has a background image, the panel will not be
translucent any longer.
If you have any other question, feel free to ask.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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