i want to have a listview that will list files to get at runtime their icons..is there a .NET way to do this or will i have to stick to the API way? if yes anyone knows links which have it?
tks..
Printable View
i want to have a listview that will list files to get at runtime their icons..is there a .NET way to do this or will i have to stick to the API way? if yes anyone knows links which have it?
tks..
Dude, I have this... I upped it on PSC give me a minuet...
The code will display the correct Icon based on the file type.
Damn... It's not there.... I have it at home somewhere.
all it is a empty image list and a array(x,1) and I extract the Icon from the file and add it to the list and update an array with the file extension and ImageList index. if the file extension is already in the array then I return the ImageList index instead of extracting the icon.
if u dont take too long i can wait for u to get home..if its not much trouble!
let me see if I can throw somthing together real quick I won't be home until late tonight.
mean while maybe you can help me out with my problem about Painting to the Desktop with GDI+
ok I got something worked out.. and its is working... Let me Clean it up and then I will post it...
ok tks
This is awsome!!!!
to return an Icon use thisCode:Public Class ExtractIcon
Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As Integer
Declare Function DrawIcon Lib "user32" Alias "DrawIcon" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal hIcon As Integer) As Integer
Public Shared Function GetIcon(ByVal Path As String) As System.Drawing.Icon
If System.IO.File.Exists(Path) Then
Dim ExtReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
Dim AppReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
Dim Extension As String = System.IO.Path.GetExtension(Path)
ExtReg = ExtReg.OpenSubKey(Extension)
Dim xStr As String = ExtReg.GetValue("")
AppReg = AppReg.OpenSubKey(xStr & "\DefaultIcon")
Dim IconData() As String
IconData = Split(AppReg.GetValue(""), ",")
Dim hIcon As Integer
hIcon = ExtractIcon(0, IconData(0), Int(IconData(1)))
GetIcon = System.Drawing.Icon.FromHandle(IntPtr.op_Explicit(hIcon))
End If
End Function
End Class
to return an image use thisCode:Me.Icon = ExtractIcon.GetIcon("path\filename.txt")
Code:PictureBox1.Image = ExtractIcon.GetIcon("path\filename.txt").ToBitmap
ok tks:D
now if you want to show the icons in a listview create a ImageList and add the icons to the image list then attach the image list to the listview control....
Let me know how you make out...
Example Application...
*NOTE this only gets Icons that are associated with windows.
Form Code:
Code:Dim WithEvents LstView As ListView
Dim WithEvents BntGo As Button
Dim WithEvents TxtBox As TextBox
Dim WithEvents ImgList As ImageList
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LstView = New ListView()
BntGo = New Button()
TxtBox = New TextBox()
ImgList = New ImageList()
LstView.View = View.List
LstView.Location = New Point(20, 20)
LstView.Width = 500
LstView.Height = 250
TxtBox.Location = New Point(20, 270)
TxtBox.Width = 440
TxtBox.Text = "c:\"
BntGo.Location = New Point(460, 270)
BntGo.Width = 60
BntGo.Height = TxtBox.Height
BntGo.Text = "Run"
Me.Width = 550
Me.Height = 320 + TxtBox.Height
Me.StartPosition = FormStartPosition.CenterScreen
Me.Controls.Add(LstView)
Me.Controls.Add(TxtBox)
Me.Controls.Add(BntGo)
End Sub
Private Sub BntGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BntGo.Click
If System.IO.Directory.Exists(TxtBox.Text) Then
LstView.Items.Clear()
ImgList.Images.Clear()
Dim Files As Array
Files = System.IO.Directory.GetFiles(TxtBox.Text)
If Files.Length > 0 Then
Dim i As Integer
For i = 0 To Files.Length - 1
Dim xIcon As System.Drawing.Icon
xIcon = ExtractIcon.GetIcon(Files(i))
ImgList.Images.Add(xIcon)
Dim xItem As New ListViewItem(System.IO.Path.GetFileName(Files(i)), ImgList.Images.Count - 1)
LstView.Items.Add(xItem)
Next
LstView.SmallImageList = ImgList
LstView.LargeImageList = ImgList
End If
End If
End Sub
CLASS CODE (revised)
Code:Public Class ExtractIcon
Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As Integer
Declare Function DrawIcon Lib "user32" Alias "DrawIcon" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal hIcon As Integer) As Integer
Public Shared Function GetIcon(ByVal Path As String) As System.Drawing.Icon
If System.IO.File.Exists(Path) Then
Dim hIcon As Integer
On Error GoTo ErrHandle
Dim Extension As String = System.IO.Path.GetExtension(Path)
If UCase(Extension) = ".EXE" Then
hIcon = ExtractIcon(0, Path, 0)
If hIcon > 1 Then
GetIcon = System.Drawing.Icon.FromHandle(IntPtr.op_Explicit(hIcon))
Else
GoTo ErrHandle
End If
Else
NoValidIcon:
Dim ExtReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
Dim AppReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
ExtReg = ExtReg.OpenSubKey(Extension)
Dim xStr As String = ExtReg.GetValue("")
AppReg = AppReg.OpenSubKey(xStr & "\DefaultIcon")
Dim IconData() As String
IconData = Split(AppReg.GetValue(""), ",")
hIcon = ExtractIcon(0, IconData(0), Int(IconData(1)))
GetIcon = System.Drawing.Icon.FromHandle(IntPtr.op_Explicit(hIcon))
End If
Exit Function
ErrHandle:
Extension = ".txt"
GoTo NoValidIcon
End If
End Function
End Class