PC Review


Reply
Thread Tools Rate Thread

creating an owner drawn listbox that includes a filetype icon and a filespec

 
 
Bernie Yaeger
Guest
Posts: n/a
 
      3rd Mar 2005
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or .largeimagelist
member, so I don't know how to translate that so that I have the icon + the
string in a listbox. Is this possible, and if so, can you give me some idea
how to go about it.

Thanks for any help.

Bernie Yaeger


 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      3rd Mar 2005
"Bernie Yaeger" <(E-Mail Removed)> schrieb:
>I now know how to gather the file type icons and I'm able to use them in a
>listview. But a listbox does not have a .smallimagelist or .largeimagelist
>member, so I don't know how to translate that so that I have the icon + the
>string in a listbox. Is this possible, and if so, can you give me some
>idea how to go about it.


I am just curious why a listview control is not sufficient...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      3rd Mar 2005
Hi,


Try something like this.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim arIcons As New ArrayList

Dim clsIco As New clsGetIcon

Dim diRoot As New IO.DirectoryInfo("C:\")

For Each fiCur As IO.FileInfo In diRoot.GetFiles

Dim c As New FileHelper

c.FileName = fiCur.Name

c.FileIcon = clsIco.getIcon(fiCur.FullName)

arIcons.Add(c)

Next

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

ListBox1.DataSource = arIcons

ListBox1.DisplayMember = "FileName"

End Sub




Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

Dim g As Graphics = e.Graphics

Dim br As SolidBrush

Dim s As String

Dim c As FileHelper

Try

c = DirectCast(ListBox1.Items.Item(e.Index), FileHelper)

s = c.FileName

Catch ex As Exception

Trace.WriteLine(ex.ToString)

End Try

g.FillRectangle(Brushes.White, e.Bounds)

If CBool(e.State And DrawItemState.Selected) Then

g.FillRectangle(Brushes.LightBlue, e.Bounds)

End If

br = New SolidBrush(Color.Black)

g.DrawIcon(c.FileIcon, e.Bounds.Left, e.Bounds.Top)

g.DrawString(s, ListBox1.Font, br, e.Bounds.Left + 25, e.Bounds.Top)

br.Dispose()



End Sub



Helper classes

Public Class FileHelper

Dim ico As Icon

Dim strFile As String

Public Property FileIcon() As Icon

Get

Return ico

End Get

Set(ByVal Value As Icon)

ico = Value

End Set

End Property

Public Property FileName() As String

Get

Return strFile

End Get

Set(ByVal Value As String)

strFile = Value

End Set

End Property

End Class



Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260), System.Runtime.InteropServices.MarshalAs( _

System.Runtime.InteropServices.UnmanagedType.ByValTStr, _

SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon
representing the file.

<VBFixedString(80), System.Runtime.InteropServices.MarshalAs( _

System.Runtime.InteropServices.UnmanagedType.ByValTStr, _

SizeConst:=80)> Public szTypeName As String

End Structure



Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON As Integer = &H100

Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon

Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name





Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

aSHFileInfo.szTypeName = Space(255)

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

Trace.WriteLine(aSHFileInfo.szTypeName)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class



Ken

---------------------

"Bernie Yaeger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or .largeimagelist
member, so I don't know how to translate that so that I have the icon + the
string in a listbox. Is this possible, and if so, can you give me some idea
how to go about it.

Thanks for any help.

Bernie Yaeger



 
Reply With Quote
 
Bernie Yaeger
Guest
Posts: n/a
 
      3rd Mar 2005
Hi Herfried, Cor;

There are several reasons why the listview is inadequate.

I am trying to replicate the funtionality and display of the Outlook Express
attachment box. The listview does not give me a horizontal scrollbar until
there is a second column displayed, but the path string sometimes runs
beyond the bounds of the listview. In addition, I can't get a vertical
scrollbar in it. Third, I do not want multi cols. Fourth, only 'list' view
can do what I need done - even smallicon view is inadequate.

The listbox has most of what I need intrinsically.

Thanks to both of you for your responses, as always.

Bernie Yaeger

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Bernie Yaeger" <(E-Mail Removed)> schrieb:
>>I now know how to gather the file type icons and I'm able to use them in a
>>listview. But a listbox does not have a .smallimagelist or
>>.largeimagelist member, so I don't know how to translate that so that I
>>have the icon + the string in a listbox. Is this possible, and if so, can
>>you give me some idea how to go about it.

>
> I am just curious why a listview control is not sufficient...
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



 
Reply With Quote
 
Bernie Yaeger
Guest
Posts: n/a
 
      3rd Mar 2005
Hi Ken,

Thanks, as always; I will try your suggestion.

Bernie

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:OX%23Evp$(E-Mail Removed)...
> Hi,
>
>
> Try something like this.
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim arIcons As New ArrayList
>
> Dim clsIco As New clsGetIcon
>
> Dim diRoot As New IO.DirectoryInfo("C:\")
>
> For Each fiCur As IO.FileInfo In diRoot.GetFiles
>
> Dim c As New FileHelper
>
> c.FileName = fiCur.Name
>
> c.FileIcon = clsIco.getIcon(fiCur.FullName)
>
> arIcons.Add(c)
>
> Next
>
> ListBox1.DrawMode = DrawMode.OwnerDrawFixed
>
> ListBox1.DataSource = arIcons
>
> ListBox1.DisplayMember = "FileName"
>
> End Sub
>
>
>
>
> Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
> System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
>
> Dim g As Graphics = e.Graphics
>
> Dim br As SolidBrush
>
> Dim s As String
>
> Dim c As FileHelper
>
> Try
>
> c = DirectCast(ListBox1.Items.Item(e.Index), FileHelper)
>
> s = c.FileName
>
> Catch ex As Exception
>
> Trace.WriteLine(ex.ToString)
>
> End Try
>
> g.FillRectangle(Brushes.White, e.Bounds)
>
> If CBool(e.State And DrawItemState.Selected) Then
>
> g.FillRectangle(Brushes.LightBlue, e.Bounds)
>
> End If
>
> br = New SolidBrush(Color.Black)
>
> g.DrawIcon(c.FileIcon, e.Bounds.Left, e.Bounds.Top)
>
> g.DrawString(s, ListBox1.Font, br, e.Bounds.Left + 25, e.Bounds.Top)
>
> br.Dispose()
>
>
>
> End Sub
>
>
>
> Helper classes
>
> Public Class FileHelper
>
> Dim ico As Icon
>
> Dim strFile As String
>
> Public Property FileIcon() As Icon
>
> Get
>
> Return ico
>
> End Get
>
> Set(ByVal Value As Icon)
>
> ico = Value
>
> End Set
>
> End Property
>
> Public Property FileName() As String
>
> Get
>
> Return strFile
>
> End Get
>
> Set(ByVal Value As String)
>
> strFile = Value
>
> End Set
>
> End Property
>
> End Class
>
>
>
> Imports System.Runtime.InteropServices
>
> Public Class clsGetIcon
>
> Public Structure SHFILEINFO
>
> Dim hIcon As IntPtr
>
> Dim iIcon As Integer
>
> Dim dwAttributes As Integer
>
> <VBFixedString(260), System.Runtime.InteropServices.MarshalAs( _
>
> System.Runtime.InteropServices.UnmanagedType.ByValTStr, _
>
> SizeConst:=260)> Public szDisplayName As String
>
> 'String that contains the name of the file as it appears in the Microsoft®
> Windows® Shell, or the path and file name of the file that contains the
> icon
> representing the file.
>
> <VBFixedString(80), System.Runtime.InteropServices.MarshalAs( _
>
> System.Runtime.InteropServices.UnmanagedType.ByValTStr, _
>
> SizeConst:=80)> Public szTypeName As String
>
> End Structure
>
>
>
> Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath
> As
> String, _
>
> ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _
>
> ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
>
> Private Const SHGFI_ICON As Integer = &H100
>
> Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon
>
> Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name
>
>
>
>
>
> Public Function getIcon(ByVal szFilename As String) As Icon
>
> Try
>
> Dim aSHFileInfo As New SHFILEINFO
>
> Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)
>
> Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON
>
> SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)
>
> Dim myIcon As Icon
>
> myIcon = Icon.FromHandle(aSHFileInfo.hIcon)
>
> aSHFileInfo.szTypeName = Space(255)
>
> SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)
>
> Trace.WriteLine(aSHFileInfo.szTypeName)
>
> Return myIcon
>
> Catch ex As Exception
>
> Debug.WriteLine(ex.ToString)
>
> Return Nothing
>
> End Try
>
> End Function
>
> End Class
>
>
>
> Ken
>
> ---------------------
>
> "Bernie Yaeger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> I now know how to gather the file type icons and I'm able to use them in a
> listview. But a listbox does not have a .smallimagelist or
> .largeimagelist
> member, so I don't know how to translate that so that I have the icon +
> the
> string in a listbox. Is this possible, and if so, can you give me some
> idea
> how to go about it.
>
> Thanks for any help.
>
> Bernie Yaeger
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create an owner drawn ListBox Gladys Microsoft C# .NET 14 8th Apr 2010 05:19 PM
VB.Net listBox - smooth scrolling - owner drawn listBox LostInMd Microsoft Dot NET 0 1st Aug 2007 03:58 PM
creating an owner drawn listbox that includes a filetype icon and a filespec Bernie Yaeger Microsoft VB .NET 5 3rd Mar 2005 07:51 PM
owner drawn listbox =?Utf-8?B?UGV0cnVz?= Microsoft C# .NET 2 1st Mar 2005 11:55 AM
Owner drawn listbox =?Utf-8?B?UGF0dHkgTydEb3Jz?= Microsoft C# .NET 1 6th Aug 2004 03:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:23 AM.