Ejemplo n.º 1
0
        /// <summary>
        /// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool Open(string filename)
        {
            UnloadPreviewHandler();

            if (String.IsNullOrEmpty(filename))
            {
                ErrorMessage = "No file loaded.";
                return(false);
            }

            // try to get GUID for the preview handler
            Guid guid = GetPreviewHandlerGUID(filename);

            ErrorMessage = "";

            if (guid != Guid.Empty)
            {
                try {
                    if (guid != mCurrentPreviewHandlerGUID)
                    {
                        mCurrentPreviewHandlerGUID = guid;

                        // need to instantiate a different COM type (file format has changed)
                        if (mCurrentPreviewHandler != null)
                        {
                            Marshal.FinalReleaseComObject(mCurrentPreviewHandler);
                        }

                        // use reflection to instantiate the preview handler type
                        Type comType = Type.GetTypeFromCLSID(mCurrentPreviewHandlerGUID);
                        mCurrentPreviewHandler = Activator.CreateInstance(comType);
                    }

                    if (mCurrentPreviewHandler is IInitializeWithFile)
                    {
                        // some handlers accept a filename
                        ((IInitializeWithFile)mCurrentPreviewHandler).Initialize(filename, 0);
                    }
                    else if (mCurrentPreviewHandler is IInitializeWithStream)
                    {
                        if (File.Exists(filename))
                        {
                            // other handlers want an IStream (in this case, a file stream)
                            mCurrentPreviewHandlerStream = File.Open(filename, FileMode.Open);
                            StreamWrapper stream = new StreamWrapper(mCurrentPreviewHandlerStream);
                            ((IInitializeWithStream)mCurrentPreviewHandler).Initialize(stream, 0);
                        }
                        else
                        {
                            ErrorMessage = "File not found.";
                        }
                    }
                    else if (mCurrentPreviewHandler is IInitializeWithItem)
                    {
                        // a third category exists, must be initialised with a shell item
                        IShellItem shellItem;
                        SHCreateItemFromParsingName(filename, IntPtr.Zero, new Guid(GUID_ISHELLITEM), out shellItem);
                        ((IInitializeWithItem)mCurrentPreviewHandler).Initialize(shellItem, 0);
                    }

                    if (mCurrentPreviewHandler is IPreviewHandler)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle r = ClientRectangle;
                        ((IPreviewHandler)mCurrentPreviewHandler).SetWindow(Handle, ref r);
                        ((IPreviewHandler)mCurrentPreviewHandler).DoPreview();

                        return(true);
                    }
                }
                catch (Exception ex) {
                    ErrorMessage = "Preview could not be generated.\n" + ex.Message;
                }
            }
            else
            {
                ErrorMessage = "No preview available.";
            }

            return(false);
        }