Rubrik: Oberfläche · Effekte | VB-Versionen: VB2005, VB2008, VB2010 | 13.04.12 |
Einsatz einer DimmingForm Es wird eine Form vorgestellt, mit deren Hilfe man den gesamten Bildschirm auf verschiedene Arten mit transparenter Farbe überdecken und nur eine eigene Form im Vordergrund zeigen kann. | ||
Autor: Dietrich Herrmann | Bewertung: | Views: 10.019 |
ohne Homepage | System: WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Wenn man eine Form aus seinem Projekt gewissermaßen "hervorgehoben" anzeigen möchte, sodass nur sie im Vordergrund zu sehen und zu bedienen ist und der Rest des Bildschirms mit transparenter Farbe überdeckt oder mit transparentem Gradient verschiedener Farben oder einfach nur zu Graustufenbild gemacht wird, kann man die folgende spezielle Form einsetzen.
Man kreiert eine neue Form, macht sie randlos mit FormBorderStyle = None, setzt Opacity = 0%, ShowInTaskbar = False und benennt sie bspw. myDimmingForm. In die Form schreibt man folgenden Code:
Imports System.Windows.Forms Imports System.Drawing, System.Drawing.Drawing2D, System.Drawing.Imaging Public Class myDimmingForm #Region " Enumeration " Public Enum ShadowMode As Integer Darken DarkenGradient GreyOut End Enum #End Region #Region " Variables " Private theTimer As New Timer() Private theActiveShadowMode As ShadowMode = ShadowMode.GreyOut Private thegreyOutModeBaseColor As Color = Color.Black Private theOpacityStep As Single = 0.05 Private theOpacityMax As Single = 0.7 Private theGradientColor1 As Color Private theGradientColor2 As Color Private theGradientMode As LinearGradientMode Private theTimerInitWait As Integer = 1000 Private theTimerInterval As Integer = 1 Private theTimerIntervalSlowDownByOpacityFactor As Integer = 150 #End Region #Region " Public properties " Public Property ActiveShadowMode() As ShadowMode Get Return theActiveShadowMode End Get Set(ByVal value As ShadowMode) theActiveShadowMode = value End Set End Property Public Property GreyOutModeBaseColor() As Color Get Return thegreyOutModeBaseColor End Get Set(ByVal value As Color) thegreyOutModeBaseColor = value End Set End Property Public Property OpacityStep() As [Single] Get Return theOpacityStep End Get Set(ByVal value As [Single]) If value < 0 Then theOpacityStep = 0.01F ElseIf value > 1 Then theOpacityStep = 1.0F Else theOpacityStep = value End If End Set End Property Public Property OpacityMax() As [Single] Get Return theOpacityMax End Get Set(ByVal value As [Single]) If value < 0 Then theOpacityMax = 0 ElseIf value > 1 Then theOpacityMax = 1.0F Else theOpacityMax = value End If End Set End Property Public Property GradientColor1() As Color Get Return theGradientColor1 End Get Set(ByVal value As Color) theGradientColor1 = value End Set End Property Public Property GradientColor2() As Color Get Return theGradientColor2 End Get Set(ByVal value As Color) theGradientColor2 = value End Set End Property Public Property GradientMode() As LinearGradientMode Get Return theGradientMode End Get Set(ByVal value As LinearGradientMode) theGradientMode = value End Set End Property Public Property TimerInitWait() As Integer Get Return theTimerInitWait End Get Set(ByVal value As Integer) If value < 0 Then theTimerInitWait = 0 Else theTimerInitWait = value End If End Set End Property Public Property TimerInterval() As Integer Get Return theTimerInterval End Get Set(ByVal value As Integer) If value < 0 Then theTimerInterval = 0 Else theTimerInterval = value End If End Set End Property Public Property TimerIntervalSlowDownByOpacityFactor() As Integer Get Return theTimerIntervalSlowDownByOpacityFactor End Get Set(ByVal value As Integer) If value < 0 Then theTimerIntervalSlowDownByOpacityFactor = 0 Else theTimerIntervalSlowDownByOpacityFactor = value End If End Set End Property #End Region #Region " Private methods " Private Function GrayScaleImage(ByVal image As Image) As Image Dim imageAttributes As New ImageAttributes() Dim colorMatrixAttributes As Single()() ' Greyscale colorMatrixAttributes = New Single()() _ {New Single() {0.299F, 0.299F, 0.299F, 0, 0}, _ New Single() {0.588F, 0.588F, 0.588F, 0, 0}, _ New Single() {0.111F, 0.111F, 0.111F, 0, 0}, _ New Single() {0, 0, 0, 1, 0}, _ New Single() {0, 0, 0, 0, 1}} Dim colorMatrix As New ColorMatrix(colorMatrixAttributes) imageAttributes.SetColorMatrix(colorMatrix) Dim bitmap As Bitmap, gr As Graphics With image bitmap = New Bitmap(.Width, .Height, PixelFormat.Format32bppArgb) bitmap.SetResolution(.HorizontalResolution, .VerticalResolution) gr = Graphics.FromImage(bitmap) gr.DrawImage(image, New Rectangle(0, 0, bitmap.Width, bitmap.Height), _ 0, 0, .Width, .Height, GraphicsUnit.Pixel, imageAttributes) End With gr.Dispose() imageAttributes.Dispose() Return bitmap End Function Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) MyBase.OnPaint(e) End Sub #End Region #Region " Public methods " Public Sub ShowShadow() Me.Location = Screen.GetBounds(New Rectangle( _ Integer.MinValue \ 2, Integer.MaxValue \ 2, _ Integer.MaxValue, Integer.MaxValue)).Location Me.Size = New Size(Integer.MaxValue, Integer.MaxValue) Select Case ActiveShadowMode Case ShadowMode.GreyOut Dim bitmap As New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppArgb) Using gr As Graphics = Graphics.FromImage(bitmap) gr.CopyFromScreen(Me.Location, New Point(0, 0), Me.Size) End Using Me.BackgroundImage = GrayScaleImage(DirectCast(bitmap, Image)) Case ShadowMode.Darken Me.BackColor = GreyOutModeBaseColor Case ShadowMode.DarkenGradient Dim rect As New Rectangle(New Point(0, 0), Me.ClientSize) Dim image As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height) Dim gr As Graphics = Graphics.FromImage(image) Dim brush As New LinearGradientBrush(rect, theGradientColor1, _ theGradientColor2, theGradientMode) ' brush.SetBlendTriangularShape(2 / 3) gr.FillRectangle(brush, rect) gr.Dispose() brush.Dispose() Me.BackgroundImage = image End Select theTimer.Interval = TimerInitWait AddHandler theTimer.Tick, New EventHandler(AddressOf theTimerTick) theTimer.Enabled = True End Sub #End Region #Region " Eventhandler " Private Sub theTimerTick(ByVal sender As Object, ByVal e As EventArgs) theTimer.Interval = TimerInterval + _ Me.Opacity * TimerIntervalSlowDownByOpacityFactor Me.Opacity += OpacityStep If Me.Opacity >= OpacityMax Then theTimer.Enabled = False End Sub #End Region Private Sub Me_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None Me.ShowInTaskbar = False Me.Opacity = 0 Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or _ ControlStyles.AllPaintingInWmPaint Or _ ControlStyles.ResizeRedraw Or _ ControlStyles.UserPaint, True) ShowShadow() End Sub End Class
Damit ist die "DimmingForm" bereits konstruiert.
Man verwendet sie bspw. folgendermaßen: Soll die Form "theSpecForm" diejenige Form sein, die absolut im Vordergrund gezeigt werden soll inklusive DimmingFunktion für den Bildschirmhintergrund, bereitet man das Ganze vor mit dem Code:
With myDimmingForm .Owner = theSpecForm .ActiveShadowMode = myDimmingForm.ShadowMode.DarkenGradient .GradientColor1 = Color.Red .GradientColor2 = Color.Black .GradientMode = Drawing2D.LinearGradientMode.Horizontal .OpacityMax = 0.7 .Show() End With theSpecForm.Show()
Dadurch wird Folgendes initiiert:
theSpecForm wird zum Owner der DimmingForm gemacht (dadurch wird beim späteren Schließen auch gleichzeitig die DimmingForm geschlossen). Der ShadowMode wird gesetzt.
Es gibt drei verschiedene Arten dieses Modus:
- Darken: der Bildschirmhintergrund wird mit der angegebenen Farbe gleichmäßig bis zu einer vorgegebenen Transparenz überdeckt.
- DarkenGradient: es müssen zwei Farben definiert werden, die den Gradienten bilden; zusätzlich kann noch der Winkel des Gradienten definiert werden.
- GreyOut: der Bildschirmhintergrund wird zum Graustufenbild gemacht (ähnlich wie beim Herunterfahren des PC).
Oben gezeigtes Beispiel ist der Fall DarkenGradient. Im Fall Darken müssen natürlich nicht die Gradientfarben definiert werden, sondern bspw. nur:
.GreyOutModeBaseColor = Color.CornflowerBlue
Bei GreyOut braucht keine Farbe definiert zu werden.
Die Transparenzstufen oder auch die Timereinstellungen für die Geschwindigkeit des Dimmens können über die entsprechenden Eigenschaften der DimmingForm gesetzt werden und werden hier nicht näher erläutert.
Viel Spaß beim Experimentieren!