/// <summary>Initializes a new instance of the <see cref="BROWSEINFO"/> struct.</summary> /// <param name="hWnd">A handle to the owner window for the dialog box.</param> /// <param name="rootPidl">A PIDL that specifies the location of the root folder from which to start browsing.</param> /// <param name="title">The string that is displayed above the tree view control in the dialog box.</param> /// <param name="flags">Flags that specify the options for the dialog box.</param> /// <param name="callback">The callback function that the dialog box calls when an event occurs..</param> /// <param name="displayNameBuffer">Buffer that receives the display name of the folder selected by the user.</param> public BROWSEINFO(HWND hWnd, IntPtr rootPidl, string title, BrowseInfoFlag flags, BrowseCallbackProc callback, SafeCoTaskMemString displayNameBuffer) { hwndOwner = hWnd; pidlRoot = rootPidl; pszDisplayName = (IntPtr)displayNameBuffer; lpszTitle = title; ulFlags = flags; lpfn = callback; lParam = IntPtr.Zero; iImage = 0; }
public ShellBrowseForFolderDialog() { hwndOwner = IntPtr.Zero; RootType = RootTypeOptions.BySpecialFolder; RootSpecialFolder = ShellApi.CSIDL.CSIDL_DESKTOP; RootPath = ""; m_DisplayName = ""; Title = ""; UserToken = IntPtr.Zero; m_FullName = ""; // Default flags values DetailsFlags = BrowseInfoFlag.BIF_BROWSEINCLUDEFILES | BrowseInfoFlag.BIF_EDITBOX | BrowseInfoFlag.BIF_NEWDIALOGSTYLE | BrowseInfoFlag.BIF_SHAREABLE | BrowseInfoFlag.BIF_STATUSTEXT | BrowseInfoFlag.BIF_USENEWUI | BrowseInfoFlag.BIF_VALIDATE; }
/// <summary></summary> protected override bool RunDialog(IntPtr hWndOwner) { IntPtr zero = IntPtr.Zero; bool flag = false; Win32.SHGetSpecialFolderLocation(hWndOwner, (int)this.mRootFolder, ref zero); if (zero == IntPtr.Zero) { Win32.SHGetSpecialFolderLocation(hWndOwner, 0, ref zero); if (zero == IntPtr.Zero) { throw new InvalidOperationException("No Root Folder."); } } BrowseInfoFlag flags = BrowseInfoFlag.NewUserInterface; if (!this.mShowNewFolderButton) { flags |= BrowseInfoFlag.NoNewFolderButton; } if (this.mIncludeFiles) { flags |= BrowseInfoFlag.BrowseEverything; } if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != ApartmentState.STA)) { throw new ThreadStateException("Thread Must Be STA."); } IntPtr pidl = IntPtr.Zero; IntPtr hglobal = IntPtr.Zero; IntPtr pszPath = IntPtr.Zero; try { this.mCurrentCallback = new BrowseCallbackProc(this.FolderBrowserDialog_BrowseCallbackProc); hglobal = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize)); pszPath = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize)); BROWSEINFO lpbi = new BROWSEINFO(); lpbi.pidlRoot = zero; lpbi.hwndOwner = hWndOwner; lpbi.pszDisplayName = hglobal; lpbi.lpszTitle = this.mDescriptionText; lpbi.ulFlags = (int)flags; lpbi.lpfn = this.mCurrentCallback; lpbi.lParam = IntPtr.Zero; lpbi.iImage = 0; pidl = Win32.SHBrowseForFolder(lpbi); if (pidl != IntPtr.Zero) { Win32.SHGetPathFromIDList(pidl, pszPath); this.mSelectedPathNeedsCheck = true; this.mSelectedPath = Marshal.PtrToStringAuto(pszPath); flag = true; } } finally { IMalloc sHMalloc = GetSHMalloc(); sHMalloc.Free(zero); if (pidl != IntPtr.Zero) { sHMalloc.Free(pidl); } if (pszPath != IntPtr.Zero) { Marshal.FreeHGlobal(pszPath); } if (hglobal != IntPtr.Zero) { Marshal.FreeHGlobal(hglobal); } this.mCurrentCallback = null; } return(flag); }
/// <summary> /// Shows the dialog box to let the user browse for and select a folder. /// </summary> /// <param name="parentWindowHandle">The HWND of the parent window.</param> /// <returns>The selected folder or <c>null</c> if no folder was selected by the user.</returns> protected override bool RunDialog(IntPtr parentWindowHandle) { // Make sure OLE is initialized. This is a prerequisite for calling SHBrowseForFolder. NativeMethods.OleInitialize(IntPtr.Zero); IntPtr rpidl = IntPtr.Zero; NativeMethods.SHGetSpecialFolderLocation(parentWindowHandle, (int)RootFolder, out rpidl); if (rpidl == IntPtr.Zero) { NativeMethods.SHGetSpecialFolderLocation(parentWindowHandle, 0, out rpidl); if (rpidl == IntPtr.Zero) { throw new InvalidOperationException("No root folder specified for FolderBrowserDialog2."); } } if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != System.Threading.ApartmentState.STA)) { throw new System.Threading.ThreadStateException("Debugging Exception Only. Thread must be STA."); } BrowseInfoFlag browseInfoFlag = BrowseInfoFlag.BIF_NEWDIALOGSTYLE | BrowseInfoFlag.BIF_SHAREABLE; if (!ShowNewFolderButton) { browseInfoFlag |= BrowseInfoFlag.BIF_NONEWFOLDERBUTTON; } if (ShowFolderPathEditBox) { browseInfoFlag |= BrowseInfoFlag.BIF_EDITBOX | BrowseInfoFlag.BIF_VALIDATE; } if (ShowFiles) { browseInfoFlag |= BrowseInfoFlag.BIF_BROWSEINCLUDEFILES; } browseInfoFlag |= (BrowseInfoFlag)BrowserFlag; NativeMethods.BROWSEINFO bi; bi.hwndOwner = parentWindowHandle; bi.pidlRoot = rpidl; bi.pszDisplayName = new string('\0', 260); bi.lpszTitle = Description; bi.ulFlags = (uint)browseInfoFlag; bi.lpfn = new NativeMethods.BrowseCallBackProc(OnBrowseEvent); bi.lParam = IntPtr.Zero; bi.iImage = 0; StringBuilder sb = new StringBuilder(260); IntPtr pidl = IntPtr.Zero; try { pidl = NativeMethods.SHBrowseForFolder(ref bi); if (((browseInfoFlag & BrowseInfoFlag.BIF_BROWSEFORPRINTER) == BrowseInfoFlag.BIF_BROWSEFORPRINTER) || ((browseInfoFlag & BrowseInfoFlag.BIF_BROWSEFORCOMPUTER) == BrowseInfoFlag.BIF_BROWSEFORCOMPUTER)) { SelectedPath = bi.pszDisplayName; } else { if (pidl == IntPtr.Zero || 0 == NativeMethods.SHGetPathFromIDList(pidl, sb)) { return(false); } SelectedPath = sb.ToString(); } return(true); } finally { if (rpidl != IntPtr.Zero) { Marshal.FreeCoTaskMem(rpidl); } if (pidl != IntPtr.Zero) { Marshal.FreeCoTaskMem(pidl); } } }