From 8705fa5f2063883f954ae29d2f4a022e76df338f Mon Sep 17 00:00:00 2001 From: Elektro Date: Wed, 19 Jun 2019 16:28:10 +0200 Subject: [PATCH] Fixed a visual issue with the menu strip. Fixed a visual issue with the menu strip. --- src/DevCase/Extensions/ControlExtensions.vb | 177 +++++++++++++++++--- src/DevCase/Extensions/FormExtensions.vb | 62 ++++--- src/DevCase/VisualStyle.vb | 48 ++++++ src/Easy Link File Viewer.vbproj | 1 + src/My Project/AssemblyInfo.vb | 4 +- src/UI/Form1.vb | 9 +- 6 files changed, 247 insertions(+), 54 deletions(-) create mode 100644 src/DevCase/VisualStyle.vb diff --git a/src/DevCase/Extensions/ControlExtensions.vb b/src/DevCase/Extensions/ControlExtensions.vb index 686cd2b..dac2284 100644 --- a/src/DevCase/Extensions/ControlExtensions.vb +++ b/src/DevCase/Extensions/ControlExtensions.vb @@ -1,11 +1,11 @@ ' This source-code is freely distributed as part of "DevCase for .NET Framework". -' -' Maybe you would like to consider to buy this powerful set of libraries to support me. +' +' Maybe you would like to consider to buy this powerful set of libraries to support me. ' You can do loads of things with my apis for a big amount of diverse thematics. -' +' ' Here is a link to the purchase page: ' https://codecanyon.net/item/elektrokit-class-library-for-net/19260282 -' +' ' Thank you. #Region " Option Statements " @@ -19,7 +19,10 @@ Option Infer Off #Region " Imports " Imports System.ComponentModel +Imports System.Linq.Expressions +Imports System.Reflection Imports System.Runtime.CompilerServices +Imports DevCase.Core.Application.UserInterface #End Region @@ -39,18 +42,18 @@ Namespace DevCase.Core.Extensions ''' ---------------------------------------------------------------------------------------------------- ''' - ''' Iterate through all the controls in the source + ''' Iterate through all the controls in the source ''' and performs the specified action on each one. ''' ''' ---------------------------------------------------------------------------------------------------- ''' ''' The source . ''' - ''' + ''' ''' ''' If , iterates through controls of child controls too. ''' - ''' + ''' ''' ''' An to perform on each control. ''' @@ -59,27 +62,27 @@ Namespace DevCase.Core.Extensions Public Sub ForEachControl(ByVal parent As Control, ByVal recursive As Boolean, ByVal action As Action(Of Control)) - ControlExtensions.ForEachControl(Of Control)(parent, recursive, action) + parent.ForEachControl(Of Control)(recursive, action) End Sub ''' ---------------------------------------------------------------------------------------------------- ''' - ''' Iterate through all the controls of the specified type in the source + ''' Iterate through all the controls of the specified type in the source ''' and performs the specified action on each one. ''' ''' ---------------------------------------------------------------------------------------------------- ''' ''' The type of the control. ''' - ''' + ''' ''' ''' The source . ''' - ''' + ''' ''' ''' If , iterates through controls of child controls too. ''' - ''' + ''' ''' ''' An to perform on each control. ''' @@ -91,24 +94,59 @@ Namespace DevCase.Core.Extensions For Each ctrl As Control In parent.Controls.OfType(Of T) action(ctrl) If (recursive) Then - ControlExtensions.ForEachControl(Of T)(ctrl, recursive, action) + ctrl.ForEachControl(Of T)(recursive, action) End If Next ctrl End Sub ''' ---------------------------------------------------------------------------------------------------- ''' - ''' Changes the color appearance of the source to its default appearance. + ''' Changes the color appearance of the source using the specified style. ''' ''' ---------------------------------------------------------------------------------------------------- ''' ''' The source . ''' + ''' + ''' + ''' The visual style. + ''' ''' ---------------------------------------------------------------------------------------------------- - Public Sub SetThemeDefault(ByVal ctrl As Control) + Public Sub SetVisualStyle(ByVal ctrl As Control, ByVal style As VisualStyle) + + Select Case style + + Case VisualStyle.Default + ControlExtensions.Internal_SetThemeDefault(ctrl) + + Case VisualStyle.VisualStudioDark + ControlExtensions.Internal_SetThemeVisualStudioDark(ctrl) + + Case Else + Throw New InvalidEnumArgumentException(NameOf(style), style, GetType(VisualStyle)) + + End Select + + End Sub + +#End Region + +#Region " Private Methods " + + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' Changes the color appearance of the source to its default appearance. + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' The source . + ''' + ''' ---------------------------------------------------------------------------------------------------- + + Private Sub Internal_SetThemeDefault(ByVal ctrl As Control) If ctrl.GetType() = GetType(Button) Then ctrl.BackColor = Button.DefaultBackColor @@ -181,8 +219,17 @@ Namespace DevCase.Core.Extensions ctrl.ForeColor = MonthCalendar.DefaultForeColor ElseIf ctrl.GetType() = GetType(MenuStrip) Then - ctrl.BackColor = MenuStrip.DefaultBackColor - ctrl.ForeColor = MenuStrip.DefaultForeColor + Dim menu As MenuStrip = DirectCast(ctrl, MenuStrip) + menu.BackColor = MenuStrip.DefaultBackColor + menu.ForeColor = MenuStrip.DefaultForeColor + For Each menuItem As ToolStripMenuItem In menu.Items.OfType(Of ToolStripMenuItem) + RemoveHandler menuItem.MouseEnter, AddressOf ControlExtensions.ToolStripMenuItem_MouseEnter_VisualStudioDark + RemoveHandler menuItem.MouseLeave, AddressOf ControlExtensions.ToolStripMenuItem_MouseLeave_VisualStudioDark + RemoveHandler menuItem.DropDownClosed, AddressOf ControlExtensions.ToolStripMenuItem_DropDownClosed_VisualStudioDark + + menuItem.BackColor = MenuStrip.DefaultBackColor + menuItem.ForeColor = MenuStrip.DefaultForeColor + Next ElseIf ctrl.GetType() = GetType(NumericUpDown) Then ctrl.BackColor = NumericUpDown.DefaultBackColor @@ -289,7 +336,7 @@ Namespace DevCase.Core.Extensions ctrl.ForeColor = WebBrowserBase.DefaultForeColor Else - Throw New NotImplementedException($"A color appearance for the specified control type is not implemented: '{ctrl.GetType().FullName}'") + Throw New NotImplementedException($"A visual style for the specified control type is not implemented: '{ctrl.GetType().FullName}'") End If @@ -305,9 +352,7 @@ Namespace DevCase.Core.Extensions ''' ''' ---------------------------------------------------------------------------------------------------- - - - Public Sub SetThemeVisualStudioDark(ByVal ctrl As Control) + Private Sub Internal_SetThemeVisualStudioDark(ByVal ctrl As Control) If ctrl.GetType() = GetType(Button) Then ctrl.BackColor = Color.FromArgb(255, 37, 37, 38) @@ -380,8 +425,22 @@ Namespace DevCase.Core.Extensions ctrl.ForeColor = Color.Gainsboro ElseIf ctrl.GetType() = GetType(MenuStrip) Then - ctrl.BackColor = Color.FromArgb(255, 45, 45, 48) - ctrl.ForeColor = Color.Gainsboro + Dim menu As MenuStrip = DirectCast(ctrl, MenuStrip) + menu.BackColor = Color.FromArgb(255, 45, 45, 48) + menu.ForeColor = Color.Gainsboro + + For Each menuItem As ToolStripMenuItem In menu.Items.OfType(Of ToolStripMenuItem) + RemoveHandler menuItem.MouseEnter, AddressOf ControlExtensions.ToolStripMenuItem_MouseEnter_VisualStudioDark + RemoveHandler menuItem.MouseLeave, AddressOf ControlExtensions.ToolStripMenuItem_MouseLeave_VisualStudioDark + RemoveHandler menuItem.DropDownClosed, AddressOf ControlExtensions.ToolStripMenuItem_DropDownClosed_VisualStudioDark + + menuItem.BackColor = Color.FromArgb(255, 45, 45, 48) + menuItem.ForeColor = Color.Gainsboro + + AddHandler menuItem.MouseEnter, AddressOf ControlExtensions.ToolStripMenuItem_MouseEnter_VisualStudioDark + AddHandler menuItem.MouseLeave, AddressOf ControlExtensions.ToolStripMenuItem_MouseLeave_VisualStudioDark + AddHandler menuItem.DropDownClosed, AddressOf ControlExtensions.ToolStripMenuItem_DropDownClosed_VisualStudioDark + Next ElseIf ctrl.GetType() = GetType(NumericUpDown) Then ctrl.BackColor = Color.FromArgb(255, 37, 37, 38) @@ -488,13 +547,81 @@ Namespace DevCase.Core.Extensions ctrl.ForeColor = Color.Gainsboro Else - MsgBox(ctrl.GetType().IsNotPublic.ToString) + Throw New NotImplementedException($"A visual style for the specified control type is not implemented: '{ctrl.GetType().FullName}'") + End If - Throw New NotImplementedException($"A color appearance for the specified control type is not implemented: '{ctrl.GetType().FullName}'") + End Sub +#End Region + +#Region " Event Handlers " + + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' Handles the event for controls + ''' that has the style applied. + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' The source of the event. + ''' + ''' + ''' + ''' The instance containing the event data. + ''' + ''' ---------------------------------------------------------------------------------------------------- + Private Sub ToolStripMenuItem_MouseEnter_VisualStudioDark(ByVal sender As Object, ByVal e As EventArgs) + Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) + item.ForeColor = Color.Black + End Sub + + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' Handles the event for controls + ''' that has the style applied. + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' The source of the event. + ''' + ''' + ''' + ''' The instance containing the event data. + ''' + ''' ---------------------------------------------------------------------------------------------------- + Private Sub ToolStripMenuItem_MouseLeave_VisualStudioDark(ByVal sender As Object, ByVal e As EventArgs) + Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) + If item.Pressed Then + item.ForeColor = Color.Black + Else + item.ForeColor = Color.Gainsboro End If + End Sub + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' Handles the event for controls + ''' that has the style applied. + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' The source of the event. + ''' + ''' + ''' + ''' The instance containing the event data. + ''' + ''' ---------------------------------------------------------------------------------------------------- + Private Sub ToolStripMenuItem_DropDownClosed_VisualStudioDark(ByVal sender As Object, ByVal e As EventArgs) + Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) + item.ForeColor = Color.Gainsboro End Sub #End Region diff --git a/src/DevCase/Extensions/FormExtensions.vb b/src/DevCase/Extensions/FormExtensions.vb index 49e0e01..f3051ef 100644 --- a/src/DevCase/Extensions/FormExtensions.vb +++ b/src/DevCase/Extensions/FormExtensions.vb @@ -20,6 +20,7 @@ Option Infer Off Imports System.ComponentModel Imports System.Runtime.CompilerServices +Imports DevCase.Core.Application.UserInterface #End Region @@ -39,13 +40,17 @@ Namespace DevCase.Core.Extensions ''' ---------------------------------------------------------------------------------------------------- ''' - ''' Changes the color appearance of the source to its default appearance. + ''' Changes the color appearance of the source using the specified style. ''' ''' ---------------------------------------------------------------------------------------------------- ''' ''' The source . ''' ''' + ''' + ''' The visual style. + ''' + ''' ''' ''' to change the color appearance of child controls too. ''' @@ -53,46 +58,57 @@ Namespace DevCase.Core.Extensions - Public Sub SetThemeDefault(ByVal f As Form, ByVal childControls As Boolean) - f.BackColor = Form.DefaultBackColor - f.ForeColor = Form.DefaultForeColor + Public Sub SetVisualStyle(ByVal f As Form, ByVal style As VisualStyle, ByVal childControls As Boolean) + + Select Case style + + Case VisualStyle.Default + f.BackColor = Form.DefaultBackColor + f.ForeColor = Form.DefaultForeColor + + + Case VisualStyle.VisualStudioDark + f.BackColor = Color.FromArgb(255, 45, 45, 48) + f.ForeColor = Color.Gainsboro + + Case Else + Throw New InvalidEnumArgumentException(NameOf(style), style, GetType(VisualStyle)) + + End Select If (childControls) Then - ForEachControl(f, True, Sub(ctrl As Control) - If (ctrl.GetType().IsPublic) Then - ctrl.SetThemeDefault() - End If - End Sub) + f.ForEachControl(True, + Sub(ctrl As Control) + If (ctrl.GetType().IsPublic) Then + ctrl.SetVisualStyle(style) + End If + End Sub) End If End Sub ''' ---------------------------------------------------------------------------------------------------- ''' - ''' Changes the color appearance of the source to Visual Studio Dark Theme appearance. + ''' Iterate through all the controls in the source + ''' and performs the specified action on each one. ''' ''' ---------------------------------------------------------------------------------------------------- ''' ''' The source . ''' ''' - ''' - ''' to change the color appearance of child controls too. + ''' + ''' If , iterates through controls of child controls too. + ''' + ''' + ''' + ''' An to perform on each control. ''' ''' ---------------------------------------------------------------------------------------------------- - Public Sub SetThemeVisualStudioDark(ByVal f As Form, ByVal childControls As Boolean) - f.BackColor = Color.FromArgb(255, 45, 45, 48) - f.ForeColor = Color.Gainsboro - - If (childControls) Then - ForEachControl(f, True, Sub(ctrl As Control) - If (ctrl.GetType().IsPublic) Then - ctrl.SetThemeVisualStudioDark() - End If - End Sub) - End If + Public Sub ForEachControl(ByVal f As Form, ByVal recursive As Boolean, ByVal action As Action(Of Control)) + f.ForEachControl(Of Control)(recursive, action) End Sub #End Region diff --git a/src/DevCase/VisualStyle.vb b/src/DevCase/VisualStyle.vb new file mode 100644 index 0000000..2268398 --- /dev/null +++ b/src/DevCase/VisualStyle.vb @@ -0,0 +1,48 @@ +' This source-code is freely distributed as part of "DevCase for .NET Framework". +' +' Maybe you would like to consider to buy this powerful set of libraries to support me. +' You can do loads of things with my apis for a big amount of diverse thematics. +' +' Here is a link to the purchase page: +' https://codecanyon.net/item/elektrokit-class-library-for-net/19260282 +' +' Thank you. + + +#Region " Option Statements " + +Option Strict On +Option Explicit On +Option Infer Off + +#End Region + +#Region " VisualStyle " + +' ReSharper disable once CheckNamespace + +Namespace DevCase.Core.Application.UserInterface + + + ''' ---------------------------------------------------------------------------------------------------- + ''' + ''' Specifies a visual style for controls. + ''' + ''' ---------------------------------------------------------------------------------------------------- + Public Enum VisualStyle As Integer + + ''' + ''' Default style. + ''' + [Default] = 0 + + ''' + ''' Visual Studio's dark style. + ''' + VisualStudioDark = 1 + + End Enum + +End Namespace + +#End Region diff --git a/src/Easy Link File Viewer.vbproj b/src/Easy Link File Viewer.vbproj index 27f1129..d3d6e3e 100644 --- a/src/Easy Link File Viewer.vbproj +++ b/src/Easy Link File Viewer.vbproj @@ -139,6 +139,7 @@ Settings.settings True + diff --git a/src/My Project/AssemblyInfo.vb b/src/My Project/AssemblyInfo.vb index 1556cd3..7c976b7 100644 --- a/src/My Project/AssemblyInfo.vb +++ b/src/My Project/AssemblyInfo.vb @@ -32,6 +32,6 @@ Imports System.Runtime.InteropServices ' by using the '*' as shown below: ' - - + + diff --git a/src/UI/Form1.vb b/src/UI/Form1.vb index fd59905..40e47d6 100644 --- a/src/UI/Form1.vb +++ b/src/UI/Form1.vb @@ -19,6 +19,7 @@ Imports System.IO Imports System.Threading Imports DevCase.Core.Application.Tools +Imports DevCase.Core.Application.UserInterface Imports DevCase.Core.Extensions Imports DevCase.Core.IO Imports DevCase.Core.IO.Tools @@ -726,15 +727,15 @@ Friend NotInheritable Class Form1 : Inherits Form Select Case My.Settings.VisualThemeIndex Case 0 ' Default theme - Me.SetThemeDefault(True) - My.Forms.AboutBox1.SetThemeDefault(True) + Me.SetVisualStyle(VisualStyle.Default, True) + My.Forms.AboutBox1.SetVisualStyle(VisualStyle.Default, True) Me.DefaultToolStripMenuItem.Checked = True Me.DarkToolStripMenuItem.Checked = False Case 1 ' Dark theme - Me.SetThemeVisualStudioDark(True) - My.Forms.AboutBox1.SetThemeVisualStudioDark(True) + Me.SetVisualStyle(VisualStyle.VisualStudioDark, True) + My.Forms.AboutBox1.SetVisualStyle(VisualStyle.VisualStudioDark, True) Me.DefaultToolStripMenuItem.Checked = False Me.DarkToolStripMenuItem.Checked = True