Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thanks to help from a Code Project member (D.K.), I have created an app (Win 7) that modifies its taskbar icon, displaying a numerical value that is important to us. (Note: on the Taskbar, not the Notification bar.)

Everything works fine when the program is run from the local workstation. But when I place the .exe on a server and create a local shortcut to it on the desktop, the modified icon does not appear -- only the icon specified in the App settings under Visual Studio. (And if I remove that icon spec, I get the stock "no-icon" icon.)

I checked the local firewall -- since the program runs (only the icon fails to present itself), I couldn't see how it could be the firewall, but to be sure I added the .exe and granted it access.

All users have access rights to the folder on the server.

I've searched CP for "icon" and not found anything; Google came up dry for me as well.

The server is Windows Server 2003 R2.
The app is a Windows Form app.

Here is the code for handling the icon -- 3 functions; the first is the main one, the second formats the number displayed in the icon, and the third picks a font that allows the number to fit in the space of an icon:

Call: Me.Icon = MakeIcon(CDbl(total)) 'updates the taskbar icon with the Total value

Functions:
VB
Private Function MakeIcon(ByVal val As Double) As Icon
        Dim bmp As New Bitmap(64, 64)
        Dim g As Graphics = Graphics.FromImage(bmp)

        'Set background color: if value is positive, DarkBlue; if negative, Maroon.
        g.FillRectangle(IIf(val >= 0, Brushes.DarkBlue, Brushes.Maroon), 0, 0, 64, 64)

        'Pick the largest font that allows the text to fit inside the icon.
        currTaskbarFont = ChooseFont(g, val, bmp)

        Dim s As String = ConvertToKilosAndMegs(val)
        Dim w As SizeF = g.MeasureString(s, currTaskbarFont)

        'Center the text within the icon.
        g.DrawString(s, currTaskbarFont, Brushes.White, _
                     (bmp.Width - w.Width) / 2, (bmp.Height - w.Height) / 2)

        Return Drawing.Icon.FromHandle(bmp.GetHicon)
    End Function

    Private Function ConvertToKilosAndMegs(ByVal val As Double) As String
        Dim sResults As String

        Select Case Math.Abs(val)
            Case Is > 9999.9999  'if >= 10,000, switch to K format
                sResults = (val * 0.001).ToString("F0") & "K"
            Case Is > 9999999.9999  'if >= 10,000,000, switch to M format
                sResults = (val * 0.000001).ToString("F0") & "M"
            Case Else
                sResults = val.ToString("F0")
        End Select

        Return sResults
    End Function

    Private Function ChooseFont(ByRef g As Graphics, ByVal val As Double, ByRef container As Bitmap) As Font
        Dim myFont As Font = fnt(0)
        Try
            For i As Integer = 1 To TASKBAR_FONT_COUNT - 1
                If g.MeasureString(val.ToString("F0"), fnt(i)).Width >= container.Width * 1.1 Then
                    myFont = fnt(i - 1)
                    Exit For
                Else
                    myFont = fnt(i)
                End If
            Next
        Catch ex As Exception
            MsgBox("An error has been encountered in the routine that selects " _
                   & "the optimum font size for the Taskbar Icon." & vbCrLf _
                   & "The icon may not display the Total P&L value properly.", _
                    MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Error with Taskbar Icon")
        End Try
        Return myFont
    End Function


Any suggestions would be much appreciated!
Posted
Updated 5-Jul-11 3:53am
v5
Comments
AspDotNetDev 30-Jun-11 15:15pm    
1) Is the server also Windows 7? 2) Edit your question and post the code you are using to modify the taskbar icon. 3) Make sure you include the code that loads the icon (e.g., if it loads it over a URL or from a resource or from the file system).
AspDotNetDev 30-Jun-11 15:16pm    
Also, is this a Windows Forms application? WPF?

1 solution

Finally got it working, albeit in a somewhat odd way. I created a .BAT file that kicks of the executable, then used a freeware utility that converts a .BAT file into an .EXE. I was able to give this .EXE an icon, and it displays on the end user workstation the way we wish it to.

Still don't know why the icon would not pass through when running the app's executable sans .BAT file, but at least I found a workaround.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900