コード例 #1
0
        /// <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);
        }
コード例 #2
0
 public static extern IntPtr SHBrowseForFolder([In] BROWSEINFO lpbi);