#if PREVIEW_HANDLER_WINFORM using ShellExtensions.Interop; using ShellExtensions.Interop.Common; using ShellExtensions.Resources; using System; using System.Drawing; using System.Windows.Forms; namespace ShellExtensions { /// /// This is the base class for all WinForms-based preview handlers and provides their basic functionality. /// To create a custom preview handler that contains a WinForms user control, /// a class must derive from this, use the , /// and implement 1 or more of the following interfaces: /// , /// , /// . /// public abstract class WinFormsPreviewHandler : PreviewHandler, IDisposable { /// /// This control must be populated by the deriving class before the preview is shown. /// public UserControl Control { get; protected set; } protected void ThrowIfNoControl() { if (Control == null) { throw new InvalidOperationException(LocalizedMessages.PreviewHandlerControlNotInitialized); } } /// /// Called when an exception is thrown during itialization of the preview control. /// /// protected override void HandleInitializeException(Exception caughtException) { if (caughtException == null) { throw new ArgumentNullException(nameof(caughtException)); } Control = new UserControl(); Control.Controls.Add(new TextBox { ReadOnly = true, Multiline = true, Dock = DockStyle.Fill, Text = caughtException.ToString(), BackColor = Color.OrangeRed }); } protected override void UpdateBounds(NativeRect bounds) { Control.Bounds = Rectangle.FromLTRB(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom); Control.Visible = true; } protected override void SetFocus() { Control.Focus(); } protected override void SetBackground(int argb) { Control.BackColor = Color.FromArgb(argb); } protected override void SetForeground(int argb) { Control.ForeColor = Color.FromArgb(argb); } protected override void SetFont(Interop.LogFont font) { Control.Font = Font.FromLogFont(font); } protected override IntPtr Handle { get { { return Control.Handle; } } } protected override void SetParentHandle(IntPtr handle) { HandlerNativeMethods.SetParent(Control.Handle, handle); } #region IDisposable Members ~WinFormsPreviewHandler() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing && Control != null) { Control.Dispose(); } } #endregion } } #endif