// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.ComponentModel; using System.Drawing; using Common.ComInterlop; namespace Common { /// /// Preview Handler base class implementing interfaces required by Preview Handler. /// public abstract class PreviewHandlerBase : IPreviewHandler, IOleWindow, IObjectWithSite, IPreviewHandlerVisuals { /// /// An instance of Preview Control Used by the Handler. /// private IPreviewHandlerControl previewControl; /// /// Hold reference for the window handle. /// private IntPtr parentHwnd; /// /// Hold the bounds of the window. /// private Rectangle windowBounds; /// /// Holds the site pointer. /// private object unkSite; /// /// Holds reference for the IPreviewHandlerFrame. /// private IPreviewHandlerFrame frame; /// /// Initializes a new instance of the class. /// public PreviewHandlerBase() { } /// /// Initializes a new instance of the class. /// public void Initialize() { previewControl = CreatePreviewHandlerControl(); } /// public abstract void DoPreview(); /// public void SetWindow(IntPtr hwnd, ref RECT rect) { this.parentHwnd = hwnd; this.windowBounds = rect.ToRectangle(); this.previewControl.SetWindow(hwnd, this.windowBounds); } /// public void SetRect(ref RECT rect) { this.windowBounds = rect.ToRectangle(); this.previewControl.SetRect(this.windowBounds); } /// public void Unload() { this.previewControl.Unload(); } /// public void SetFocus() { this.previewControl.SetFocus(); } /// public void QueryFocus(out IntPtr phwnd) { this.previewControl.QueryFocus(out IntPtr result); phwnd = result; if (phwnd == IntPtr.Zero) { throw new Win32Exception(); } } /// public uint TranslateAccelerator(ref MSG pmsg) { // Current implementation simply directs all Keystrokes to IPreviewHandlerFrame. This is the recommended approach to handle keystokes for all low-integrity preview handlers. // Source: https://learn.microsoft.com/windows/win32/shell/building-preview-handlers#ipreviewhandlertranslateaccelerator if (this.frame != null) { return this.frame.TranslateAccelerator(ref pmsg); } const uint S_FALSE = 1; return S_FALSE; } /// public void GetWindow(out IntPtr phwnd) { phwnd = this.previewControl.GetWindowHandle(); } /// public void ContextSensitiveHelp(bool fEnterMode) { // Should always return NotImplementedException. Source: https://learn.microsoft.com/windows/win32/shell/building-preview-handlers#iolewindowcontextsensitivehelp throw new NotImplementedException(); } /// public void SetSite(object pUnkSite) { // Implementation logic details: https://learn.microsoft.com/windows/win32/shell/building-preview-handlers#iobjectwithsitesetsite this.unkSite = pUnkSite; this.frame = this.unkSite as IPreviewHandlerFrame; } /// public void GetSite(ref Guid riid, out object ppvSite) { ppvSite = this.unkSite; } /// public void SetBackgroundColor(COLORREF color) { this.previewControl.SetBackgroundColor(color.Color); } /// public void SetFont(ref LOGFONT plf) { this.previewControl.SetFont(Font.FromLogFont(plf)); } /// public void SetTextColor(COLORREF color) { this.previewControl.SetTextColor(color.Color); } /// /// Provide instance of the implementation of . Should be overridden by the implementation class with a control object to be used. /// /// Instance of the . protected abstract IPreviewHandlerControl CreatePreviewHandlerControl(); } }