Example #1
0
        void CreateShellView()
        {
            IShellView     previous       = m_ComInterface;
            Rectangle      bounds         = ClientRectangle;
            FOLDERSETTINGS folderSettings = new FOLDERSETTINGS();

            // Create an IShellView object.
            m_ComInterface = CreateViewObject(m_CurrentFolder, Handle);

            // Set the FOLDERSETTINGS.
            folderSettings.ViewMode = (FOLDERVIEWMODE)m_View;

            if (!m_ShowWebView)
            {
                folderSettings.fFlags |= FOLDERFLAGS.NOWEBVIEW;
            }

            if (!m_MultiSelect)
            {
                folderSettings.fFlags |= FOLDERFLAGS.SINGLESEL;
            }

            // Tell the IShellView object to create a view window and
            // activate it.
            try
            {
                m_ComInterface.CreateViewWindow(previous, ref folderSettings,
                                                GetShellBrowser(), ref bounds,
                                                out m_ShellViewWindow);
            }
            catch (COMException ex)
            {
                // If the operation was cancelled by the user (for example
                // because an empty removable media drive was selected,
                // then "Cancel" pressed in the resulting dialog) convert
                // the exception into something more meaningfil.
                if (ex.ErrorCode == unchecked ((int)0x800704C7U))
                {
                    throw new UserAbortException(ex);
                }
            }

            m_ComInterface.UIActivate(1);

            // Disable the window if in design mode, so that user input is
            // passed onto the designer.
            if (DesignMode)
            {
                User32.EnableWindow(m_ShellViewWindow, false);
            }

            // Destroy the previous view window.
            if (previous != null)
            {
                previous.DestroyViewWindow();
            }
        }
        int IShellBrowser.BrowseObject(IntPtr pidl, SBSP wFlags)
        {
            if (this.InvokeRequired)
            {
                AutoResetEvent theEvent = new AutoResetEvent(false);
                int            result   = WinError.E_FAIL;
                this.Invoke((Action)(() =>
                {
                    result = ((IShellBrowser)this).BrowseObject(pidl, wFlags);
                    theEvent.Set();
                }));
                theEvent.WaitOne();
                return(result);
            }

            int          hr;
            IntPtr       folderTmpPtr;
            IShellFolder folderTmp;
            IntPtr       pidlTmp;

            //  We'll need the shell folder GUID.
            var shellFolderGuid = typeof(IShellFolder).GUID;
            var shellViewGuid   = typeof(IShellView).GUID;

            //  Check to see if we have a desktop pidl, relative pidl or absolite pidl.
            if (Shell32.ILIsEqual(pidl, desktopFolderPidl))
            {
                //  The provided PIDL is the desktop folder.
                pidlTmp   = Shell32.ILClone(desktopFolderPidl);
                folderTmp = desktopFolder;
            }
            else if ((wFlags & SBSP.SBSP_RELATIVE) != 0)
            {
                // SBSP_RELATIVE - pidl is relative from the current folder
                if ((hr = currentFolder.BindToObject(pidl, IntPtr.Zero,
                                                     ref shellFolderGuid,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(hr);
                }
                pidlTmp   = Shell32.ILCombine(currentAbsolutePidl, pidl);
                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }
            else
            {
                // SBSP_ABSOLUTE - pidl is an absolute pidl (relative from desktop)
                pidlTmp = Shell32.ILClone(pidl);
                if ((hr = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero,
                                                     ref shellFolderGuid,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(hr);
                }
                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }

            if (folderTmp == null)
            {
                Shell32.ILFree(pidlTmp);
                return(WinError.E_FAIL);
            }

            // Check that we have a new pidl
            if (Shell32.ILIsEqual(pidlTmp, currentAbsolutePidl))
            {
                Shell32.ILFree(pidlTmp);
                return(WinError.S_OK);
            }

            currentFolder = folderTmp;

            FOLDERSETTINGS fs             = new FOLDERSETTINGS();
            IShellView     lastIShellView = shellView;

            if (lastIShellView != null)
            {
                lastIShellView.GetCurrentInfo(ref fs);
            }
            // Copy the old folder settings
            else
            {
                fs          = new FOLDERSETTINGS();
                fs.fFlags   = folderFlags;
                fs.ViewMode = folderViewMode;
            }

            // Create the IShellView
            IntPtr iShellViewPtr;

            hr = folderTmp.CreateViewObject(Handle,
                                            ref shellViewGuid, out iShellViewPtr);
            if (hr == WinError.S_OK)
            {
                shellView = (IShellView)
                            Marshal.GetObjectForIUnknown(iShellViewPtr);

                hWndListView = IntPtr.Zero;
                RECT rc =
                    new RECT(0, 0,
                             ClientSize.Width,
                             ClientSize.Height);

                int res;

                try
                {
                    // Create the actual list view.
                    res = shellView.CreateViewWindow(lastIShellView, ref fs,
                                                     this, ref rc, ref hWndListView);
                }
                catch (COMException)
                {
                    return(WinError.E_FAIL);
                }

                if (res < 0)
                {
                    return(WinError.E_FAIL);
                }

                // Release the old IShellView
                if (lastIShellView != null)
                {
                    lastIShellView.GetCurrentInfo(ref fs);
                    lastIShellView.UIActivate(SVUIA_STATUS.SVUIA_DEACTIVATE);
                    lastIShellView.DestroyViewWindow();
                }

                // Set focus to the IShellView
                shellView.UIActivate(SVUIA_STATUS.SVUIA_ACTIVATE_FOCUS);
                currentAbsolutePidl = pidlTmp;
            }

            return(WinError.S_OK);
        }
        static int ChangeFolder(ShellDebuggerModel model, IntPtr pidl,
                                SBSP wFlags, IShellFolder folderTmp, IntPtr pidlTmp)
        {
            model.currentFolder = folderTmp;
            var Form = model.Form;

            FOLDERSETTINGS fs              = new FOLDERSETTINGS();
            IShellView     lastIShellView  = model.ShellView;
            IShellView2    lastIShellView2 = model.ShellView as IShellView2;

            if (lastIShellView != null)
            {
                lastIShellView.GetCurrentInfo(ref fs);
            }
            // Copy the old folder settings
            else
            {
                fs          = new FOLDERSETTINGS();
                fs.fFlags   = ShellDebuggerModel.folderFlags;
                fs.ViewMode = ShellDebuggerModel.folderViewMode;
            }

            // Create the IShellView
            IntPtr iShellViewPtr;
            var    shellViewGuid = typeof(IShellView).GUID;
            var    hr            = ShellObject.CreateViewObject(folderTmp, Form.Handle,
                                                                ref shellViewGuid, out iShellViewPtr);

            if (hr != WinError.S_OK)
            {
                shellViewGuid = typeof(IShellView).GUID;
                hr            = ShellObject.CreateViewObject(folderTmp, Form.Handle,
                                                             ref shellViewGuid, out iShellViewPtr);
            }

            if (hr == WinError.S_OK)
            {
                model.ShellView = null;
                model.ShellView = (IShellView) // IShellView2
                                  Marshal.GetObjectForIUnknown(iShellViewPtr);
                //if (model.ShellView == null)
                //    model.ShellView = (IShellView)
                //               Marshal.GetObjectForIUnknown(iShellViewPtr);
                // int CreateViewWindow2(SV2CVW2_PARAMS lpParams);

                var  hWndListView = IntPtr.Zero;
                RECT rc           =
                    new RECT(0, 0,
                             Form.ClientSize.Width,
                             Form.ClientSize.Height);

                int res;
                model.lastViewPidl = IntPtr.Zero;
                var shellView = model.ShellView;

                try
                {
                    // Create the actual list view.
                    res = shellView.CreateViewWindow(lastIShellView, ref fs,
                                                     model, ref rc, ref hWndListView);

                    model.hWndListView = hWndListView;
                    shellView.EnableModeless(true);
                }
                catch (COMException)
                {
                    return(WinError.E_FAIL);
                }

                if (res < 0)
                {
                    return(WinError.E_FAIL);
                }

                // Release the old IShellView
                if (lastIShellView != null)
                {
                    lastIShellView.GetCurrentInfo(ref fs);
                    lastIShellView.UIActivate(SVUIA_STATUS.SVUIA_DEACTIVATE);
                    lastIShellView.DestroyViewWindow();
                }

                // Set focus to the IShellView
                model.ShellView.UIActivate(SVUIA_STATUS.SVUIA_ACTIVATE_FOCUS);
                model.currentAbsolutePidl = pidlTmp;

                if (model.lastViewPidl != IntPtr.Zero)
                {
                    //    var lastItem = model.LastSelected;
                }
                //else
                //{
                //    // empty list
                //}
            }

            return(WinError.S_OK);
        }