Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / FolderNameEditor.cs / 1 / FolderNameEditor.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms.Design { using System; using System.Design; using System.ComponentModel; using Microsoft.Win32; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Drawing.Design; using System.IO; using System.Security; using System.Security.Permissions; using System.Runtime.InteropServices; ////// /// /// public class FolderNameEditor : UITypeEditor { private FolderBrowser folderBrowser; ///Provides an editor /// for choosing a folder from the filesystem. ////// /// /// [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { if (folderBrowser == null) { folderBrowser = new FolderBrowser(); InitializeDialog(folderBrowser); } if (folderBrowser.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return value; } return folderBrowser.DirectoryPath; } ///[To be supplied.] ////// /// /// Retrieves the editing style of the Edit method. If the method /// is not supported, this will return None. /// [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } ////// /// /// Initializes the folder browser dialog when it is created. This gives you /// an opportunity to configure the dialog as you please. The default /// implementation provides a generic folder browser. /// protected virtual void InitializeDialog(FolderBrowser folderBrowser) { } ////// /// /// protected sealed class FolderBrowser : Component { private static readonly int MAX_PATH = 260; // Root node of the tree view. private FolderBrowserFolder startLocation = FolderBrowserFolder.Desktop; // Browse info options private FolderBrowserStyles publicOptions = FolderBrowserStyles.RestrictToFilesystem; private UnsafeNativeMethods.BrowseInfos privateOptions = UnsafeNativeMethods.BrowseInfos.NewDialogStyle; // Description text to show. private string descriptionText = String.Empty; // Folder picked by the user. private string directoryPath = String.Empty; ///[To be supplied.] ////// /// /// The styles the folder browser will use when browsing /// folders. This should be a combination of flags from /// the FolderBrowserStyles enum. /// public FolderBrowserStyles Style { get { return publicOptions; } set { publicOptions = value; } } ////// /// /// Gets the directory path of the folder the user picked. /// public string DirectoryPath { get { return directoryPath; } } ////// /// /// Gets/sets the start location of the root node. /// public FolderBrowserFolder StartLocation { get { return startLocation; } set { startLocation = value; } } ////// /// /// public string Description { get { return descriptionText; } set { descriptionText = (value == null) ? String.Empty: value; } } ////// Gets or sets a description to show above the folders. Here you can provide instructions for /// selecting a folder. /// ////// /// Helper function that returns the IMalloc interface used by the shell. /// private static UnsafeNativeMethods.IMalloc GetSHMalloc() { UnsafeNativeMethods.IMalloc[] malloc = new UnsafeNativeMethods.IMalloc[1]; UnsafeNativeMethods.Shell32.SHGetMalloc(malloc); return malloc[0]; } ////// /// /// Shows the folder browser dialog. /// public DialogResult ShowDialog() { return ShowDialog(null); } ////// /// /// Shows the folder browser dialog with the specified owner. /// public DialogResult ShowDialog(IWin32Window owner) { IntPtr pidlRoot = IntPtr.Zero; // Get/find an owner HWND for this dialog IntPtr hWndOwner; if (owner != null) { hWndOwner = owner.Handle; } else { hWndOwner = UnsafeNativeMethods.GetActiveWindow(); } // Get the IDL for the specific startLocation UnsafeNativeMethods.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int) startLocation, ref pidlRoot); if (pidlRoot == IntPtr.Zero) { return DialogResult.Cancel; } int mergedOptions = (int)publicOptions | (int)privateOptions; if ((mergedOptions & (int)UnsafeNativeMethods.BrowseInfos.NewDialogStyle) != 0) { Application.OleRequired(); } IntPtr pidlRet = IntPtr.Zero; try { // Construct a BROWSEINFO UnsafeNativeMethods.BROWSEINFO bi = new UnsafeNativeMethods.BROWSEINFO(); IntPtr buffer = Marshal.AllocHGlobal(MAX_PATH); bi.pidlRoot = pidlRoot; bi.hwndOwner = hWndOwner; bi.pszDisplayName = buffer; bi.lpszTitle = descriptionText; bi.ulFlags = mergedOptions; bi.lpfn = IntPtr.Zero; bi.lParam = IntPtr.Zero; bi.iImage = 0; // And show the dialog pidlRet = UnsafeNativeMethods.Shell32.SHBrowseForFolder(bi); if (pidlRet == IntPtr.Zero) { // User pressed Cancel return DialogResult.Cancel; } // Then retrieve the path from the IDList UnsafeNativeMethods.Shell32.SHGetPathFromIDList(pidlRet, buffer); // Convert to a string directoryPath = Marshal.PtrToStringAuto(buffer); // Then free all the stuff we've allocated or the SH API gave us Marshal.FreeHGlobal(buffer); } finally { UnsafeNativeMethods.IMalloc malloc = GetSHMalloc(); malloc.Free(pidlRoot); if (pidlRet != IntPtr.Zero) { malloc.Free(pidlRet); } } return DialogResult.OK; } } ////// protected enum FolderBrowserFolder { /// Desktop = 0x0000, /// Favorites = 0x0006, /// MyComputer = 0x0011, /// MyDocuments = 0x0005, /// MyPictures = 0x0027, /// NetAndDialUpConnections = 0x0031, /// NetworkNeighborhood = 0x0012, /// Printers = 0x0004, /// Recent = 0x0008, /// SendTo = 0x0009, /// StartMenu = 0x000b, /// Templates = 0x0015, } /// /// [Flags] protected enum FolderBrowserStyles { /// BrowseForComputer = UnsafeNativeMethods.BrowseInfos.BrowseForComputer, /// BrowseForEverything = UnsafeNativeMethods.BrowseInfos.BrowseForEverything, /// BrowseForPrinter = UnsafeNativeMethods.BrowseInfos.BrowseForPrinter, /// RestrictToDomain = UnsafeNativeMethods.BrowseInfos.DontGoBelowDomain, /// RestrictToFilesystem = UnsafeNativeMethods.BrowseInfos.ReturnOnlyFSDirs, /// RestrictToSubfolders = UnsafeNativeMethods.BrowseInfos.ReturnFSAncestors, /// ShowTextBox = UnsafeNativeMethods.BrowseInfos.EditBox, } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TreeWalker.cs
- DesignBinding.cs
- ColumnResizeUndoUnit.cs
- DataGridItem.cs
- DataListAutoFormat.cs
- AggregatePushdown.cs
- SemaphoreSecurity.cs
- SamlConstants.cs
- TextBlock.cs
- XmlNavigatorStack.cs
- WebPartConnectionsCancelVerb.cs
- TextSelectionHelper.cs
- EntityDescriptor.cs
- Helper.cs
- PrePrepareMethodAttribute.cs
- TextServicesContext.cs
- MatchAttribute.cs
- FixedLineResult.cs
- TreeView.cs
- InkCanvasAutomationPeer.cs
- XmlSerializationReader.cs
- CollectionType.cs
- FrameworkTextComposition.cs
- Reference.cs
- GenerateTemporaryAssemblyTask.cs
- CapabilitiesRule.cs
- CodeDirectionExpression.cs
- MdiWindowListItemConverter.cs
- MachineKeySection.cs
- SelectionItemProviderWrapper.cs
- LockRecoveryTask.cs
- NavigationPropertyEmitter.cs
- EventLogConfiguration.cs
- PreviewKeyDownEventArgs.cs
- OrderedDictionaryStateHelper.cs
- TextEndOfParagraph.cs
- ProviderUtil.cs
- IODescriptionAttribute.cs
- XPathAncestorQuery.cs
- TimeManager.cs
- HMACRIPEMD160.cs
- XmlConvert.cs
- PropertyGridEditorPart.cs
- DragStartedEventArgs.cs
- EntryPointNotFoundException.cs
- LinearGradientBrush.cs
- ExpressionVisitorHelpers.cs
- DbgCompiler.cs
- IdleTimeoutMonitor.cs
- SiteMapProvider.cs
- XmlTextReaderImpl.cs
- ServiceChannel.cs
- WebPartVerbsEventArgs.cs
- ManipulationLogic.cs
- BodyGlyph.cs
- WmlValidatorAdapter.cs
- QfeChecker.cs
- X509Utils.cs
- DataSourceCache.cs
- BypassElement.cs
- FieldBuilder.cs
- UrlAuthFailedErrorFormatter.cs
- SchemaMapping.cs
- IndexerReference.cs
- RangeValueProviderWrapper.cs
- NameValueCollection.cs
- XPathNodePointer.cs
- input.cs
- SerializationInfoEnumerator.cs
- ExpressionConverter.cs
- UpDownBase.cs
- ThreadSafeList.cs
- WorkflowTransactionService.cs
- DataServiceKeyAttribute.cs
- ImportContext.cs
- TextDecorationCollectionConverter.cs
- DoubleConverter.cs
- xmlfixedPageInfo.cs
- TextParentUndoUnit.cs
- AliasedSlot.cs
- SystemBrushes.cs
- SqlDataSourceEnumerator.cs
- StrokeNodeEnumerator.cs
- Style.cs
- CharacterBufferReference.cs
- WpfKnownTypeInvoker.cs
- ProjectionCamera.cs
- XmlCharCheckingWriter.cs
- HandlerMappingMemo.cs
- StdRegProviderWrapper.cs
- XmlSchemaIdentityConstraint.cs
- PageCache.cs
- SafeMILHandleMemoryPressure.cs
- XmlCharCheckingWriter.cs
- EdmValidator.cs
- UnicastIPAddressInformationCollection.cs
- GlyphRun.cs
- StorageAssociationTypeMapping.cs
- Transform3D.cs
- Environment.cs