' ***********************************************************************
' Author : Elektro
' Modified : 24-July-2015
' ***********************************************************************
' <copyright file="TransparentControl.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
#Region " Option Statements "
Option Explicit On
Option Strict On
Option Infer Off
#End Region
#Region " Imports "
Imports System
Imports System.Drawing
Imports System.Windows.Forms
#End Region
#Region " TransparentControl "
''' <summary>
''' A transparent <see cref="Control"/>.
''' </summary>
Public NotInheritable Class TransparentControl : Inherits Control
#Region " Properties "
''' <summary>
''' Gets or sets the background image displayed in the control.
''' </summary>
''' <value>The background image.</value>
Public Overloads Property BackgroundImage As Image
Get
Return Me.backgroundImageB
End Get
Set(ByVal value As Image)
Me.backgroundImageB = value
MyBase.RecreateHandle()
End Set
End Property
''' <summary>
''' ( Backing Field )
''' The background image displayed in the control.
''' </summary>
Private backgroundImageB As Image
''' <summary>
''' Gets or sets the image displayed in the control.
''' </summary>
''' <value>The image.</value>
Public Overloads Property Image As Image
Get
Return Me.imageB
End Get
Set(ByVal value As Image)
Me.imageB = value
MyBase.RecreateHandle()
End Set
End Property
''' <summary>
''' ( Backing Field )
''' The background image displayed in the control.
''' </summary>
Private imageB As Image
''' <summary>
''' Gets the background color for the control.
''' </summary>
''' <value>The background color.</value>
<EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows ReadOnly Property BackColor() As Color
Get
Return Color.Transparent
End Get
End Property
''' <summary>
''' Gets the required creation parameters when the control's handle is created.
''' </summary>
''' <value>The creation parameters.</value>
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = (cp.ExStyle Or 32)
Return cp
End Get
End Property
#End Region
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="TransparentControl"/> class.
''' </summary>
Public Sub New()
End Sub
#End Region
#Region " Event-Handlers "
''' <summary>
''' Handles the <see cref="E:Control.Paint"/> event.
''' </summary>
''' <param name="e">A <see cref="T:Forms.PaintEventArgs"/> that contains the event data.</param>
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
If Me.backgroundImageB IsNot Nothing Then ' Draw background image (filled).
e.Graphics.DrawImage(Me.backgroundImageB, Me.ClientRectangle)
End If
If Me.imageB IsNot Nothing Then ' Draw image (centered).
e.Graphics.DrawImage(Me.imageB,
((Me.ClientRectangle.Width \ 2) - (Me.imageB.Width \ 2)),
CSng((Me.ClientRectangle.Height / 2) - (Me.imageB.Height / 2)))
End If
End Sub
''' <summary>
''' Handles the <see cref="E:PaintBackground" /> event.
''' </summary>
''' <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
' Ignore painting (don't raise PaintBackground event).
End Sub
#End Region
End Class
#End Region