Ejemplo n.º 1
0
        protected override bool RunDialog(System.IntPtr hwndOwner)
        {
            string m_DisplayName = "";
            string sDisplay;
            IntPtr ptrRet;

            // Get IMalloc interface
            WindowsAPI.SHGetMalloc(out ptrRet);

            Type    mallocType = System.Type.GetType("IMalloc");
            IMalloc pMalloc    = ( IMalloc )Marshal.GetTypedObjectForIUnknown(ptrRet, mallocType);
            IntPtr  pidlRoot   = IntPtr.Zero;

            BROWSEINFO bi = new BROWSEINFO();

            bi.hwndOwner      = hwndOwner;
            bi.pidlRoot       = pidlRoot; //TODO: Root`s selection
            bi.pszDisplayName = new string( ' ', 256 );
            bi.lpszTitle      = Title;
            bi.ulFlags        = GetFlagsValue();
            bi.lParam         = 0;
            bi.lpfn           = new WindowsAPI.BrowseCallbackProc(this.HookProc);

            IntPtr pidlSelected;

            pidlSelected  = WindowsAPI.SHBrowseForFolder(ref bi);
            m_DisplayName = bi.pszDisplayName.ToString();

            // if display name is whitespace then return FAIL
            if (m_DisplayName.Trim() == string.Empty)
            {
                return(false);
            }

            IShellFolder isf = GetDesktopFolder();

            STRRET ptrDisplayName;

            isf.GetDisplayNameOf(pidlSelected,
                                 (uint)SHGNO.SHGDN_NORMAL | (uint)SHGNO.SHGDN_FORPARSING,
                                 out ptrDisplayName);

            WindowsAPI.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay);
            m_sFullName = sDisplay;

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

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

            Marshal.ReleaseComObject(isf);
            Marshal.ReleaseComObject(pMalloc);

            return(true);
        }
