Microsoft example doesn't work... 
Author Message
 Microsoft example doesn't work...

Hi all,

I need to get the icon associated with a file. I'm using the function
SHGetFileInfo as suggested in Microsoft example:
http://www.*-*-*.com/ ;en-us;q319340

With this code I get the icon, but some icons don't paint well. Windows XP
style icons (24bits+alpha channel) paint wrong on the ListView control...
alpha transparent pixels seams to be replaced by annoying black solid
pixels.

However, all icons paint well as the form icon, just adding this code:
Me.Icon = Drawing.Icon.FromHandle(shinfo.hIcon)

So the problem should be in ImageList or the ListView. I tested all
ColorDepth in the ListView but no luck.

Any suggestion?

Thank's alot!

Kind regards,
Betty



Tue, 24 May 2005 03:20:19 GMT  
 Microsoft example doesn't work...
Betty Boop:

I'm not seeing the problem you're having with this. I implemented the code
exactly the way it is described in the KB article and added the imports
statement to the top of my form. It works fine.

Imports System.Runtime.InteropServices

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ListView1 As System.Windows.Forms.ListView

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

<System.Diagnostics.De{*filter*}StepThrough()> Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container()

Me.ListView1 = New System.Windows.Forms.ListView()

Me.Button1 = New System.Windows.Forms.Button()

Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.SuspendLayout()

'

'ListView1

'

Me.ListView1.Location = New System.Drawing.Point(8, 8)

Me.ListView1.Name = "ListView1"

Me.ListView1.Size = New System.Drawing.Size(928, 240)

Me.ListView1.TabIndex = 0

Me.ListView1.View = System.Windows.Forms.View.SmallIcon

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(816, 264)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(112, 24)

Me.Button1.TabIndex = 1

Me.Button1.Text = "Button1"

'

'ImageList1

'

Me.ImageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit

Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(944, 598)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1,
Me.ListView1})

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Structure SHFILEINFO

Public hIcon As IntPtr ' : icon

Public iIcon As Integer ' : icondex

Public dwAttributes As Integer ' : SFGAO_ flags

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _

Public szDisplayName As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _

Public szTypeName As String

End Structure

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

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, _

ByVal uFlags As Integer) As IntPtr

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1

Private Const SHGFI_LARGEICON = &H0 ' Large icon

Private nIndex = 0

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

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim hImgSmall As IntPtr 'The handle to the system image list.

Dim hImgLarge As IntPtr 'The handle to the system image list.

Dim fName As String 'The file name to get the icon from.

Dim shinfo As SHFILEINFO

shinfo = New SHFILEINFO()

Dim openFileDialog1 As OpenFileDialog

openFileDialog1 = New OpenFileDialog()

openFileDialog1.InitialDirectory = "c:\temp\"

openFileDialog1.Filter = "All files (*.*)|*.*"

openFileDialog1.FilterIndex = 2

openFileDialog1.RestoreDirectory = True

ListView1.SmallImageList = ImageList1

ListView1.LargeImageList = ImageList1

shinfo.szDisplayName = New String(Chr(0), 260)

shinfo.szTypeName = New String(Chr(0), 80)

If (openFileDialog1.ShowDialog() = DialogResult.OK) Then

fName = openFileDialog1.FileName

'Use this to get the small icon.

hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), _

SHGFI_ICON Or SHGFI_SMALLICON)

'Use this to get the large icon.

'hImgLarge = SHGetFileInfo(fName, 0,

'ref shinfo, (uint)Marshal.SizeOf(shinfo),

'SHGFI_ICON | SHGFI_LARGEICON);

'The icon is returned in the hIcon member of the shinfo struct.

Dim myIcon As System.Drawing.Icon

myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

ImageList1.Images.Add(myIcon) 'Add icon to imageList.

ListView1.Items.Add(fName, nIndex) 'Add file name and icon to listview.

nIndex = nIndex + 1

End If

End Sub

End Class


Quote:
> Hi all,

> I need to get the icon associated with a file. I'm using the function
> SHGetFileInfo as suggested in Microsoft example:
> http://www.*-*-*.com/ ;en-us;q319340

> With this code I get the icon, but some icons don't paint well. Windows XP
> style icons (24bits+alpha channel) paint wrong on the ListView control...
> alpha transparent pixels seams to be replaced by annoying black solid
> pixels.

