Ejemplo n.º 1
0
        private int LoadMenu( object parent, string folder )
        {
            SortableToolStripMenuItem item;
            Type t;
            ToolStripItemCollection items;

            SHFILEINFO shinfo;
            IntPtr imageHandle;
            uint flags;
            string text,
                sortName,
                lastText = "";
            DirectoryInfo folderInfo;
            List<DirectoryInfo> dirs;
            List<FileInfo> files;

            t = parent.GetType();
            if (t == typeof(ContextMenuStrip)) {
                items = ((ContextMenuStrip)parent).Items;
            } else if (t == typeof(SortableToolStripMenuItem)) {
                items = ((SortableToolStripMenuItem)parent).DropDownItems;
            } else {
                return 0;
            }

            shinfo = new SHFILEINFO();
            flags = Win32.SHGFI_TYPENAME | Win32.SHGFI_DISPLAYNAME | Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_SYSICONINDEX;
            text = string.Empty;
            sortName = string.Empty;

            folderInfo = new DirectoryInfo(folder);
            if (!folderInfo.Exists) {
                MessageBox.Show(string.Format("The {0} is invalid", folderInfo.FullName), "MenuStack", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return 0;
            }

            if (subfolders) {
                dirs = new List<DirectoryInfo>(folderInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly));
                dirs.Sort(delegate( DirectoryInfo a, DirectoryInfo b )
                {
                    return string.Compare(a.Name, b.Name, StringComparison.InvariantCultureIgnoreCase);
                });

                foreach (DirectoryInfo dir in dirs) {
                    sortName = dir.Name;
                    text = CleanName(dir.Name);

                    if (text.Equals("-")) {
                        items.Add(new SortableToolStripSeparator(sortName));
                        continue;
                    }

                    item = new SortableToolStripMenuItem(text);
                    item.SortName = sortName.Trim();
                    item.Tag = dir.FullName;

                    if (LoadMenu(item, dir.FullName) > 0) {
                        items.Add(item);
                    }
                }
            }

            files = new List<FileInfo>();

            foreach (string pattern in FilePattern.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) {
                foreach (FileInfo f in folderInfo.GetFiles(pattern, SearchOption.TopDirectoryOnly)) {
                    if (files.Find(fe => fe.FullName.Equals(f.FullName, StringComparison.CurrentCultureIgnoreCase)) == null) {
                        files.Add(f);
                    }
                }
            }

            files.Sort(delegate( FileInfo a, FileInfo b )
            {
                return string.Compare(a.Name, b.Name, StringComparison.InvariantCultureIgnoreCase);
            });

            foreach (FileInfo file in files) {
                if (file.Name.Equals("desktop.ini", StringComparison.InvariantCultureIgnoreCase)) {
                    continue;
                } else if (file.Name.Equals("thumbs.db", StringComparison.InvariantCultureIgnoreCase)) {
                    continue;
                }

                Icon icon;
                IntPtr PtrIcon;

                imageHandle = Win32.SHGetFileInfo(file.FullName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags); // Get the details of the file
                PtrIcon = Win32.ImageList_GetIcon(imageHandle, shinfo.iIcon.ToInt64(), Win32.ILD_TRANSPARENT);
                icon = Icon.FromHandle(PtrIcon);
                //icon = Icon.ExtractAssociatedIcon(file.FullName);

                sortName = file.Name;
                text = CleanName(file.Name).TrimEnd(".lnk");

                if (!showExts && text.IndexOf(".") > -1) {
                    text = Path.GetFileNameWithoutExtension(text);
                }

                //
                // Allow 'macros' in the file name..
                //

                // If the file name has [#s] then it will
                // only be displayed if subfolders is true.
                if (text.IndexOf("[#s]") > -1) {
                    if (!subfolders) {
                        continue;
                    }
                    text = text.Replace("[#s]", "").Trim();
                }

                //
                // If the file is only '-' characters, change it to just one '-' only.
                //
                //if (text.IndexOf("-") > -1) {
                //	string tmp = text;
                //	while (tmp.IndexOf("--") > -1) {
                //		tmp = tmp.Replace("--", "-");
                //	}
                //	if (tmp.Equals("-")) {
                //		text = tmp;
                //	}
                //}

                //
                // Create the menu item or separator
                //
                if (text.Equals("-")) {
                    items.Add(new SortableToolStripSeparator(sortName));
                    continue;
                } else {
                    // Create the menu item for the file.
                    if (icon != null && icon.Height > 0 && icon.Width > 0) {
                        item = new SortableToolStripMenuItem(text, icon.ToBitmap());
                        icon.Dispose();
                    } else {
                        item = new SortableToolStripMenuItem(text);
                    }
                    if (item == null) {
                        continue;
                    }

                    item.SortName = sortName.Trim();
                    item.Tag = file.FullName;
                    item.Click += new EventHandler(item_Click);

                    items.Add(item);
                }
            }

            // Sort the folders and files together alphabetically.
            if (combine) {
                SortItems(items);
            }

            if (showSearch) {
                searchBox_TextChanged(null, null);
            }

            // Remove duplicate separators.
            for (int i = items.Count - 1; i >= 0; i--) {
                if (items[i] is SortableToolStripSeparator) {
                    if (lastText == "-") {
                        items.RemoveAt(i);
                    }
                    lastText = "-";
                    continue;
                }
                lastText = items[i].Text;
            }

            return items.Count;
        }
Ejemplo n.º 2
0
		public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags );