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. 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: Es gibt drei verschiedene Arten dieses Modus:
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! Dieser Tipp wurde bereits 10.025 mal aufgerufen. Voriger Tipp | Zufälliger Tipp | Nächster Tipp
Anzeige
Diesen und auch alle anderen Tipps & Tricks finden Sie auch auf unserer aktuellen vb@rchiv Vol.6 (einschl. Beispielprojekt!) Ein absolutes Muss - Geballtes Wissen aus mehr als 8 Jahren vb@rchiv! - nahezu alle Tipps & Tricks und Workshops mit Beispielprojekten - Symbol-Galerie mit mehr als 3.200 Icons im modernen Look Weitere Infos - 4 Entwickler-Vollversionen (u.a. sevFTP für .NET), Online-Update-Funktion u.v.m. |
vb@rchiv CD Vol.6 Geballtes Wissen aus mehr als 8 Jahren vb@rchiv! Online-Update-Funktion Entwickler-Vollversionen u.v.m. Tipp des Monats April 2024 Skyfloy Chart von Microsoft und dazu noch gratis Tutorial für Microsoft Chart Controls für Microsoft .NET Framework 3.5 Neu! sevCoolbar 3.0 Professionelle Toolbars im modernen Design! Mit sevCoolbar erstellen Sie in wenigen Minuten ansprechende und moderne Toolbars und passen diese optimal an das Layout Ihrer Anwendung an (inkl. große Symbolbibliothek) - für VB und MS-Access |
||||||||||||||||
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein. |