Get an applications icon

dannyres

Well-known member
Joined
Aug 29, 2003
Messages
67
Hi, im trying to work out how to get an applications taskbar icon. So far ive gotten it to work, but it only seems to work with .net applications. This is my code:

Code:
        Dim Handle As Integer = TheHandleToAnApp
        Dim hicon As Integer = SendMessage(Handle, WM_GETICON, ICON_SMALL, 0)

        MsgBox(hicon) the handle to the icon

        Dim ptr As New IntPtr(hicon)
        Dim i As Icon = Icon.FromHandle(ptr)

The problem is, with non .net applications SendMessage always returns 0. Ive tried with different things like ICON_BIG and ICON_SMALL2, but it always returns 0. Ive checked with spy++ and the window is definatly getting the WM_GETICON request but it just returns 0.

Can anybody help??



Thanks, Dan
 
have you not tried the ExtractIcon api? eg:
Code:
    Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As IntPtr

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path As String = "C:\Program Files\Messenger\msmsgs.exe"

        Dim x As IntPtr = ExtractIcon(Me.Handle.ToInt32, path, 0) /// if you change the 0 to 1 , 2 etc... you will get the sub-icons.
        Dim ico As Icon = Icon.FromHandle(x)
        Me.Icon = ico

    End Sub
 
That works alright for what i want todo usually, but what if the program chanes its icon at runtime? I need the actual icon that the program is using right at that moment like the alt-tab window or the taskbar does.

The wierd thing is, my code should work perfectly according to msdn etc. :(



Dan
 
Last edited by a moderator:
Back
Top