> However, all icons paint well as the form icon, just adding this code:
> Me.Icon = Drawing.Icon.FromHandle(shinfo.hIcon)

> So the problem should be in ImageList or the ListView. I tested all
> ColorDepth in the ListView but no luck.

> Any suggestion?

> Thank's alot!

> Kind regards,
> Betty



Wed, 25 May 2005 06:47:05 GMT  
 Microsoft example doesn't work...
Ok, thanks anyway.

I found out the TreeView bug... it doesn't work with 32 bits icons unless
you use a manifest.
It was a simple solution.

Regards.


Quote:
> Hi Richard,

> Thank you for answering... I was affraid no one get into this topic, and
I'm
> still needing.

> I implemented the example exactly as Microsoft suggests (copy and paste,
no
> mistakes on the code, and also tested the C# code also available), and it
> gets the correct icon... but...

> However, when I add the icons to the ImageList (to show on the ListView
> control), some icons lose the transparency and become black. Take a look
at
> the picture I attached to this email.

> To reproduce the problem, run the code provided by Microsoft on a Windows
XP
> machine, and select a file with an windows common extension (.txt, .ini,
> log), for example, the file c:\windows\setupapi.log or the file
> c:\windows\help\desktop.ini

> I tested it on several XP machines, so I believe it's not a windows
> installation problem.

> This happens on icons with 32 bits (24 colour and a transparency alpha
> channel) used by Windows XP. Transparency pixels become solid black...
> damn... at least if they become white, I could live with that...

> I'm desperate... I even consider convert the icons to bitmap, replace the
> black pixels one by one, and back again to icon...

> I also notice that using the same tricky icons as the form icon works
> fine... no black pixels! Adding one line of code:

> Me.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

> So the problem must be on the way ImageList or ListView handles XP style
> icons.

> If you can help, I will be very grateful.

> Thanks again.

> Kind regards,

> Betty



> > Betty Boop:

> > I'm not seeing the problem you're having with this. I implemented the
code
> > exactly the way it is described in the KB article and added the imports
> > statement to the top of my form. It works fine.

> > Imports System.Runtime.InteropServices

> > Public Class Form1

> > Inherits System.Windows.Forms.Form

> > #Region " Windows Form Designer generated code "

> > Public Sub New()

> > MyBase.New()

> > 'This call is required by the Windows Form Designer.

> > InitializeComponent()

> > 'Add any initialization after the InitializeComponent() call

> > End Sub

> > 'Form overrides dispose to clean up the component list.

> > Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

> > If disposing Then

> > If Not (components Is Nothing) Then

> > components.Dispose()

> > End If

> > End If

> > MyBase.Dispose(disposing)

> > End Sub

> > 'Required by the Windows Form Designer

> > Private components As System.ComponentModel.IContainer

> > 'NOTE: The following procedure is required by the Windows Form Designer

> > 'It can be modified using the Windows Form Designer.

> > 'Do not modify it using the code editor.

> > Friend WithEvents ListView1 As System.Windows.Forms.ListView

> > Friend WithEvents Button1 As System.Windows.Forms.Button

> > Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog

> > Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

> > <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
> InitializeComponent()

> > Me.components = New System.ComponentModel.Container()

> > Me.ListView1 = New System.Windows.Forms.ListView()

> > Me.Button1 = New System.Windows.Forms.Button()

> > Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()

> > Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

> > Me.SuspendLayout()

> > '

> > 'ListView1

> > '

> > Me.ListView1.Location = New System.Drawing.Point(8, 8)

> > Me.ListView1.Name = "ListView1"

> > Me.ListView1.Size = New System.Drawing.Size(928, 240)

> > Me.ListView1.TabIndex = 0

> > Me.ListView1.View = System.Windows.Forms.View.SmallIcon

> > '

> > 'Button1

> > '

> > Me.Button1.Location = New System.Drawing.Point(816, 264)

> > Me.Button1.Name = "Button1"

> > Me.Button1.Size = New System.Drawing.Size(112, 24)

> > Me.Button1.TabIndex = 1

> > Me.Button1.Text = "Button1"

> > '

> > 'ImageList1

> > '

> > Me.ImageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit

> > Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

> > Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

> > '

> > 'Form1

> > '

> > Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

> > Me.ClientSize = New System.Drawing.Size(944, 598)

> > Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1,
> > Me.ListView1})