Ejemplo n.º 2
0
        private bool RunDialogDownlevel(IntPtr owner)
        {
            IntPtr rootItemIdList   = IntPtr.Zero;
            IntPtr resultItemIdList = IntPtr.Zero;

            if (NativeMethods.SHGetSpecialFolderLocation(owner, RootFolder, ref rootItemIdList) != 0)
            {
                if (NativeMethods.SHGetSpecialFolderLocation(owner, 0, ref rootItemIdList) != 0)
                {
                    throw new InvalidOperationException("Unable to retrieve the root folder.");
                }
            }
            try
            {
                NativeMethods.BROWSEINFO info = new NativeMethods.BROWSEINFO
                {
                    hwndOwner      = owner,
                    lpfn           = new NativeMethods.BrowseCallbackProc(BrowseCallbackProc),
                    lpszTitle      = Description,
                    pidlRoot       = rootItemIdList,
                    pszDisplayName = new string('\0', 260),
                    ulFlags        = NativeMethods.BrowseInfoFlags.NewDialogStyle | NativeMethods.BrowseInfoFlags.ReturnOnlyFsDirs
                };
                if (!ShowNewFolderButton)
                {
                    info.ulFlags |= NativeMethods.BrowseInfoFlags.NoNewFolderButton;
                }
                resultItemIdList = NativeMethods.SHBrowseForFolder(ref info);
                if (resultItemIdList != IntPtr.Zero)
                {
                    StringBuilder path = new StringBuilder(260);
                    NativeMethods.SHGetPathFromIDList(resultItemIdList, path);
                    SelectedPath = path.ToString();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                if (rootItemIdList != null)
                {
                    IMalloc malloc = NativeMethods.SHGetMalloc();
                    malloc.Free(rootItemIdList);
                    Marshal.ReleaseComObject(malloc);
                }
                if (resultItemIdList != null)
                {
                    Marshal.FreeCoTaskMem(resultItemIdList);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 指定した特殊フォルダのアイコンイメージを取得する
        /// </summary>
        /// <param name="targetFolder">アイコンイメージ取得対象の特殊フォルダを指すenum</param>
        /// <param name="size">
        /// 取得するアイコンのサイズ
        /// </param>
        /// <returns>取得されたアイコンをIconクラスで返す</returns>
        public static Icon GetSpecialFolderAssociatedImage(SpecialFolderID targetFolder, EIconSize size)
        {
            uint flags = Shell32Wrapper.SHGFI_ICON | Shell32Wrapper.SHGFI_PIDL;

            if (size == EIconSize.Small)
            {
                flags |= Shell32Wrapper.SHGFI_SMALLICON;
            }
            SHFILEINFO fileInfo = new SHFILEINFO();
            IntPtr     pidlRoot = IntPtr.Zero;

            try
            {
                // 特殊フォルダのpIDLを取得
                Shell32Wrapper.SHGetSpecialFolderLocation(IntPtr.Zero, (int)targetFolder, out pidlRoot);

                // アイコンの取得
                Shell32Wrapper.SHGetFileInfo(pidlRoot, 0, ref fileInfo, (uint)Marshal.SizeOf(fileInfo), flags);
                if (fileInfo.hIcon == IntPtr.Zero)
                {
                    return(null);
                }
                else
                {
                    var unmanagedIcon = Icon.FromHandle(fileInfo.hIcon);
                    return(new Icon(unmanagedIcon, unmanagedIcon.Size)); // unmanagedIconは破棄されるのでManagedで新しく作成する
                }
            }
            finally
            {
                if (fileInfo.hIcon != IntPtr.Zero)
                {
                    User32Wrapper.DestroyIcon(fileInfo.hIcon);
                }

                // pidlを解放する。
                if (pidlRoot != IntPtr.Zero)
                {
                    IMalloc malloc = GetSHMalloc();
                    malloc.Free(pidlRoot);
                }
            }
        }
        public System.Drawing.Bitmap GetThumbnail(string file)
        {
            if (!Util.Utils.FileExistsInCache(file))
            {
                throw new FileNotFoundException(
                          String.Format("The file '{0}' does not exist", file),
                          file);
            }

            if (thumbNail != null)
            {
                thumbNail.SafeDispose();
                thumbNail = null;
            }

            IShellFolder folder = null;

            try
            {
                folder = getDesktopFolder;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (folder != null)
            {
                IntPtr pidlMain = IntPtr.Zero;
                try
                {
                    int    cParsed   = 0;
                    int    pdwAttrib = 0;
                    string filePath  = Path.GetDirectoryName(file);
                    pidlMain = IntPtr.Zero;
                    folder.ParseDisplayName(
                        IntPtr.Zero,
                        IntPtr.Zero,
                        filePath,
                        out cParsed,
                        out pidlMain,
                        out pdwAttrib);
                }
                catch (Exception ex)
                {
                    Marshal.ReleaseComObject(folder);
                    throw ex;
                }

                if (pidlMain != IntPtr.Zero)
                {
                    // IShellFolder:
                    Guid iidShellFolder = new
                                          Guid("000214E6-0000-0000-C000-000000000046");
                    IShellFolder item = null;

                    try
                    {
                        folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
                    }
                    catch (Exception ex)
                    {
                        Marshal.ReleaseComObject(folder);
                        Allocator.Free(pidlMain);
                        throw ex;
                    }

                    //if (item != null)
                    //{
                    //  //
                    //  IEnumIDList idEnum = null;
                    //  try
                    //  {
                    //    item.EnumObjects(
                    //    IntPtr.Zero,
                    //    (ESHCONTF.SHCONTF_FOLDERS |
                    //    ESHCONTF.SHCONTF_NONFOLDERS),
                    //    ref idEnum);
                    //  }
                    //  catch (Exception ex)
                    //  {
                    //    Marshal.ReleaseComObject(folder);
                    //    Allocator.Free(pidlMain);
                    //    throw ex;
                    //  }

                    //  if (idEnum != null)
                    //  {
                    //    // start reading the enum:
                    //    int hRes = 0;
                    //    IntPtr pidl = IntPtr.Zero;
                    //    int fetched = 0;
                    //    bool complete = false;
                    //    while (!complete)
                    //    {
                    //      hRes = idEnum.Next(1, ref pidl, out fetched);
                    //      if (hRes != 0)
                    //      {
                    //        pidl = IntPtr.Zero;
                    //        complete = true;
                    //      }
                    //      else
                    //      {
                    //        if (getThumbnail(file, pidl, item))
                    //        {
                    //          complete = true;
                    //        }
                    //      }
                    //      if (pidl != IntPtr.Zero)
                    //      {
                    //        Allocator.Free(pidl);
                    //      }
                    //    }

                    //    Marshal.ReleaseComObject(idEnum);
                    //  }


                    //  Marshal.ReleaseComObject(item);
                    //}

                    if (item != null)
                    {
                        string sFileName = Path.GetFileName(file);
                        IntPtr pidlFile  = IntPtr.Zero;
                        try
                        {
                            int cParsed   = 0;
                            int pdwAttrib = 0;
                            item.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, sFileName, out cParsed, out pidlFile, out pdwAttrib);
                            if (pidlFile != IntPtr.Zero)
                            {
                                getThumbnail(file, pidlFile, item);
                                Allocator.Free(pidlFile);
                            }
                        }
                        catch (Exception ex)
                        {
                            Marshal.ReleaseComObject(folder);
                            Allocator.Free(pidlMain);
                            throw ex;
                        }
                        Marshal.ReleaseComObject(item);
                    }

                    Allocator.Free(pidlMain);
                }

                Marshal.ReleaseComObject(folder);
            }
            return(thumbNail);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// When overridden in a derived class, specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>
        /// true if the dialog box was successfully run; otherwise, false.
        /// </returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            IntPtr zero = IntPtr.Zero;
            bool   flag = false;

            SHGetSpecialFolderLocation(hwndOwner, (int)rootFolder, ref zero);
            if (zero == IntPtr.Zero)
            {
                SHGetSpecialFolderLocation(hwndOwner, 0, ref zero);
                if (zero == IntPtr.Zero)
                {
                    throw new InvalidOperationException(
                              "Folder Browser Dialog: no root folder");
                }
            }

            //int flags = 0x40;
            //if (!showNewFolderButton) flags += 0x200;

            if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != ApartmentState.STA))
            {
                throw new ThreadStateException("Debugging exception only: Thread must be STA");
            }

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

            try
            {
                BROWSEINFO lpbi = new BROWSEINFO();
                hglobal             = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                pszPath             = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                callback            = new BrowseCallbackProc(FolderBrowserDialog_BrowseCallbackProc);
                lpbi.pidlRoot       = zero;
                lpbi.hwndOwner      = hwndOwner;
                lpbi.pszDisplayName = hglobal;
                lpbi.lpszTitle      = descriptionText;
                lpbi.ulFlags        = (int)flags;
                lpbi.lpfn           = callback;
                lpbi.lParam         = IntPtr.Zero;
                lpbi.iImage         = 0;
                pidl = SHBrowseForFolder(lpbi);
                if (pidl != IntPtr.Zero)
                {
                    SHGetPathFromIDList(pidl, pszPath);
                    selectedPathNeedsCheck = true;
                    selectedPath           = 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);
                }

                callback = null;
            }
            return(flag);
        }
Ejemplo n.º 6
0
 public void Free(IntPtr pv)
 {
     m.Free(pv);
 }
Ejemplo n.º 7
0
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool result = false;

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

            SelectedPath = string.Empty;

            try
            {
                SHGetFolderLocation(hwndOwner, (int)Environment.SpecialFolder.Desktop, IntPtr.Zero, 0, out pidlRoot);

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

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

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

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

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally // release all unmanaged resources
            {
                IMalloc malloc = 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);
        }
Ejemplo n.º 8
0
        public Bitmap GetThumbnail(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(null);
            }

            if (!File.Exists(fileName) && !Directory.Exists(fileName))
            {
                throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName);
            }
            if (_thumbNail != null)
            {
                _thumbNail.Dispose();
                _thumbNail = null;
            }
            IShellFolder folder = null;

            try
            {
                folder = getDesktopFolder;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (folder != null)
            {
                IntPtr pidlMain = IntPtr.Zero;
                try
                {
                    int    cParsed   = 0;
                    int    pdwAttrib = 0;
                    string filePath  = Path.GetDirectoryName(fileName);
                    folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref cParsed, ref pidlMain, ref pdwAttrib);
                }
                catch (Exception ex)
                {
                    Marshal.ReleaseComObject(folder);
                    throw ex;
                }
                if (pidlMain != IntPtr.Zero)
                {
                    Guid         iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
                    IShellFolder item           = null;
                    try
                    {
                        folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
                    }
                    catch (Exception ex)
                    {
                        Marshal.ReleaseComObject(folder);
                        Allocator.Free(pidlMain);
                        throw ex;
                    }
                    if (item != null)
                    {
                        IEnumIDList idEnum = null;
                        try
                        {
                            item.EnumObjects(IntPtr.Zero, (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS), ref idEnum);
                        }
                        catch (Exception ex)
                        {
                            Marshal.ReleaseComObject(folder);
                            Allocator.Free(pidlMain);
                            throw ex;
                        }
                        if (idEnum != null)
                        {
                            int    hRes     = 0;
                            IntPtr pidl     = IntPtr.Zero;
                            int    fetched  = 0;
                            bool   complete = false;
                            while (!complete)
                            {
                                hRes = idEnum.Next(1, ref pidl, ref fetched);
                                if (hRes != 0)
                                {
                                    pidl     = IntPtr.Zero;
                                    complete = true;
                                }
                                else
                                {
                                    if (_getThumbNail(fileName, pidl, item))
                                    {
                                        complete = true;
                                    }
                                }
                                if (pidl != IntPtr.Zero)
                                {
                                    Allocator.Free(pidl);
                                }
                            }
                            Marshal.ReleaseComObject(idEnum);
                        }
                        Marshal.ReleaseComObject(item);
                    }
                    Allocator.Free(pidlMain);
                }
                Marshal.ReleaseComObject(folder);
            }
            return(ThumbNail);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != System.Threading.ApartmentState.STA))
            {
                throw new Exception("Thread must be STA");
            }

            IntPtr pidlRoot = IntPtr.Zero;
            bool   success  = false;

            SHGetSpecialFolderLocation(hwndOwner, (int)_rootFolder, ref pidlRoot);
            if (pidlRoot == IntPtr.Zero)
            {
                SHGetSpecialFolderLocation(hwndOwner, (int)System.Environment.SpecialFolder.Desktop, ref pidlRoot);
                if (pidlRoot == IntPtr.Zero)
                {
                    throw new Exception("No root folder");
                }
            }

            if (null != _rootFolderPath && 0 < _rootFolderPath.Length)
            {
                IntPtr pidlTmp   = IntPtr.Zero;
                IntPtr psfGaoOut = IntPtr.Zero;

                if (S_OK == SHParseDisplayName(_rootFolderPath, IntPtr.Zero, ref pidlTmp, 0, psfGaoOut))
                {
                    pidlRoot = pidlTmp;
                }
            }

            int num = 0x40;

            if (!_showNewFolderButton)
            {
                num += 0x200;
            }

            IntPtr pidl    = IntPtr.Zero;
            IntPtr hGlobal = IntPtr.Zero;
            IntPtr pszPath = IntPtr.Zero;

            try {
                BROWSEINFO lpbi = new BROWSEINFO();
                hGlobal             = Marshal.AllocHGlobal((int)(MAX_PATH * Marshal.SystemDefaultCharSize));
                pszPath             = Marshal.AllocHGlobal((int)(MAX_PATH * Marshal.SystemDefaultCharSize));
                _callback           = new FolderBrowseCallbackProc(this.CallbackProc);
                lpbi.pidlRoot       = pidlRoot;
                lpbi.hwndOwner      = hwndOwner;
                lpbi.pszDisplayName = hGlobal;
                lpbi.lpszTitle      = _description;
                lpbi.ulFlags        = num;
                lpbi.lpfn           = _callback;
                lpbi.lParam         = IntPtr.Zero;
                lpbi.iImage         = 0;
                pidl = SHBrowseForFolder(lpbi);

                if (pidl != IntPtr.Zero)
                {
                    SHGetPathFromIDList(pidl, pszPath);
                    this._selectedPathNeedCheck = true;
                    this._selectedPath          = Marshal.PtrToStringAuto(pszPath);
                    success = true;
                }
            } finally {
                IMalloc shMalloc = GetSHMalloc();
                shMalloc.Free(pidlRoot);
                if (pidl != IntPtr.Zero)
                {
                    shMalloc.Free(pidl);
                }
                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }
                if (hGlobal != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hGlobal);
                }
                _callback = null;
            }

            return(success);
        }
