예제 #1
0
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            IntPtr zero = IntPtr.Zero;
            bool   flag = false;

            NativeMethods.SHGetSpecialFolderLocation(hWndOwner, (int)this.rootFolder, ref zero);
            if (zero == IntPtr.Zero)
            {
                NativeMethods.SHGetSpecialFolderLocation(hWndOwner, 0, ref zero);
                if (zero == IntPtr.Zero)
                {
                    throw new InvalidOperationException("FolderBrowserDialogNoRootFolder");
                }
            }
            int num = 0x40;

            if (!this.showNewFolderButton)
            {
                num += 0x200;
            }

            IntPtr pidl    = IntPtr.Zero;
            IntPtr hglobal = IntPtr.Zero;
            IntPtr pszPath = IntPtr.Zero;

            try
            {
                NativeMethods.BROWSEINFO lpbi = new NativeMethods.BROWSEINFO();
                hglobal             = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                pszPath             = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                this.callback       = new BrowseCallbackProc(this.FolderBrowserDialog_BrowseCallbackProc);
                lpbi.pidlRoot       = zero;
                lpbi.hwndOwner      = hWndOwner;
                lpbi.pszDisplayName = hglobal;
                lpbi.lpszTitle      = this.descriptionText;
                lpbi.ulFlags        = num;
                lpbi.lpfn           = this.callback;
                lpbi.lParam         = IntPtr.Zero;
                lpbi.iImage         = 0;
                pidl = NativeMethods.SHBrowseForFolder(lpbi);
                if (pidl != IntPtr.Zero)
                {
                    NativeMethods.SHGetPathFromIDList(pidl, pszPath);
                    this.selectedPathNeedsCheck = true;
                    this.selectedPath           = Marshal.PtrToStringAuto(pszPath);
                    flag = true;
                }
            }
            finally
            {
                NativeMethods.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.callback = null;
            }
            return(flag);
        }
예제 #2
0
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool result = false;

            IntPtr pidlRoot     = IntPtr.Zero,
                   pszPath      = IntPtr.Zero,
                   pidlSelected = IntPtr.Zero;

            SelectedPath = string.Empty;

            try
            {
                if (RootType == RootType.SpecialFolder)
                {
                    NativeMethods.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, IntPtr.Zero, 0, out pidlRoot);
                }
                else // RootType == Path
                {
                    uint iAttribute;
                    NativeMethods.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute);
                }

                var browseInfo = new NativeMethods.BROWSEINFO
                {
                    HwndOwner   = hwndOwner,
                    Root        = pidlRoot,
                    DisplayName = new string(' ', 256),
                    Title       = Title,
                    Flags       = (uint)_dialogOptions,
                    LParam      = 0,
                    Callback    = HookProc
                };

                // Show dialog
                pidlSelected = NativeMethods.SHBrowseForFolder(ref browseInfo);

                if (pidlSelected != IntPtr.Zero)
                {
                    result = true;

                    pszPath = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                    NativeMethods.SHGetPathFromIDList(pidlSelected, pszPath);

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally // release all unmanaged resources
            {
                NativeMethods.IMalloc malloc = NativeMethods.GetSHMalloc();

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

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

                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }

                Marshal.ReleaseComObject(malloc);
            }

            return(result);
        }
예제 #3
0
 private static NativeMethods.IMalloc GetSHMalloc()
 {
     NativeMethods.IMalloc[] ppMalloc = new NativeMethods.IMalloc[1];
     NativeMethods.SHGetMalloc(ppMalloc);
     return(ppMalloc[0]);
 }
예제 #4
0
파일: Dialogs.cs 프로젝트: kav-it/SharpLib
        protected override Boolean RunDialog(IntPtr hwndOwner)
        {
            Boolean result = false;

            IntPtr pidlRoot     = IntPtr.Zero,
                   pszPath      = IntPtr.Zero,
                   pidlSelected = IntPtr.Zero;

            try
            {
                if (RootType == DialogFolderType.SpecialFolder)
                {
                    NativeMethods.ShellGetFolderLocation(hwndOwner, (int)RootSpecialFolder, IntPtr.Zero, 0, out pidlRoot);
                }
                else
                {
                    uint iAttribute;
                    NativeMethods.ShellParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute);
                }

                NativeMethods.BrowseInfo browseInfo = new NativeMethods.BrowseInfo
                {
                    HwndOwner   = hwndOwner,
                    Root        = pidlRoot,
                    DisplayName = new String(' ', 256),
                    Title       = Title,
                    Flags       = (uint)_dialogOptions,
                    LParam      = 0,
                    Callback    = new NativeMethods.WndProc(HookProc)
                };

                // Show dialog
                pidlSelected = NativeMethods.ShellBrowseForFolder(ref browseInfo);

                if (pidlSelected != IntPtr.Zero)
                {
                    result = true;

                    pszPath = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                    NativeMethods.ShellGetPathFromIDList(pidlSelected, pszPath);

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally
            {
                NativeMethods.IMalloc malloc = NativeMethods.GetShMalloc();

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

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

                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }

                Marshal.ReleaseComObject(malloc);
            }

            return(result);
        }