zoukankan      html  css  js  c++  java
  • 将文件的图标添加到LISTVIEW中

    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.IO;
    using System.Runtime.InteropServices;
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            public static uint SHGFI_ICON = 0x100;
            public static uint SHGFI_DISPLAYNAME = 0x200;
            public static uint SHGFI_TYPENAME = 0x400;
            public static uint SHGFI_ATTRIBUTES = 0x800;
            public static uint SHGFI_ICONLOCATION = 0x1000;
            public static uint SHGFI_EXETYPE = 0x2000;
            public static uint SHGFI_SYSICONINDEX = 0x4000;
            public static uint SHGFI_LINKOVERLAY = 0x8000;
            public static uint SHGFI_SELECTED = 0x10000;
            public static uint SHGFI_LARGEICON = 0x0;
            public static uint SHGFI_SMALLICON = 0x1;
            public static uint SHGFI_OPENICON = 0x2;
            public static uint SHGFI_SHELLICONSIZE = 0x4;
            public static uint SHGFI_PIDL = 0x8;
            public static uint SHGFI_USEFILEATTRIBUTES = 0x10;
    
            public static uint FILE_ATTRIBUTE_NORMAL = 0x80;
            public static uint LVM_FIRST = 0x1000;
            public static uint LVM_SETIMAGELIST = LVM_FIRST + 3;
            public static uint LVSIL_NORMAL = 0;
            public static uint LVSIL_SMALL = 1;
    
            [DllImport("Shell32.dll")]
            public static extern IntPtr SHGetFileInfo(string pszPath,
                uint dwFileAttributes, ref SHFILEINFO psfi,
                int cbfileInfo, uint uFlags);
    
            public struct SHFILEINFO
            {
                public IntPtr hIcon;
                public int iIcon;
                public int dwAttributes;
                public string szDisplayName;
                public string szTypeName;
            }
    
            [DllImport("User32.DLL")]
            public static extern int SendMessage(IntPtr hWnd,
                uint Msg, IntPtr wParam, IntPtr lParam);
    
            public void SetImageList(ListView AListView)
            {
                SHFILEINFO vFileInfo = new SHFILEINFO();
                IntPtr vImageList = SHGetFileInfo("", 0, ref vFileInfo,
                    Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
                    SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
    
                SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_NORMAL,
                    vImageList);
    
                vImageList = SHGetFileInfo("", 0, ref vFileInfo,
                    Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
                    SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
                SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_SMALL,
                    vImageList);
            }
    
            public int FileIconIndex(string AFileName)
            {
                SHFILEINFO vFileInfo = new SHFILEINFO();
                SHGetFileInfo(AFileName, 0, ref vFileInfo,
                    Marshal.SizeOf(vFileInfo), SHGFI_SYSICONINDEX);
                return vFileInfo.iIcon;
            }
     
    
            private void button1_Click(object sender, EventArgs e)
            {
                SetImageList(listView1);
                listView1.Items.Add("Project1.exe", FileIconIndex(@"C:UsersAdminDesktopProject1.exe"));
                listView1.Items.Add("Project1.dproj", FileIconIndex(@"C:UsersAdminDesktopProject1.dproj"));
                listView1.Items.Add("Unit1.dfm", FileIconIndex(@"C:UsersAdminDesktopUnit1.dfm"));
                listView1.Items.Add("Unit1.pas", FileIconIndex(@"C:UsersAdminDesktopUnit1.pas"));
            }
        }
    }
    View Code
  • 相关阅读:
    Spring MVC:框架及其组件介绍
    Goods:生成订单
    Goods:我的订单查询分页
    LeetCode:Kth Largest Element in an Array(need update)
    LeetCode:Swap Nodes in Pairs
    Goods:购物车条目加减数量实现
    LeetCode:Sum Root to Leaf Numbers
    Goods:购物车模块之全选按钮与条目之复选按钮的click事件
    Goods:查询某个用户的购物车条目以及添加购物车条目
    洛谷2387 NOI2014魔法森林(LCT维护最小生成树)
  • 原文地址:https://www.cnblogs.com/xe2011/p/3456767.html
Copyright © 2011-2022 走看看