Ejemplo n.º 11
0
        /// <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 = GetActiveWindow();
            }
            // Get the IDL for the specific startLocation.
            if (RootPIDL != IntPtr.Zero)
            {
                pidlRoot = RootPIDL;
            }
            else
            {
                SHGetSpecialFolderLocation(hWndOwner, (int)RootFolderID, out pidlRoot);
            }

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

            if ((flags & (int)BffStyles.NewDialogStyle) != 0)
            {
                if (System.Threading.ApartmentState.MTA == Application.OleRequired())
                {
                    flags = flags & (~(int)BffStyles.NewDialogStyle);
                }
            }
            IntPtr pidlRet = IntPtr.Zero;

            try
            {
                // Construct a BROWSEINFO.
                BROWSEINFO bi     = new BROWSEINFO();
                IntPtr     buffer = Marshal.AllocHGlobal(MAX_PATH);
                bi.pidlRoot       = pidlRoot;
                bi.hwndOwner      = hWndOwner;
                bi.pszDisplayName = buffer;
                bi.lpszTitle      = Description;
                bi.ulFlags        = flags;
                bi.lpfn           = new BFFCALLBACK(FolderBrowserCallback);
                // Show the dialog.
                pidlRet = 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 == SHGetPathFromIDList(pidlRet, sb))
                {
                    SelectedPath = String.Format(":{0}", pidlRet);
                    pidlRet      = IntPtr.Zero; // avoid object to be freed
                }
                else
                {
                    // Convert to a string.
                    SelectedPath = sb.ToString();
                }
            }
            finally
            {
                IMalloc malloc = GetSHMalloc();
                malloc.Free(pidlRoot);
                if (pidlRet != IntPtr.Zero)
                {
                    malloc.Free(pidlRet);
                }
            }
            return(DialogResult.OK);
        }
