Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Get external applications icons

$
0
0
hello, i am developing a custom taskbar similar to Windows Taskbar. So far, i got the running applications and added a control for each of them to my taskbar. Now, i want to get the icons of those applications running in Windows Taskbar. So far, i've tried it this way, with no luck:

 <DllImportAttribute("user32.dll", EntryPoint:="SendMessageW")> _
    Public Shared Function SendMessage(<InAttribute()> ByVal hWnd As System.IntPtr, ByVal Msg As UInteger, ByVal wParam As UInteger, ByVal lParam As Integer) As IntPtr
    End Function
    Public Const WM_GETICON As UInteger = &H7F

    Public Shared Function GetWindowIcon(ByVal WindowHandle As IntPtr) As Icon
        Dim IconHandle As IntPtr = SendMessage(WindowHandle, WM_GETICON, 0, 0)
        If Not IconHandle = IntPtr.Zero Then
            Return Icon.FromHandle(IconHandle)
        Else
            Return Nothing
        End If
    End Function
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each processname As Process In Process.GetProcesses
            If processname.MainWindowTitle = "Untitled - Notepad" Then
                Dim x As Icon = GetWindowIcon(processname.Handle)
                'PictureBox1.Image = GetWindowIcon(processname.Handle).ToBitmap
                Me.Icon = x
            End If
        Next
    End Sub


- which does nothing, and like this:

   <StructLayout(LayoutKind.Sequential)> _
    Private Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As IntPtr
        Public dwAttributes As UInteger
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure

    <DllImport("shell32.dll")> _
    Private Shared Function SHGetFileInfo(ByRef path As String, ByRef fileAttributes As UInteger, _
ByRef fileInfo As SHFILEINFO, ByRef sizeInfo As UInteger, ByRef flags As UInteger) As IntPtr
    End Function

    Public Function GetApplicationIcon(ByRef fileName As String, ByRef sizeType As String) As Icon
        Dim iconHandle As IntPtr
        Dim fileInfo As New SHFILEINFO()

        Try
            Select Case sizeType.ToLower()
                Case "small"
                    iconHandle = SHGetFileInfo(fileName, 0, fileInfo, CInt(Marshal.SizeOf(fileInfo)), 256 Or 1)
                    Exit Select
                Case "large"
                    iconHandle = SHGetFileInfo(fileName, 0, fileInfo, CInt(Marshal.SizeOf(fileInfo)), 256 Or 0)
                    Exit Select
                Case Else
                    iconHandle = SHGetFileInfo(fileName, 0, fileInfo, CInt(Marshal.SizeOf(fileInfo)), 256 Or 1)
                    Exit Select
            End Select

            Return System.Drawing.Icon.FromHandle(fileInfo.hIcon)
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
            Return Nothing
        End Try
    End Function
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Icon = GetApplicationIcon("Notepad.exe", "small")
    End Sub


which throws an error of kind: "object reference not set to an instance of an object. What am i doing wrong and what is the right way to get the icon of an external program an display it as a bitmap in a picturebox?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>