Results 1 to 11 of 11

Thread: how to get document's icons at runtime?

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    how to get document's icons at runtime?

    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..
    \m/\m/

  2. #2
    Lively Member
    Join Date
    May 2002
    Posts
    94
    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.

  3. #3
    Lively Member
    Join Date
    May 2002
    Posts
    94
    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.

  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    if u dont take too long i can wait for u to get home..if its not much trouble!
    \m/\m/

  5. #5
    Lively Member
    Join Date
    May 2002
    Posts
    94
    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+

  6. #6
    Lively Member
    Join Date
    May 2002
    Posts
    94
    ok I got something worked out.. and its is working... Let me Clean it up and then I will post it...

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ok tks
    \m/\m/

  8. #8
    Lively Member
    Join Date
    May 2002
    Posts
    94
    This is awsome!!!!


    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 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 Icon use this
    Code:
    Me.Icon = ExtractIcon.GetIcon("path\filename.txt")
    to return an image use this
    Code:
    PictureBox1.Image = ExtractIcon.GetIcon("path\filename.txt").ToBitmap

  9. #9

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ok tks
    \m/\m/

  10. #10
    Lively Member
    Join Date
    May 2002
    Posts
    94
    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...

  11. #11
    Lively Member
    Join Date
    May 2002
    Posts
    94
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width