Ejemplo n.º 12
0
        internal DialogResult ShowDialog()
        {
            IWin32Window owner = null;

            int[] pidlRoot = new int[1];

            // Get/find an owner HWND for this dialog
            IntPtr hwndOwner;

            if (owner != null)
            {
                hwndOwner = owner.Handle;
            }
            else
            {
                hwndOwner = GetActiveWindow();
            }

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

            if (pidlRoot[0] == 0)
            {
                // UNDONE, fabioy:
                // Show we throw an exception here instead?
                return(DialogResult.Cancel);
            }

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

            // UNDONE, fabioy:
            // Expose the display name as another property.
            int pidlRet = 0;

            try {
                // Construct a BROWSEINFO
                BROWSEINFO bi = new BROWSEINFO();

                IntPtr buffer = (IntPtr)Marshal.AllocHGlobal(MAX_PATH);

                bi.pidlRoot       = pidlRoot[0];
                bi.hwndOwner      = hwndOwner;
                bi.pszDisplayName = buffer;
                bi.lpszTitle      = descriptionText;
                bi.ulFlags        = mergedOptions;
                bi.lpfn           = (IntPtr)0;
                bi.lParam         = 0;
                bi.iImage         = (IntPtr)0;

                // And show the dialog
                pidlRet = Shell32.SHBrowseForFolder(bi);

                if (pidlRet == 0)
                {
                    // User pressed Cancel
                    return(DialogResult.Cancel);
                }

                // Then retrieve the path from the IDList
                //Shell32.SHGetPathFromIDList(pidlRet, buffer);

                // Convert to a string
                directoryPath = Marshal.PtrToStringUni(buffer);

                // Then free all the stuff we've allocated or the SH API gave us
                Marshal.FreeHGlobal(buffer);
            }
            finally {
                IMalloc malloc = GetSHMalloc();
                malloc.Free(pidlRoot[0]);

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

            return(DialogResult.OK);
        }
        public System.Drawing.Bitmap GetThumbNail(
            string file
            )
        {
            if ((!File.Exists(file)) && (!Directory.Exists(file)))
            {
                throw new FileNotFoundException(
                          String.Format("The file '{0}' does not exist", file),
                          file);
            }

            if (thumbNail != null)
            {
                thumbNail.Dispose();
                thumbNail = null;
            }

            IShellFolder folder = null;

            try
            {
                folder = getDesktopFolder;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (folder != null)
            {
                IntPtr pidlMain = IntPtr.Zero;
                try
                {
                    int    cParsed   = 0;
                    int    pdwAttrib = 0;
                    string filePath  = Path.GetDirectoryName(file);
                    pidlMain = IntPtr.Zero;
                    folder.ParseDisplayName(
                        IntPtr.Zero,
                        IntPtr.Zero,
                        filePath,
                        out cParsed,
                        out pidlMain,
                        out pdwAttrib);
                }
                catch (Exception ex)
                {
                    DirectShowUtil.ReleaseComObject(folder);
                    throw ex;
                }

                if (pidlMain != IntPtr.Zero)
                {
                    // IShellFolder:
                    Guid         iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
                    IShellFolder item           = null;

                    try
                    {
                        folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
                    }
                    catch (Exception ex)
                    {
                        DirectShowUtil.ReleaseComObject(folder);
                        Allocator.Free(pidlMain);
                        throw ex;
                    }

                    if (item != null)
                    {
                        //
                        IEnumIDList idEnum = null;
                        try
                        {
                            item.EnumObjects(
                                IntPtr.Zero,
                                (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS),
                                ref idEnum);
                        }
                        catch (Exception ex)
                        {
                            DirectShowUtil.ReleaseComObject(folder);
                            Allocator.Free(pidlMain);
                            throw ex;
                        }

                        if (idEnum != null)
                        {
                            // start reading the enum:
                            int    hRes     = 0;
                            IntPtr pidl     = IntPtr.Zero;
                            int    fetched  = 0;
                            bool   complete = false;
                            while (!complete)
                            {
                                hRes = idEnum.Next(1, ref pidl, out fetched);
                                if (hRes != 0)
                                {
                                    pidl     = IntPtr.Zero;
                                    complete = true;
                                }
                                else
                                {
                                    if (getThumbNail(file, pidl, item))
                                    {
                                        complete = true;
                                    }
                                }
                                if (pidl != IntPtr.Zero)
                                {
                                    Allocator.Free(pidl);
                                }
                            }

                            DirectShowUtil.ReleaseComObject(idEnum);
                        }


                        DirectShowUtil.ReleaseComObject(item);
                    }

                    Allocator.Free(pidlMain);
                }

                DirectShowUtil.ReleaseComObject(folder);
            }
            return(thumbNail);
        }
Ejemplo n.º 14
0
        public Bitmap GetThumbnail(string fileName)
        {
            if (!File.Exists(fileName) && !Directory.Exists(fileName))
            {
                throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName);
            }

            if (ThumbNail != null)
            {
                ThumbNail.Dispose();
                ThumbNail = null;
            }

            var folder = getDesktopFolder;

            if (folder == null)
            {
                return(ThumbNail);
            }

            var pidlMain = IntPtr.Zero;

            try
            {
                var cParsed   = 0;
                var pdwAttrib = 0;
                var filePath  = Path.GetDirectoryName(fileName);
                folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref cParsed, ref pidlMain, ref pdwAttrib);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, null, null);
                throw;
            }

            if (pidlMain == IntPtr.Zero)
            {
                freeResources(pidlMain, folder, null, null);
                return(null);
            }


            var          iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
            IShellFolder item           = null;

            try
            {
                folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, null, item);
                throw;
            }

            if (item == null)
            {
                freeResources(pidlMain, folder, null, null);
                return(null);
            }

            IEnumIDList idEnum = null;

            try
            {
                item.EnumObjects(IntPtr.Zero, (Eshcontf.ShcontfFolders | Eshcontf.ShcontfNonfolders), ref idEnum);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, idEnum, item);
                throw;
            }

            if (idEnum == null)
            {
                freeResources(pidlMain, folder, null, item);
                return(null);
            }

            var pidl     = IntPtr.Zero;
            var fetched  = 0;
            var complete = false;

            while (!complete)
            {
                var hRes = idEnum.Next(1, ref pidl, ref fetched);
                if (hRes != 0)
                {
                    pidl     = IntPtr.Zero;
                    complete = true;
                }
                else
                {
                    if (getThumbNail(fileName, pidl, item))
                    {
                        complete = true;
                    }
                }

                if (pidl != IntPtr.Zero)
                {
                    allocator.Free(pidl);
                }
            }

            freeResources(pidlMain, folder, idEnum, item);
            return(ThumbNail);
        }