/// <summary> /// Summary of Initializer. /// </summary> /// private static void Initializer() { if (m_Initialized) { return; } int dwFlag = (int)(ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES | ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.SMALLICON); ExpTreeLib.ShellDll.SHFILEINFO shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); m_smImgList = ExpTreeLib.ShellDll.SHGetFileInfo(".txt", ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwFlag); Debug.Assert((!m_smImgList.Equals(IntPtr.Zero)), "Failed to create Image Small ImageList"); if (m_smImgList.Equals(IntPtr.Zero)) { throw (new Exception("Failed to create Small ImageList")); } /*dwFlag = (int)(ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES | ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.LARGEICON); * m_lgImgList = ExpTreeLib.ShellDll.SHGetFileInfo(".txt", ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwFlag); * Debug.Assert((! m_lgImgList.Equals(IntPtr.Zero)), "Failed to create Image Small ImageList"); * if (m_lgImgList.Equals(IntPtr.Zero)) * { * throw (new Exception("Failed to create Large ImageList")); * }*/ m_Initialized = true; }
/// <summary> /// Summary of Initializer. /// </summary> /// private static void Initializer() { if (m_Initialized) { return; } int dwFlag = (int)(ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES | ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.SMALLICON); ExpTreeLib.ShellDll.SHFILEINFO shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); m_smImgList = ExpTreeLib.ShellDll.SHGetFileInfo(".txt", ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwFlag); Debug.Assert((! m_smImgList.Equals(IntPtr.Zero)), "Failed to create Image Small ImageList"); if (m_smImgList.Equals(IntPtr.Zero)) { throw (new Exception("Failed to create Small ImageList")); } /*dwFlag = (int)(ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES | ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.LARGEICON); m_lgImgList = ExpTreeLib.ShellDll.SHGetFileInfo(".txt", ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwFlag); Debug.Assert((! m_lgImgList.Equals(IntPtr.Zero)), "Failed to create Image Small ImageList"); if (m_lgImgList.Equals(IntPtr.Zero)) { throw (new Exception("Failed to create Large ImageList")); }*/ m_Initialized = true; }
/// <summary> /// Summary of GetIconIndex. /// </summary> /// <param name="item"></param> /// <param name="GetOpenIcon"></param> /// public static int GetIconIndex(CShItem item, bool GetOpenIcon, bool GetSelectedIcon) { int rVal; //The returned Index if (dcAttrIndexCache.TryGetValue((int)item.Attributes, out rVal)) return rVal; Initializer(); bool HasOverlay = false; //true if it's an overlay //int rVal; //The returned Index int dwflag = (int)(ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.PIDL | ExpTreeLib.ShellDll.SHGFI.ICON); int dwAttr = 0; //build Key into HashTable for this Item int Key = ! GetOpenIcon ? item.IconIndexNormal * 256 : item.IconIndexOpen * 256; CShItem with_1 = item; if (with_1.IsLink) { Key = Key | 1; dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.LINKOVERLAY; HasOverlay = true; } if (with_1.IsShared) { Key = Key | 2; dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.ADDOVERLAYS; HasOverlay = true; } if (GetSelectedIcon) { Key = Key | 4; dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.SELECTED; HasOverlay = true; //not really an overlay, but handled the same } if (m_Table.ContainsKey(Key)) { rVal = (int)m_Table[Key]; mCnt++; } else if (! HasOverlay) //for non-overlay icons, we already have { rVal = Key / 256; // the right index -- put in table m_Table[Key] = rVal; bCnt++; } else //don't have iconindex for an overlay, get it. { //This is the tricky part -- add overlaid Icon to systemimagelist // use of SmallImageList from Calum McLellan //ExpTreeLib.ShellDll.SHFILEINFO shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); ExpTreeLib.ShellDll.SHFILEINFO shfi_small = new ExpTreeLib.ShellDll.SHFILEINFO(); //IntPtr HR; IntPtr HR_SMALL; if (with_1.IsFileSystem && ! with_1.IsDisk && ! with_1.IsFolder) { dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES; dwAttr = ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL; } //HR = ExpTreeLib.ShellDll.SHGetFileInfo(with_1.PIDL, dwAttr, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwflag); HR_SMALL = ExpTreeLib.ShellDll.SHGetFileInfo(with_1.PIDL, dwAttr, ref shfi_small, ExpTreeLib.ShellDll.cbFileInfo, dwflag | (int)ExpTreeLib.ShellDll.SHGFI.SMALLICON); m_Mutex.WaitOne(); rVal = ExpTreeLib.ShellDll.ImageList_ReplaceIcon(m_smImgList, - 1, shfi_small.hIcon); Debug.Assert(rVal > - 1, "Failed to add overlaid small icon"); /*int rVal2; rVal2 = ExpTreeLib.ShellDll.ImageList_ReplaceIcon(m_lgImgList, - 1, shfi.hIcon); Debug.Assert(rVal2 > - 1, "Failed to add overlaid large icon"); Debug.Assert(rVal == rVal2, "Small & Large IconIndices are Different");*/ m_Mutex.ReleaseMutex(); //ExpTreeLib.ShellDll.DestroyIcon(shfi.hIcon); ExpTreeLib.ShellDll.DestroyIcon(shfi_small.hIcon); if (rVal < 0/* || rVal != rVal2*/) { throw (new ApplicationException("Failed to add Icon for " + item.DisplayName)); } m_Table[Key] = rVal; } if (item.IsFileSystem && item.IsFolder) { dcAttrIndexCache[(int)item.Attributes] = rVal; } return rVal; }
/// <summary> /// Summary of GetIconIndex. /// </summary> /// <param name="item"></param> /// <param name="GetOpenIcon"></param> /// public static int GetIconIndex(CShItem item, bool GetOpenIcon, bool GetSelectedIcon) { int rVal; //The returned Index if (dcAttrIndexCache.TryGetValue((int)item.Attributes, out rVal)) { return(rVal); } Initializer(); bool HasOverlay = false; //true if it's an overlay //int rVal; //The returned Index int dwflag = (int)(ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.PIDL | ExpTreeLib.ShellDll.SHGFI.ICON); int dwAttr = 0; //build Key into HashTable for this Item int Key = !GetOpenIcon ? item.IconIndexNormal * 256 : item.IconIndexOpen * 256; CShItem with_1 = item; if (with_1.IsLink) { Key = Key | 1; dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.LINKOVERLAY; HasOverlay = true; } if (with_1.IsShared) { Key = Key | 2; dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.ADDOVERLAYS; HasOverlay = true; } if (GetSelectedIcon) { Key = Key | 4; dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.SELECTED; HasOverlay = true; //not really an overlay, but handled the same } if (m_Table.ContainsKey(Key)) { rVal = (int)m_Table[Key]; mCnt++; } else if (!HasOverlay) //for non-overlay icons, we already have { rVal = Key / 256; // the right index -- put in table m_Table[Key] = rVal; bCnt++; } else //don't have iconindex for an overlay, get it. { //This is the tricky part -- add overlaid Icon to systemimagelist // use of SmallImageList from Calum McLellan //ExpTreeLib.ShellDll.SHFILEINFO shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); ExpTreeLib.ShellDll.SHFILEINFO shfi_small = new ExpTreeLib.ShellDll.SHFILEINFO(); //IntPtr HR; IntPtr HR_SMALL; if (with_1.IsFileSystem && !with_1.IsDisk && !with_1.IsFolder) { dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES; dwAttr = ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL; } //HR = ExpTreeLib.ShellDll.SHGetFileInfo(with_1.PIDL, dwAttr, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwflag); HR_SMALL = ExpTreeLib.ShellDll.SHGetFileInfo(with_1.PIDL, dwAttr, ref shfi_small, ExpTreeLib.ShellDll.cbFileInfo, dwflag | (int)ExpTreeLib.ShellDll.SHGFI.SMALLICON); m_Mutex.WaitOne(); rVal = ExpTreeLib.ShellDll.ImageList_ReplaceIcon(m_smImgList, -1, shfi_small.hIcon); Debug.Assert(rVal > -1, "Failed to add overlaid small icon"); /*int rVal2; * rVal2 = ExpTreeLib.ShellDll.ImageList_ReplaceIcon(m_lgImgList, - 1, shfi.hIcon); * Debug.Assert(rVal2 > - 1, "Failed to add overlaid large icon"); * Debug.Assert(rVal == rVal2, "Small & Large IconIndices are Different");*/ m_Mutex.ReleaseMutex(); //ExpTreeLib.ShellDll.DestroyIcon(shfi.hIcon); ExpTreeLib.ShellDll.DestroyIcon(shfi_small.hIcon); if (rVal < 0 /* || rVal != rVal2*/) { throw (new ApplicationException("Failed to add Icon for " + item.DisplayName)); } m_Table[Key] = rVal; } if (item.IsFileSystem && item.IsFolder) { dcAttrIndexCache[(int)item.Attributes] = rVal; } return(rVal); }
//only used when desktopfolder has not been intialized /// <summary> /// Private Constructor. Creates CShItem of the Desktop /// </summary> /// private CShItem() { if (DesktopBase != null) { throw (new Exception("Attempt to initialize CShItem for second time")); } int HR; //firstly determine what the local machine calls a "System Folder" and "My Computer" IntPtr tmpPidl = IntPtr.Zero; HR = ExpTreeLib.ShellDll.SHGetSpecialFolderLocation(0, (int)ExpTreeLib.ShellDll.CSIDL.DRIVES, ref tmpPidl); ExpTreeLib.ShellDll.SHFILEINFO shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); int dwflag = (int)(ExpTreeLib.ShellDll.SHGFI.DISPLAYNAME | ExpTreeLib.ShellDll.SHGFI.TYPENAME | ExpTreeLib.ShellDll.SHGFI.PIDL); int dwAttr = 0; ExpTreeLib.ShellDll.SHGetFileInfo(tmpPidl, dwAttr, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwflag); m_strSystemFolder = shfi.szTypeName; m_strMyComputer = shfi.szDisplayName; Marshal.FreeCoTaskMem(tmpPidl); //set OS version info XPorAbove = ShellDll.IsXpOrAbove(); Win2KOrAbove = ShellDll.Is2KOrAbove(); //With That done, now set up Desktop CShItem m_Path = "::{" + ExpTreeLib.ShellDll.DesktopGUID.ToString() + "}"; m_IsFolder = true; m_HasSubFolders = true; m_IsBrowsable = false; HR = ExpTreeLib.ShellDll.SHGetDesktopFolder(ref m_Folder); m_Pidl = ShellDll.GetSpecialFolderLocation(IntPtr.Zero, (int)ExpTreeLib.ShellDll.CSIDL.DESKTOP); dwflag = (int)(ExpTreeLib.ShellDll.SHGFI.DISPLAYNAME | ExpTreeLib.ShellDll.SHGFI.TYPENAME | ExpTreeLib.ShellDll.SHGFI.SYSICONINDEX | ExpTreeLib.ShellDll.SHGFI.PIDL); dwAttr = 0; IntPtr H = ExpTreeLib.ShellDll.SHGetFileInfo(m_Pidl, dwAttr, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwflag); m_DisplayName = shfi.szDisplayName; m_TypeName = strSystemFolder; //not returned correctly by SHGetFileInfo m_IconIndexNormal = shfi.iIcon; m_IconIndexOpen = shfi.iIcon; m_HasDispType = true; m_IsDropTarget = true; m_IsReadOnly = false; m_IsReadOnlySetup = true; //also get local name for "My Documents" int pchEaten = 0; tmpPidl = IntPtr.Zero; int pdwAttributes = 0; HR = m_Folder.ParseDisplayName(0, IntPtr.Zero, "::{450d8fba-ad25-11d0-98a8-0800361b1103}", ref pchEaten, ref tmpPidl, ref pdwAttributes); shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); dwflag = (int)(ExpTreeLib.ShellDll.SHGFI.DISPLAYNAME | ExpTreeLib.ShellDll.SHGFI.TYPENAME | ExpTreeLib.ShellDll.SHGFI.PIDL); dwAttr = 0; ExpTreeLib.ShellDll.SHGetFileInfo(tmpPidl, dwAttr, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwflag); m_strMyDocuments = shfi.szDisplayName; Marshal.FreeCoTaskMem(tmpPidl); //this must be done after getting "My Documents" string m_SortFlag = ComputeSortFlag(); //Set DesktopBase DesktopBase = this; // Lastly, get the Path and CShItem of the DesktopDirectory -- useful for DragDrop m_DeskTopDirectory = new CShItem(ExpTreeLib.ShellDll.CSIDL.DESKTOPDIRECTORY); }
/// <summary> /// Set DisplayName, TypeName, and SortFlag when actually needed /// </summary> /// private void SetDispType() { //Get Displayname, TypeName ExpTreeLib.ShellDll.SHFILEINFO shfi = new ExpTreeLib.ShellDll.SHFILEINFO(); int dwflag = (int)(ExpTreeLib.ShellDll.SHGFI.DISPLAYNAME | ExpTreeLib.ShellDll.SHGFI.TYPENAME | ExpTreeLib.ShellDll.SHGFI.PIDL); int dwAttr = 0; if (m_IsFileSystem && ! m_IsFolder) { dwflag = dwflag | (int)ExpTreeLib.ShellDll.SHGFI.USEFILEATTRIBUTES; dwAttr = ExpTreeLib.ShellDll.FILE_ATTRIBUTE_NORMAL; } IntPtr H = ExpTreeLib.ShellDll.SHGetFileInfo(m_Pidl, dwAttr, ref shfi, ExpTreeLib.ShellDll.cbFileInfo, dwflag); m_DisplayName = shfi.szDisplayName; m_TypeName = shfi.szTypeName; //fix DisplayName if (m_DisplayName.Equals("")) { m_DisplayName = m_Path; } //Fix TypeName //If m_IsFolder And m_TypeName.Equals("File") Then // m_TypeName = "File Folder" //End If m_SortFlag = ComputeSortFlag(); m_HasDispType = true; }