Beispiel #1
0
        public DialogResult ShowDialog(IWin32Window owner)
        {
            InitData initdata = new InitData(this, owner.Handle);

            Win32.BROWSEINFO bi = new Win32.BROWSEINFO
            {
                iImage    = 0,
                hwndOwner = owner.Handle
            };

            if (Win32.SHGetSpecialFolderLocation(owner.Handle, (int)RootFolder, ref bi.pidlRoot) != 0)
            {
                bi.pidlRoot = IntPtr.Zero;
            }
            bi.lpszTitle = "";
            bi.ulFlags   = Win32.BIF_RETURNONLYFSDIRS;  // do NOT use BIF_NEWDIALOGSTYLE or BIF_STATUSTEXT
            if (ShowEditbox)
            {
                bi.ulFlags |= Win32.BIF_EDITBOX;
            }
            if (!ShowNewFolderButton)
            {
                bi.ulFlags |= Win32.BIF_NONEWFOLDERBUTTON;
            }
            bi.lpfn = _browseCallbackHandler;
            // Initialization data, used in _browseCallbackHandler
            IntPtr hInit = Marshal.AllocHGlobal(Marshal.SizeOf(initdata));

            Marshal.StructureToPtr(initdata, hInit, true);
            bi.lParam = hInit;

            IntPtr pidlSelectedPath = IntPtr.Zero;

            try
            {
                pidlSelectedPath = Win32.SHBrowseForFolder(ref bi);
                StringBuilder sb = new StringBuilder(256);
                if (Win32.SHGetPathFromIDList(pidlSelectedPath, sb))
                {
                    SelectedPath = sb.ToString();
                    return(DialogResult.OK);
                }
            }
            finally
            {
                // Caller is responsible for freeing this memory.
                Marshal.FreeCoTaskMem(pidlSelectedPath);
            }

            return(DialogResult.Cancel);
        }
        public DialogResult ShowDialog(IWin32Window owner)
        {
            InitData initdata = new InitData(this, owner.Handle);

            Win32.BROWSEINFO bi = new Win32.BROWSEINFO();
            bi.iImage = 0;
            bi.hwndOwner = owner.Handle;
            if (0 != Win32.SHGetSpecialFolderLocation(owner.Handle, (int)this.RootFolder, ref bi.pidlRoot))
                bi.pidlRoot = IntPtr.Zero;
            bi.lpszTitle = "";
            bi.ulFlags = Win32.BIF_RETURNONLYFSDIRS;	// do NOT use BIF_NEWDIALOGSTYLE or BIF_STATUSTEXT
            if (this.ShowEditbox)
                bi.ulFlags |= Win32.BIF_EDITBOX;
            if (!this.ShowNewFolderButton)
                bi.ulFlags |= Win32.BIF_NONEWFOLDERBUTTON;
            bi.lpfn = new Win32.BrowseCallbackProc(_browseCallbackHandler);
            // Initialization data, used in _browseCallbackHandler
            IntPtr hInit = Marshal.AllocHGlobal(Marshal.SizeOf(initdata));
            Marshal.StructureToPtr(initdata, hInit, true);
            bi.lParam = hInit;

            IntPtr pidlSelectedPath = IntPtr.Zero;
            try
            {
                pidlSelectedPath = Win32.SHBrowseForFolder(ref bi);
                StringBuilder sb = new StringBuilder(256);
                if (Win32.SHGetPathFromIDList(pidlSelectedPath, sb))
                {
                    SelectedPath = sb.ToString();
                    return DialogResult.OK;
                }
            }
            finally
            {
                // Caller is responsible for freeing this memory.
                Marshal.FreeCoTaskMem(pidlSelectedPath);
            }

            return DialogResult.Cancel;
        }