question

Farkler avatar image
0 Votes"
Farkler asked Farkler answered

VB.NET change background color or image in windows form application in the taskbar when user running multiple instances of application

Thanks for reading. We have a VB.NET windows form application running .NET 4.8 Framework. Users often run multiple instances of the application. When this occurs, all of the same colored minimized windows in the taskbar make it difficult for the user to distinguish the different instances. I would like to implement a background color change based on the users Process ID (System.Diagnostics.Process.GetCurrentProcess). For instance, if the user had 3 instances of the application running with 2 forms open apiece then there would be 6 windows in the taskbar of the same color and/or text. I would like to differentiate between those 3 instances if possible. Thanks for the help.

dotnet-visual-basicwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Note that the buttons depend on “Combine taskbar buttons” options of Taskbar.

Since the taskbar shows icons, maybe you can generate specific icons (coloured differently).

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

change background color or image in windows form application in the taskbar...

You can use ITaskbarList3.SetOverlayIcon to set a colored icon in the Taskbar Button, like in the MS SDK sample :

126397-itaskbarlist3.gif



itaskbarlist3.gif (5.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Basic logic would go into MyApplication class which is created from project properties, Application tab, View Application events button.

Something like this which as coded is limited to colors/open instances of an application.

 Imports Microsoft.VisualBasic.ApplicationServices
    
 Namespace My
     Partial Friend Class MyApplication
         Public backColorDictionary As Dictionary(Of Integer, Color) =
                    New Dictionary(Of Integer, Color) From
             {
                 {1, Color.Red},
                 {2, Color.Green},
                 {3, Color.Blue},
                 {4, Color.DarkOrange}
             }
    
         Private _color As Color
         ''' <summary>
         ''' Form background color
         ''' </summary>
         ''' <returns></returns>
         Public ReadOnly Property BackColor() As Color
             Get
                 Return _color
             End Get
         End Property
    
         ''' <summary>
         ''' Logic to determine background color to use or a default if more instance
         ''' of the application open than colors in <see cref="backColorDictionary"/>
         ''' </summary>
         ''' <param name="sender"></param>
         ''' <param name="e"></param>
         Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
    
             Dim currentProcess = Process.GetCurrentProcess().ProcessName
             Dim counter = Process.GetProcessesByName(currentProcess).Length
             If counter <= backColorDictionary.Count Then
                 _color = backColorDictionary(counter)               '
             Else
                 _color = Color.White
             End If
    
         End Sub
     End Class
 End Namespace

Then in a form load event

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
     BackColor = My.Application.BackColor


Update

Here we get over 100 colors

 Imports System.Reflection
 Imports Microsoft.VisualBasic.ApplicationServices
    
 Namespace My
     Partial Friend Class MyApplication
         Public backColorDictionary As Dictionary(Of Integer, Color) = CreateColorsDictionary()
    
         Public Shared Function ColorStructToList() As List(Of Color)
             Return GetType(Color).GetProperties(
                 BindingFlags.Static Or
                 BindingFlags.DeclaredOnly Or
                 BindingFlags.Public).Select(
                     Function(c)
                         Return CType(c.GetValue(Nothing, Nothing), Color)
                     End Function).ToList()
         End Function
         Private Function CreateColorsDictionary() As Dictionary(Of Integer, Color)
             Dim colors = ColorStructToList()
    
             Dim results = colors.Select(Function(item, index) New With
                                            {
                                                 .Index = index + 1,
                                                 .Color = item
                                            })
             Dim Dictionary = New Dictionary(Of Integer, Color)
    
    
             For Each anonymous In results
    
                 If anonymous.Color = Color.Transparent Then
                     Dictionary.Add(anonymous.Index, Color.White)
                 Else
                     Dictionary.Add(anonymous.Index, anonymous.Color)
                 End If
    
             Next
    
             Return Dictionary
    
         End Function
    
         Private _color As Color
         ''' <summary>
         ''' Form background color
         ''' </summary>
         ''' <returns></returns>
         Public ReadOnly Property BackColor() As Color
             Get
                 Return _color
             End Get
         End Property
    
         ''' <summary>
         ''' Logic to determine background color to use or a default if more instance
         ''' of the application open than colors in <see cref="backColorDictionary"/>
         ''' </summary>
         ''' <param name="sender"></param>
         ''' <param name="e"></param>
         Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
    
             Dim currentProcess = Process.GetCurrentProcess().ProcessName
             Dim counter = Process.GetProcessesByName(currentProcess).Length
             If counter <= backColorDictionary.Count Then
                 _color = backColorDictionary(counter)               '
             Else
                 _color = Color.White
             End If
    
         End Sub
     End Class
 End Namespace


Opening three instances gets
126441-figure1.png




figure1.png (26.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Farkler avatar image
0 Votes"
Farkler answered

very helpful both Castorix31 and Karen. Thanks!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.