www.pudn.com > FileSecurity.rar > SystemImageList.cs


using System; 
using System.Runtime.InteropServices; 
 
namespace FileSecurity 
{ 
    class SystemImageList 
    { 
        #region Member Variables 
 
        private static IntPtr m_pImgHandle = IntPtr.Zero; 
 
        private static Boolean m_bImgInit = false; 
 
        #endregion 
 
        #region Constants 
 
        private const UInt32 TVSIL_NORMAL = 0; 
        private const UInt32 TVM_SETIMAGELIST = 4361; 
 
        #endregion 
 
        #region Private Methods 
 
        private static void InitImageList() 
        { 
            if (m_bImgInit) 
                throw new Exception("The system image list handle has already been retrieved."); 
 
            ShellAPI.SHFILEINFO shInfo = new ShellAPI.SHFILEINFO(); 
            ShellAPI.SHGFI dwAttribs =  
                ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES |  
                ShellAPI.SHGFI.SHGFI_SMALLICON | 
                ShellAPI.SHGFI.SHGFI_SYSICONINDEX; 
            m_pImgHandle = ShellAPI.SHGetFileInfo(".txt", ShellAPI.FILE_ATTRIBUTE_NORMAL, out shInfo, (uint)Marshal.SizeOf(shInfo), dwAttribs); 
             
            if (m_pImgHandle.Equals(IntPtr.Zero)) 
                throw new Exception("Unable to retrieve system image list handle."); 
 
            m_bImgInit = true; 
        } 
 
        #endregion 
 
        #region Public Methods 
 
        public static void SetTVImageList(IntPtr tvwHandle) 
        { 
            InitImageList(); 
            Int32 hRes = ShellAPI.SendMessage(tvwHandle, TVM_SETIMAGELIST, TVSIL_NORMAL, m_pImgHandle); 
            if (hRes != 0) 
                Marshal.ThrowExceptionForHR(hRes); 
        } 
 
        #endregion 
    } 
}