> > Me.Name = "Form1"

> > Me.Text = "Form1"

> > Me.ResumeLayout(False)

> > End Sub

> > #End Region

> > Private Structure SHFILEINFO

> > Public hIcon As IntPtr ' : icon

> > Public iIcon As Integer ' : icondex

> > Public dwAttributes As Integer ' : SFGAO_ flags

> > <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _

> > Public szDisplayName As String

> > <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _

> > Public szTypeName As String

> > End Structure

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

> > ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
> > cbFileInfo As Integer, _

> > ByVal uFlags As Integer) As IntPtr

> > Private Const SHGFI_ICON = &H100

> > Private Const SHGFI_SMALLICON = &H1

> > Private Const SHGFI_LARGEICON = &H0 ' Large icon

> > Private nIndex = 0

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

> > End Sub

> > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Button1.Click

> > Dim hImgSmall As IntPtr 'The handle to the system image list.

> > Dim hImgLarge As IntPtr 'The handle to the system image list.

> > Dim fName As String 'The file name to get the icon from.

> > Dim shinfo As SHFILEINFO

> > shinfo = New SHFILEINFO()

> > Dim openFileDialog1 As OpenFileDialog

> > openFileDialog1 = New OpenFileDialog()

> > openFileDialog1.InitialDirectory = "c:\temp\"

> > openFileDialog1.Filter = "All files (*.*)|*.*"

> > openFileDialog1.FilterIndex = 2

> > openFileDialog1.RestoreDirectory = True

> > ListView1.SmallImageList = ImageList1

> > ListView1.LargeImageList = ImageList1

> > shinfo.szDisplayName = New String(Chr(0), 260)

> > shinfo.szTypeName = New String(Chr(0), 80)

> > If (openFileDialog1.ShowDialog() = DialogResult.OK) Then

> > fName = openFileDialog1.FileName

> > 'Use this to get the small icon.

> > hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), _

> > SHGFI_ICON Or SHGFI_SMALLICON)

> > 'Use this to get the large icon.

> > 'hImgLarge = SHGetFileInfo(fName, 0,

> > 'ref shinfo, (uint)Marshal.SizeOf(shinfo),

> > 'SHGFI_ICON | SHGFI_LARGEICON);

> > 'The icon is returned in the hIcon member of the shinfo struct.

> > Dim myIcon As System.Drawing.Icon

> > myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

> > ImageList1.Images.Add(myIcon) 'Add icon to imageList.

> > ListView1.Items.Add(fName, nIndex) 'Add file name and icon to listview.

> > nIndex = nIndex + 1

> > End If

> > End Sub

> > End Class



> > > Hi all,

> > > I need to get the icon associated with a file. I'm using the function
> > > SHGetFileInfo as suggested in Microsoft example:
> > > http://www.*-*-*.com/ ;en-us;q319340

> > > With this code I get the icon, but some icons don't paint well.
Windows
> XP
> > > style icons (24bits+alpha channel) paint wrong on the ListView
> control...
> > > alpha transparent pixels seams to be replaced by annoying black solid
> > > pixels.

> > > However, all icons paint well as the form icon, just adding this code:
> > > Me.Icon = Drawing.Icon.FromHandle(shinfo.hIcon)

> > > So the problem should be in ImageList or the ListView. I tested all
> > > ColorDepth in the ListView but no luck.

> > > Any suggestion?

> > > Thank's alot!

> > > Kind regards,
> > > Betty



Sun, 29 May 2005 03:27:22 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Microsoft example doesn't work

2. Microsoft's KB Q113236 Doesn't work

3. ANOTHER help example doesn't work

4. Process.Start - example doesn't work

5. dotnetfx.exe bootstrapper example setup.exe doesn't work

6. DragIcon Property Problem /VB Example Doesn't work

7. Why doesn't MSDN ListView examples work?

8. Microsoft's tooltip example does not work?

9. Can't get Microsoft DAO example to work, please help

10. Microsoft web control doesn't work

11. fRefreshLinks Doesn't work if path doesn't exist

12. Microsoft VB doesn't know how to talk to Microsoft FoxPro

 

 
Powered by phpBB® Forum Software