Пример #1
0
        private Win32API.IMalloc GetSHMalloc()
        {
            var ppMalloc = new Win32API.IMalloc[1];

            Win32API.Shell32.SHGetMalloc(ppMalloc);

            return(ppMalloc[0]);
        }
            /// <summary>
            ///
            /// </summary>
            /// <param name="hwndOwner"></param>
            /// <returns></returns>
            protected override bool RunDialog(System.IntPtr hwndOwner)
            {
                IntPtr ptr1  = IntPtr.Zero;
                bool   flag1 = false;

                Win32API.SHGetSpecialFolderLocation(hwndOwner, (int)m_rootFolder, ref ptr1);
                if (ptr1 == IntPtr.Zero)
                {
                    Win32API.SHGetSpecialFolderLocation(hwndOwner, 0, ref ptr1);
                    if (ptr1 == IntPtr.Zero)
                    {
                        throw new Exception("FolderBrowserDialogNoRootFolder");
                    }
                }

                //Initialize the OLE to current thread.
                Application.OleRequired();
                IntPtr ptr2 = IntPtr.Zero;

                try
                {
                    Win32API.BROWSEINFO browseinfo1 = new Win32API.BROWSEINFO();
                    IntPtr ptr3 = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                    IntPtr ptr4 = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                    Win32API.BrowseCallbackProc proc1 = new Win32API.BrowseCallbackProc(this.FolderBrowserDialog_BrowseCallbackProc);
                    browseinfo1.pidlRoot       = ptr1;
                    browseinfo1.hwndOwner      = hwndOwner;
                    browseinfo1.pszDisplayName = ptr3;
                    browseinfo1.lpszTitle      = m_descriptionText;
                    browseinfo1.ulFlags        = 0x40;
                    browseinfo1.lpfn           = proc1;
                    browseinfo1.lParam         = IntPtr.Zero;
                    browseinfo1.iImage         = 0;
                    ptr2 = Win32API.SHBrowseForFolder(browseinfo1);

                    string s = Marshal.PtrToStringAuto(ptr3);

                    if (ptr2 != IntPtr.Zero)
                    {
                        Win32API.SHGetPathFromIDList(ptr2, ptr4);
                        this.m_selectedPath = Marshal.PtrToStringAuto(ptr4);
                        Marshal.FreeHGlobal(ptr4);
                        Marshal.FreeHGlobal(ptr3);
                        flag1 = true;
                    }
                }
                finally
                {
                    Win32API.IMalloc malloc1 = GetSHMalloc();
                    malloc1.Free(ptr1);
                    if (ptr2 != IntPtr.Zero)
                    {
                        malloc1.Free(ptr2);
                    }
                }
                return(flag1);
            }
        /// <summary>
        /// Shows the folder browser dialog box with the specified owner window.
        /// </summary>
        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 = Win32API.GetActiveWindow();
            }

            // Get the IDL for the specific startLocation.
            Win32API.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)startLocation, out pidlRoot);

            if (pidlRoot == IntPtr.Zero)
            {
                return(DialogResult.Cancel);
            }

            int mergedOptions = (int)publicOptions | (int)privateOptions;

            if ((mergedOptions & (int)Win32API.Shell32.BffStyles.NewDialogStyle) != 0)
            {
                if (System.Threading.ApartmentState.MTA == Application.OleRequired())
                {
                    mergedOptions = mergedOptions & (~(int)Win32API.Shell32.BffStyles.NewDialogStyle);
                }
            }

            IntPtr pidlRet = IntPtr.Zero;

            try
            {
                // Construct a BROWSEINFO.
                Win32API.Shell32.BROWSEINFO bi = new Win32API.Shell32.BROWSEINFO();
                IntPtr buffer = Marshal.AllocHGlobal(MAX_PATH);

                bi.pidlRoot       = pidlRoot;
                bi.hwndOwner      = hWndOwner;
                bi.pszDisplayName = buffer;
                bi.lpszTitle      = Description;
                bi.ulFlags        = mergedOptions;
                bi.lpfn           = new Win32API.Shell32.BFFCALLBACK(callback);
                // The rest of the fields are initialized to zero by the constructor.
                // bi.lParam = IntPtr.Zero;    bi.iImage = 0;

                // Show the dialog.
                pidlRet = Win32API.Shell32.SHBrowseForFolder(ref bi);

                // Free the buffer you've allocated on the global heap.
                Marshal.FreeHGlobal(buffer);

                if (pidlRet == IntPtr.Zero)
                {
                    // User clicked Cancel.
                    return(DialogResult.Cancel);
                }

                // Then retrieve the path from the IDList.
                StringBuilder sb = new StringBuilder(MAX_PATH);
                if (0 == Win32API.Shell32.SHGetPathFromIDList(pidlRet, sb))
                {
                    return(DialogResult.Cancel);
                }

                // Convert to a string.
                SelectedPath = sb.ToString();
            }
            finally
            {
                Win32API.IMalloc malloc = GetSHMalloc();
                malloc.Free(pidlRoot);

                if (pidlRet != IntPtr.Zero)
                {
                    malloc.Free(pidlRet);
                }
            }

            return(DialogResult.OK);
        }
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 private Win32API.IMalloc GetSHMalloc()
 {
     Win32API.IMalloc[] mallocArray1 = new Win32API.IMalloc[1];
     Win32API.SHGetMalloc(mallocArray1);
     return(mallocArray1[0]);
 }