123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.ComponentModel;
- using Microsoft.Win32;
- using SmartCoalApplication.SystemLayer.FileDlgExtenders.Win32Types;
- namespace SmartCoalApplication.SystemLayer.FileDlgExtenders.FileDialogExtenders
- {
- public static class Extensions
- {
- #region extension Methods
- public static DialogResult ShowDialog(this FileDialog fdlg, FileDialogControlBase ctrl, IWin32Window owner) //where T : FileDialogControlBase, new()
- {
- ctrl.FileDlgType = (fdlg is SaveFileDialog) ? FileDialogType.SaveFileDlg : FileDialogType.OpenFileDlg;
- if (ctrl.ShowDialogExt(fdlg, owner) == DialogResult.OK)
- return DialogResult.OK;
- else
- return DialogResult.Ignore;
- }
- #endregion
- }
- //see http://msdn.microsoft.com/en-us/magazine/cc300434.aspx
- public static class FileDialogPlaces
- {
- private static readonly string TempKeyName = "TempPredefKey_" + Guid.NewGuid().ToString();
- private const string Key_PlacesBar = @"Software\Microsoft\Windows\CurrentVersion\Policies\ComDlg32\PlacesBar";
- private static RegistryKey _fakeKey;
- private static IntPtr _overriddenKey;
- private static object[] m_places;
- public static void SetPlaces(this FileDialog fd, object[] places)
- {
- if (fd == null || places == null)
- return;
- if (m_places == null)
- m_places = new object[places.GetLength(0)];
- for (int i = 0; i < m_places.GetLength(0); i++)
- {
- m_places[i] = places[i];
- }
- if (_fakeKey != null)
- ResetPlaces(fd);
- SetupFakeRegistryTree();
- if (fd != null)
- fd.Disposed += (object sender, EventArgs e) => { if (m_places != null && fd != null) ResetPlaces(fd); };
- }
- static FileDialogPlaces()
- {
- }
- static public void ResetPlaces(this FileDialog fd)
- {
- if (_overriddenKey != IntPtr.Zero)
- {
- ResetRegistry(_overriddenKey);
- _overriddenKey = IntPtr.Zero;
- }
- if (_fakeKey != null)
- {
- _fakeKey.Close();
- _fakeKey = null;
- }
- //delete the key tree
- Registry.CurrentUser.DeleteSubKeyTree(TempKeyName);
- m_places = null;
- }
- private static void SetupFakeRegistryTree()
- {
- try
- {
- _fakeKey = Registry.CurrentUser.CreateSubKey(TempKeyName);
- _overriddenKey = InitializeRegistry();
- // at this point, m_TempKeyName equals places key
- // write dynamic places here reading from Places
- RegistryKey reg = Registry.CurrentUser.CreateSubKey(Key_PlacesBar);
- for (int i = 0; i < m_places.GetLength(0); i++)
- {
- if (m_places[i] != null)
- {
- reg.SetValue("Place" + i.ToString(), m_places[i]);
- }
- }
- }
- catch
- {
- }
- }
- //public static IntPtr GetRegistryHandle(RegistryKey registryKey)
- //{
- // Type type = registryKey.GetType();
- // FieldInfo fieldInfo = type.GetField("hkey", BindingFlags.Instance | BindingFlags.NonPublic);
- // return (IntPtr)fieldInfo.GetValue(registryKey);
- //}
- static readonly UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
- private static IntPtr InitializeRegistry()
- {
- IntPtr hkMyCU;
- Win32Types.NativeMethods.RegCreateKeyW(HKEY_CURRENT_USER, TempKeyName, out hkMyCU);
- Win32Types.NativeMethods.RegOverridePredefKey(HKEY_CURRENT_USER, hkMyCU);
- return hkMyCU;
- }
- static void ResetRegistry(IntPtr hkMyCU)
- {
- try
- {
- Win32Types.NativeMethods.RegOverridePredefKey(HKEY_CURRENT_USER, IntPtr.Zero);
- Win32Types.NativeMethods.RegCloseKey(hkMyCU);
- }
- catch
- {
- }
- }
- }
- //http://www.codeguru.com/cpp/misc/misc/system/article.php/c13407/
- // fot .net 4.0 and up use CustomPlaces instead :http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.customplaces.aspx
- public enum Places
- {
- [Description("Desktop")]
- Desktop = 0,
- [Description("Internet Explorer ")]
- InternetExplorer = 1,
- [Description("Program Files")]
- Programs = 2,
- [Description("Control Panel")]
- ControlPanel = 3,
- [Description("Printers")]
- Printers = 4,
- [Description("My Documents")]
- MyDocuments = 5,
- [Description("Favorites")]
- Favorites = 6,
- [Description("Startup folder")]
- StartupFolder = 7,
- [Description("Recent Files")]
- RecentFiles = 8,
- [Description("Send To")]
- SendTo = 9,
- [Description("Recycle Bin")]
- RecycleBin = 0xa,
- [Description("Start menu")]
- StartMenu = 0xb,
- [Description("Logical My Documents")]
- Logical_MyDocuments = 0xc,
- [Description("My Music")]
- MyMusic = 0xd,
- [Description("My Videos")]
- MyVideos = 0xe,
- [Description("<user name>\\Desktop")]
- UserName_Desktop = 0x10,
- [Description("My Computer")]
- MyComputer = 0x11,
- [Description("My Network Places")]
- MyNetworkPlaces = 18,
- [Description("<user name>\nethood")]
- User_Name_Nethood = 0x13,
- [Description("Fonts")]
- Fonts = 0x14,
- [Description("All Users\\Start Menu")]
- All_Users_StartMenu = 0x16,
- [Description("All Users\\Start Menu\\Programs ")]
- All_Users_StartMenu_Programs = 0x17,
- [Description("All Users\\Startup")]
- All_Users_Startup = 0x18,
- [Description("All Users\\Desktop")]
- All_Users_Desktop = 0x19,
- [Description("<user name>\\Application Data ")]
- User_name_ApplicationData = 0x1a,
- [Description("<user name>\\PrintHood ")]
- User_Name_PrintHood = 0x1b,
- [Description("<user name>\\Local Settings\\Applicaiton Data (nonroaming)")]
- Local_ApplicaitonData = 0x1c,
- [Description("Nonlocalized common startup ")]
- NonlocalizedCommonStartup = 0x1e,
- [Description("")]
- CommonFavorites = 0x1f,
- [Description("Internet Cache ")]
- InternetCache = 0x20,
- [Description("Cookies ")]
- Cookies = 0x21,
- [Description("History")]
- History = 0x22,
- [Description("All Users\\Application Data ")]
- All_Users_ApplicationData = 0x23,
- [Description("Windows Directory")]
- WindowsDirectory = 0x24,
- [Description("System Directory")]
- SystemDirectory = 0x25,
- [Description("Program Files ")]
- ProgramFiles = 0x26,
- [Description("My Pictures ")]
- MyPictures = 0x27,
- [Description("USERPROFILE")]
- USERPROFILE = 0x28,
- [Description("system directory on RISC")]
- SYSTEN_RISC = 0x29,
- [Description("Program Files on RISC ")]
- Program_Files_RISC = 0x2a,
- [Description("Program Files\\Common")]
- Common = 0x2b,
- [Description("Program Files\\Common on RISC")]
- Common_RISC = 0x2c,
- [Description("All Users\\Templates ")]
- Templates = 0x2d,
- [Description("All Users\\Documents")]
- All_Users_Documents = 0x2e,
- [Description("All Users\\Start Menu\\Programs\\Administrative Tools")]
- AdministrativeTools = 0x2f,
- [Description("<user name>\\Start Menu\\Programs\\Administrative Tools")]
- USER_AdministrativeTools = 0x30,
- [Description("Network and Dial-up Connections")]
- Network_DialUp_Connections = 0x31,
- [Description("All Users\\My Music")]
- All_Users_MyMusic = 0x35,
- [Description("All Users\\My Pictures")]
- All_Users_MyPictures = 0x36,
- [Description("All Users\\My Video")]
- All_Users_MyVideo = 0x37,
- [Description("Resource Directory")]
- Resource = 0x38,
- [Description("Localized Resource Directory ")]
- Localized_Resource = 0x39,
- [Description("OEM specific apps")]
- OEM_Specific = 0x3a,
- [Description("USERPROFILE\\Local Settings\\Application Data\\Microsoft\\CD Burning")]
- CDBurning = 0x3b
- }
- }
|