Zetbox.Client.WPF.View.DocumentManagement.PreviewersManager.AttachPreview C# (CSharp) Method

AttachPreview() public method

public AttachPreview ( IntPtr handler, string fileName, Rect viewRect ) : void
handler System.IntPtr
fileName string
viewRect System.Windows.Rect
return void
        public void AttachPreview(IntPtr handler, string fileName, Rect viewRect)
        {
            Release();

            try
            {
                string CLSID = "8895b1c6-b41f-4c1c-a562-0d564250836f";
                Guid g = new Guid(CLSID);
                string[] exts = fileName.Split('.');
                string ext = exts[exts.Length - 1];
                var regKey = string.Format(@".{0}\ShellEx\{1:B}", ext, g);
                using (RegistryKey hk = Registry.ClassesRoot.OpenSubKey(regKey))
                {
                    if (hk == null)
                    {
                        Logging.Log.WarnOnce("Unable to initialize IPreviewHandler - Registry Key not found: " + regKey);
                        return;
                    }
                    var objValue = hk.GetValue("");
                    if (objValue == null)
                    {
                        Logging.Log.WarnOnce("Unable to initialize IPreviewHandler - no handler registrated in Registry");
                        return;
                    }
                    if (!Guid.TryParse(objValue.ToString(), out g))
                    {
                        Logging.Log.WarnOnce("Unable to initialize IPreviewHandler - cannot parse guid from registry: " + objValue);
                        return;
                    }

                    Type type = Type.GetTypeFromCLSID(g, false);
                    if (type == null)
                    {
                        Logging.Log.WarnOnce(string.Format("Unable to initialize IPreviewHandler - could not load COM Object, Type.GetTypeFromCLSID({0}) returend false", g));
                        return;
                    }
                    object comObj = Activator.CreateInstance(type);

                    IInitializeWithFile fileInit = comObj as IInitializeWithFile;
                    IInitializeWithStream streamInit = comObj as IInitializeWithStream;
                    IInitializeWithItem itemInit = comObj as IInitializeWithItem;

                    bool isInitialized = false;
                    if (fileInit != null)
                    {
                        fileInit.Initialize(fileName, 0); // 0 = STGM_READ
                        isInitialized = true;
                    }
                    else if (streamInit != null)
                    {
                        stream = new COMStream(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));
                        streamInit.Initialize((IStream)stream, 0); // 0 = STGM_READ
                        isInitialized = true;
                    }
                    else if (itemInit != null)
                    {
                        IShellItem shellItem;
                        SHCreateItemFromParsingName(fileName, IntPtr.Zero, typeof(IShellItem).GUID, out shellItem);
                        itemInit.Initialize(shellItem, 0); // 0 = STGM_READ
                        isInitialized = true;
                    }

                    pHandler = comObj as IPreviewHandler;
                    if (isInitialized && pHandler != null)
                    {
                        RECT r = new RECT(viewRect);

                        pHandler.SetWindow(handler, ref r);
                        pHandler.SetRect(ref r);

                        pHandler.DoPreview();
                    }
                    else
                    {
                        Release();
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Log.Warn("Unable to initialize IPreviewHandler", ex);
                Release();
            }
        }

Usage Example

Example #1
0
 private void AttachPreview()
 {
     EnsurePreviewManager();
     if (host != null && host.Handle != IntPtr.Zero && !string.IsNullOrEmpty(PreviewFilePath))
     {
         pManager.AttachPreview(host.Handle, PreviewFilePath, actualRect);
     }
 }