| 分類:[C#]
環境 ・WindowsXP ・Visual C# 2008 Express Edition
お世話になります。 ファイルアイコンを取得し、そのアイコンをリストビューに表示させたいのですが、 リストビューに表示されずに困っています。どこがわるいか指摘頂けないでしょうか。 よろしくお願いします。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
using System.Windows; using System.Runtime.InteropServices; using System.IO;
namespace IconExtract { public partial class Form1 : Form { // -------------------------------------------- // アイコン取得用のWin32 API // -------------------------------------------- // SHGetFileInfo関数 [DllImport("shell32.dll")] private static extern IntPtr SHGetFileInfo (string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
// SHGetFileInfo関数で使用するフラグ private enum shfileflg { SHGFI_ICON = 0x000000100, SHGFI_DISPLAYNAME = 0x000000200, SHGFI_TYPENAME = 0x000000400, SHGFI_ATTRIBUTES = 0x000000800, SHGFI_ICONLOCATION = 0x000001000, SHGFI_EXETYPE = 0x000002000, SHGFI_SYSICONINDEX = 0x000004000, SHGFI_LINKOVERLAY = 0x000008000, SHGFI_SELECTED = 0x000010000, SHGFI_ATTR_SPECIFIED = 0x000020000, SHGFI_LARGEICON = 0x000000000, SHGFI_SMALLICON = 0x000000001, SHGFI_OPENICON = 0x000000002, SHGFI_SHELLICONSIZE = 0x000000004, SHGFI_PIDL = 0x000000008, SHGFI_USEFILEATTRIBUTES = 0x000000010 };
// SHGetFileInfo関数で使用する構造体 private struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; };
public Form1() { InitializeComponent(); }
public static void main() { // アイコンを取得 SHFILEINFO shinfo = new SHFILEINFO(); uint flg; flg = (uint)(shfileflg.SHGFI_ICON | shfileflg.SHGFI_LARGEICON); IntPtr hSuccess = SHGetFileInfo("c:\\test.log", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flg); if (hSuccess != IntPtr.Zero) { Icon appIcon = Icon.FromHandle(shinfo.hIcon); imageList1.Images.Add(appIcon); listView1.LargeImageList = imageList1; } } } } |