★搜Asp.net★(www.soAsp.net),为专业技术文档网站。
包括Asp.net开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档·VB.NET开发技术文档。
还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。
用VB.NET获取并保存图标文件
Private Sub Open()
Dim cdl As New OpenFileDialog
If cdl.ShowDialog = Windows.Forms.DialogResult.OK Then
ListView1.Items.Clear()
ImageList1.Images.Clear()
Dim iIcon_Num As Integer = Icon_Num(IntPtr.Zero, cdl.FileName, -1)
Me.ToolStripStatusLabel1.Text = cdl.FileName
Me.ToolStripStatusLabel2.Text = "共有 " & iIcon_Num.ToString() & " 个图标"
Dim i As Integer
For i = 0 To iIcon_Num - 1 Step 1
Dim j As UInt32
j = System.Convert.ToUInt32(i)
Dim hIcon As System.IntPtr = ExtractIcon(IntPtr.Zero, cdl.FileName, j)
ImageList1.Images.Add(Drawing.Icon.FromHandle(hIcon).ToBitmap)
ListView1.Items.Add("第" + (i + 1).ToString() + "个图标")
ListView1.Items(i).ImageIndex = i
Next i
End If
End Sub
Private Sub Save()
Dim cdl As New SaveFileDialog
If cdl.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim filename As String
filename = cdl.FileName
If filename.Substring(filename.LastIndexOf(".") + 1).ToLower <> "ico" Then
filename = filename & ".ico"
End If
Dim k As Integer = 0
Dim lVCItem As ListView.SelectedListViewItemCollection = New ListView.SelectedListViewItemCollection(ListView1)
k = lVCItem.Item(0).Index
Dim j As UInt32
j = System.Convert.ToUInt32(k)
Dim hIcon As System.IntPtr = ExtractIcon(IntPtr.Zero, Me.ToolStripStatusLabel1.Text, j)
Dim ico As System.Drawing.Icon = Drawing.Icon.FromHandle(hIcon)
Dim file As New System.IO.FileStream(filename, System.IO.FileMode.Create)
ico.Save(file)
file.Close()
Me.ToolStripStatusLabel1.Text = "文件保存 " & filename
End If
End Sub
'--------------------
Module ModuleMain
    <System.Runtime.InteropServices.DllImport("Shell32.dll")> Public Function ExtractIcon(ByVal src As System.IntPtr, ByVal strFileName As String, ByVal uiIconIndex As UInt32) As System.IntPtr
    End Function
    Declare Auto Function Icon_Num Lib "Shell32.dll" Alias "ExtractIcon" (ByVal src As System.IntPtr, ByVal strFileName As String, ByVal uiIconIndex As Integer) As Integer
End Module