6.17.2011

OpenIconDialog

If you noticed in your tool box, you've got your OpenFileDialog, SaveFileDialog, ColorDialog, ect...
But There Is No OpenIconDialog.
What we are going to do, is to use the PickIconDlg Api to show a dialog to choose an embedded icon in a file,it will return the index of the selected icon in the file, and then we use the ExtractIconEx Api to load the icon of that index to memory, get its handle and return a variable of type System.Drawing.Icon.
So basically, we made an OpenIconDialog.

Declare Unicode Function PickIconDlg Lib "shell32.dll" (ByVal hwnd As IntPtr, ByVal pszIconPath As StringBuilder, ByVal cchIconPath As Integer, ByRef piIconIndex As Integer) As Integer
    <DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function ExtractIconEx(ByVal stExeFileName As String, ByVal nIconIndex As Integer, ByVal phiconLarge As IntPtr(), ByVal phiconSmall As IntPtr(), ByVal nIcons As Integer) As Integer
    End Function
    Function OpenIconDialog(Optional ByVal IcoPath As String = "Shell32.dll") As Icon
        Dim buff As New StringBuilder(IcoPath, 500)
        Dim ind As Integer
        If PickIconDlg(Nothing, buff, buff.Capacity, ind) = 1 Then
            Dim bitm As New Bitmap(1, 1)
            Dim hIcon() As IntPtr = New IntPtr(0) {}
            If (ExtractIconEx(buff.ToString, ind, hIcon, Nothing, 1) = 1) AndAlso hIcon(0) <> IntPtr.Zero Then
                Return Icon.FromHandle(hIcon(0))
            End If
        End If
        Return Nothing
    End Function
It would look something like this :

Msdn recommends that we call DestroyIcon api after finishing of using the icon, but we don't need to add that api declaration to our code since the type System.Drawing.Icon has a method called Dispose, that will eventually cleanup and call the DestroyIcon api.

1 comment:

noor said...


شركة غسيل خزانات بمكة شركة غسيل خزانات بمكة
شركة غسيل خزانات بجدة شركة غسيل خزانات بجدة
شركة غسيل خزانات بالدمام شركة غسيل خزانات بالدمام
شركة غسيل خزانات بالمدينة المنورة شركة غسيل خزانات بالمدينة المنورة