correctin file ending (#5970)

This commit is contained in:
Clint Rutkas
2020-08-14 15:10:06 -07:00
committed by GitHub
parent 2ce16bcdb7
commit be36b4aac1
17 changed files with 2734 additions and 2734 deletions

View File

@@ -1,45 +1,45 @@
// 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.Drawing;
using System.Runtime.InteropServices;
namespace Common.ComInterlop
{
/// <summary>
/// The RECT structure defines a rectangle by the coordinates of its upper-left and lower-right corners.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
/// <summary>
/// Specifies the x-coordinate of the upper-left corner of the rectangle.
/// </summary>
public int Left;
/// <summary>
/// Specifies the y-coordinate of the upper-left corner of the rectangle.
/// </summary>
public int Top;
/// <summary>
/// Specifies the x-coordinate of the lower-right corner of the rectangle.
/// </summary>
public int Right;
/// <summary>
/// Specifies the y-coordinate of the lower-right corner of the rectangle.
/// </summary>
public int Bottom;
/// <summary>
/// Creates a <see cref="Rectangle" /> structure with the edge locations specified in the struct.
/// </summary>
/// <returns>Return a <see cref="Rectangle"/>.</returns>
public Rectangle ToRectangle()
{
return Rectangle.FromLTRB(this.Left, this.Top, this.Right, this.Bottom);
}
}
}
// 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.Drawing;
using System.Runtime.InteropServices;
namespace Common.ComInterlop
{
/// <summary>
/// The RECT structure defines a rectangle by the coordinates of its upper-left and lower-right corners.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
/// <summary>
/// Specifies the x-coordinate of the upper-left corner of the rectangle.
/// </summary>
public int Left;
/// <summary>
/// Specifies the y-coordinate of the upper-left corner of the rectangle.
/// </summary>
public int Top;
/// <summary>
/// Specifies the x-coordinate of the lower-right corner of the rectangle.
/// </summary>
public int Right;
/// <summary>
/// Specifies the y-coordinate of the lower-right corner of the rectangle.
/// </summary>
public int Bottom;
/// <summary>
/// Creates a <see cref="Rectangle" /> structure with the edge locations specified in the struct.
/// </summary>
/// <returns>Return a <see cref="Rectangle"/>.</returns>
public Rectangle ToRectangle()
{
return Rectangle.FromLTRB(this.Left, this.Top, this.Right, this.Bottom);
}
}
}

View File

@@ -1,185 +1,185 @@
// 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.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// Form based implementation of <see cref="IPreviewHandlerControl"/>.
/// </summary>
public abstract class FormHandlerControl : Form, IPreviewHandlerControl
{
/// <summary>
/// Needed to make the form a child window.
/// </summary>
private static int gwlStyle = -16;
private static int wsChild = 0x40000000;
/// <summary>
/// Holds the parent window handle.
/// </summary>
private IntPtr parentHwnd;
/// <summary>
/// Initializes a new instance of the <see cref="FormHandlerControl"/> class.
/// </summary>
public FormHandlerControl()
{
// Gets the handle of the control to create the control on the VI thread. Invoking the Control.Handle get accessor forces the creation of the underlying window for the control.
// This is important, because the thread that instantiates the preview handler component and calls its constructor is a single-threaded apartment (STA) thread, but the thread that calls into the interface members later on is a multithreaded apartment (MTA) thread. Windows Forms controls are meant to run on STA threads.
// More details: https://docs.microsoft.com/en-us/archive/msdn-magazine/2007/january/windows-vista-and-office-writing-your-own-preview-handlers.
var forceCreation = this.Handle;
this.FormBorderStyle = FormBorderStyle.None;
this.Visible = false;
}
/// <inheritdoc />
public IntPtr GetHandle()
{
return this.Handle;
}
/// <inheritdoc />
public void QueryFocus(out IntPtr result)
{
var getResult = IntPtr.Zero;
this.InvokeOnControlThread(() =>
{
getResult = GetFocus();
});
result = getResult;
}
/// <inheritdoc />
public void SetBackgroundColor(Color argbColor)
{
this.InvokeOnControlThread(() =>
{
this.BackColor = argbColor;
});
}
/// <inheritdoc />
public void SetFocus()
{
this.InvokeOnControlThread(() =>
{
this.Focus();
});
}
/// <inheritdoc />
public void SetFont(Font font)
{
this.InvokeOnControlThread(() =>
{
this.Font = font;
});
}
/// <inheritdoc />
public void SetRect(Rectangle windowBounds)
{
this.UpdateWindowBounds(windowBounds);
}
/// <inheritdoc />
public void SetTextColor(Color color)
{
this.InvokeOnControlThread(() =>
{
this.ForeColor = color;
});
}
/// <inheritdoc />
public void SetWindow(IntPtr hwnd, Rectangle rect)
{
this.parentHwnd = hwnd;
this.UpdateWindowBounds(rect);
}
/// <inheritdoc />
public virtual void Unload()
{
this.InvokeOnControlThread(() =>
{
this.Visible = false;
foreach (Control c in this.Controls)
{
c.Dispose();
}
this.Controls.Clear();
});
// Call garbage collection at the time of unloading of Preview. This is to mitigate issue with WebBrowser Control not able to dispose properly.
// Which is preventing prevhost.exe to exit at the time of closing File explorer.
// Preview Handlers run in a separate process from PowerToys. This will not affect the performance of other modules.
// Mitigate the following Github issue: https://github.com/microsoft/PowerToys/issues/1468
GC.Collect();
}
/// <inheritdoc />
public virtual void DoPreview<T>(T dataSource)
{
this.Visible = true;
}
/// <summary>
/// Executes the specified delegate on the thread that owns the control's underlying window handle.
/// </summary>
/// <param name="func">Delegate to run.</param>
public void InvokeOnControlThread(MethodInvoker func)
{
this.Invoke(func);
}
/// <summary>
/// Changes the parent window of the specified child window.
/// </summary>
/// <param name="hWndChild">A handle to the child window.</param>
/// <param name="hWndNewParent">A handle to the new parent window.</param>
/// <returns>If the function succeeds, the return value is a handle to the previous parent window and NULL in case of failure.</returns>
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
/// <summary>
/// Retrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.
/// </summary>
/// <returns>The return value is the handle to the window with the keyboard focus. If the calling thread's message queue does not have an associated window with the keyboard focus, the return value is NULL.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetFocus();
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
/// <summary>
/// Update the Form Control window with the passed rectangle.
/// </summary>
/// <param name="windowBounds">An instance of rectangle.</param>
private void UpdateWindowBounds(Rectangle windowBounds)
{
this.InvokeOnControlThread(() =>
{
// We must set the WS_CHILD style to change the form to a control within the Explorer preview pane
int windowStyle = GetWindowLong(this.Handle, gwlStyle);
if ((windowStyle & wsChild) == 0)
{
SetWindowLong(this.Handle, gwlStyle, windowStyle | wsChild);
}
SetParent(this.Handle, this.parentHwnd);
this.Bounds = windowBounds;
});
}
}
}
// 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.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// Form based implementation of <see cref="IPreviewHandlerControl"/>.
/// </summary>
public abstract class FormHandlerControl : Form, IPreviewHandlerControl
{
/// <summary>
/// Needed to make the form a child window.
/// </summary>
private static int gwlStyle = -16;
private static int wsChild = 0x40000000;
/// <summary>
/// Holds the parent window handle.
/// </summary>
private IntPtr parentHwnd;
/// <summary>
/// Initializes a new instance of the <see cref="FormHandlerControl"/> class.
/// </summary>
public FormHandlerControl()
{
// Gets the handle of the control to create the control on the VI thread. Invoking the Control.Handle get accessor forces the creation of the underlying window for the control.
// This is important, because the thread that instantiates the preview handler component and calls its constructor is a single-threaded apartment (STA) thread, but the thread that calls into the interface members later on is a multithreaded apartment (MTA) thread. Windows Forms controls are meant to run on STA threads.
// More details: https://docs.microsoft.com/en-us/archive/msdn-magazine/2007/january/windows-vista-and-office-writing-your-own-preview-handlers.
var forceCreation = this.Handle;
this.FormBorderStyle = FormBorderStyle.None;
this.Visible = false;
}
/// <inheritdoc />
public IntPtr GetHandle()
{
return this.Handle;
}
/// <inheritdoc />
public void QueryFocus(out IntPtr result)
{
var getResult = IntPtr.Zero;
this.InvokeOnControlThread(() =>
{
getResult = GetFocus();
});
result = getResult;
}
/// <inheritdoc />
public void SetBackgroundColor(Color argbColor)
{
this.InvokeOnControlThread(() =>
{
this.BackColor = argbColor;
});
}
/// <inheritdoc />
public void SetFocus()
{
this.InvokeOnControlThread(() =>
{
this.Focus();
});
}
/// <inheritdoc />
public void SetFont(Font font)
{
this.InvokeOnControlThread(() =>
{
this.Font = font;
});
}
/// <inheritdoc />
public void SetRect(Rectangle windowBounds)
{
this.UpdateWindowBounds(windowBounds);
}
/// <inheritdoc />
public void SetTextColor(Color color)
{
this.InvokeOnControlThread(() =>
{
this.ForeColor = color;
});
}
/// <inheritdoc />
public void SetWindow(IntPtr hwnd, Rectangle rect)
{
this.parentHwnd = hwnd;
this.UpdateWindowBounds(rect);
}
/// <inheritdoc />
public virtual void Unload()
{
this.InvokeOnControlThread(() =>
{
this.Visible = false;
foreach (Control c in this.Controls)
{
c.Dispose();
}
this.Controls.Clear();
});
// Call garbage collection at the time of unloading of Preview. This is to mitigate issue with WebBrowser Control not able to dispose properly.
// Which is preventing prevhost.exe to exit at the time of closing File explorer.
// Preview Handlers run in a separate process from PowerToys. This will not affect the performance of other modules.
// Mitigate the following Github issue: https://github.com/microsoft/PowerToys/issues/1468
GC.Collect();
}
/// <inheritdoc />
public virtual void DoPreview<T>(T dataSource)
{
this.Visible = true;
}
/// <summary>
/// Executes the specified delegate on the thread that owns the control's underlying window handle.
/// </summary>
/// <param name="func">Delegate to run.</param>
public void InvokeOnControlThread(MethodInvoker func)
{
this.Invoke(func);
}
/// <summary>
/// Changes the parent window of the specified child window.
/// </summary>
/// <param name="hWndChild">A handle to the child window.</param>
/// <param name="hWndNewParent">A handle to the new parent window.</param>
/// <returns>If the function succeeds, the return value is a handle to the previous parent window and NULL in case of failure.</returns>
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
/// <summary>
/// Retrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.
/// </summary>
/// <returns>The return value is the handle to the window with the keyboard focus. If the calling thread's message queue does not have an associated window with the keyboard focus, the return value is NULL.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetFocus();
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
/// <summary>
/// Update the Form Control window with the passed rectangle.
/// </summary>
/// <param name="windowBounds">An instance of rectangle.</param>
private void UpdateWindowBounds(Rectangle windowBounds)
{
this.InvokeOnControlThread(() =>
{
// We must set the WS_CHILD style to change the form to a control within the Explorer preview pane
int windowStyle = GetWindowLong(this.Handle, gwlStyle);
if ((windowStyle & wsChild) == 0)
{
SetWindowLong(this.Handle, gwlStyle, windowStyle | wsChild);
}
SetParent(this.Handle, this.parentHwnd);
this.Bounds = windowBounds;
});
}
}
}

View File

@@ -1,77 +1,77 @@
// 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.Drawing;
namespace Common
{
/// <summary>
/// Interface defining methods requirement by the <see cref="PreviewHandlerBase"/> control.
/// </summary>
public interface IPreviewHandlerControl
{
/// <summary>
/// Directs the preview handler to return the HWND from calling the GetFocus function.
/// Source: https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ipreviewhandler-queryfocus.
/// </summary>
/// <param name="result">Returns the handle of the window with focus.</param>
void QueryFocus(out IntPtr result);
/// <summary>
/// Sets focus to the control.
/// </summary>
void SetFocus();
/// <summary>
/// Sets the font according to the font set in Windows Settings.
/// More details: https://docs.microsoft.com/en-us/windows/win32/shell/building-preview-handlers#ipreviewhandlervisualssetfont.
/// </summary>
/// <param name="font">Instance of Font.</param>
void SetFont(Font font);
/// <summary>
/// Sets the Text color according to the Windows Settings.
/// </summary>
/// <param name="color">Instance of color.</param>
void SetTextColor(Color color);
/// <summary>
/// Sets the Background color. For instance to fill the window when the handler renders to area smaller provided by SetWindow and SetRect.
/// </summary>
/// <param name="argbColor">Instance of color.</param>
void SetBackgroundColor(Color argbColor);
/// <summary>
/// Gets the HWND of the control window.
/// </summary>
/// <returns>Pointer to the window handle.</returns>
IntPtr GetHandle();
/// <summary>
/// Hide the preview and free any resource used for the preview.
/// </summary>
void Unload();
/// <summary>
/// Directs the control to change the area within the parent hwnd that it draws into.
/// </summary>
/// <param name="windowBounds">Instance of Rectangle defining the area.</param>
void SetRect(Rectangle windowBounds);
/// <summary>
/// Sets the parent window of the previewer window, as well as the area within the parent to be used for the previewer window..
/// </summary>
/// <param name="hwnd">Pointer to the parent window handle.</param>
/// <param name="rect">Instance of Rectangle defining the area.</param>
void SetWindow(IntPtr hwnd, Rectangle rect);
/// <summary>
/// Called by Preview Handler to start the preview.
/// </summary>
/// <typeparam name="T">File Path or Stream reference for the file.</typeparam>
/// <param name="dataSource">Represents the source of preview data.</param>
void DoPreview<T>(T dataSource);
}
}
// 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.Drawing;
namespace Common
{
/// <summary>
/// Interface defining methods requirement by the <see cref="PreviewHandlerBase"/> control.
/// </summary>
public interface IPreviewHandlerControl
{
/// <summary>
/// Directs the preview handler to return the HWND from calling the GetFocus function.
/// Source: https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ipreviewhandler-queryfocus.
/// </summary>
/// <param name="result">Returns the handle of the window with focus.</param>
void QueryFocus(out IntPtr result);
/// <summary>
/// Sets focus to the control.
/// </summary>
void SetFocus();
/// <summary>
/// Sets the font according to the font set in Windows Settings.
/// More details: https://docs.microsoft.com/en-us/windows/win32/shell/building-preview-handlers#ipreviewhandlervisualssetfont.
/// </summary>
/// <param name="font">Instance of Font.</param>
void SetFont(Font font);
/// <summary>
/// Sets the Text color according to the Windows Settings.
/// </summary>
/// <param name="color">Instance of color.</param>
void SetTextColor(Color color);
/// <summary>
/// Sets the Background color. For instance to fill the window when the handler renders to area smaller provided by SetWindow and SetRect.
/// </summary>
/// <param name="argbColor">Instance of color.</param>
void SetBackgroundColor(Color argbColor);
/// <summary>
/// Gets the HWND of the control window.
/// </summary>
/// <returns>Pointer to the window handle.</returns>
IntPtr GetHandle();
/// <summary>
/// Hide the preview and free any resource used for the preview.
/// </summary>
void Unload();
/// <summary>
/// Directs the control to change the area within the parent hwnd that it draws into.
/// </summary>
/// <param name="windowBounds">Instance of Rectangle defining the area.</param>
void SetRect(Rectangle windowBounds);
/// <summary>
/// Sets the parent window of the previewer window, as well as the area within the parent to be used for the previewer window..
/// </summary>
/// <param name="hwnd">Pointer to the parent window handle.</param>
/// <param name="rect">Instance of Rectangle defining the area.</param>
void SetWindow(IntPtr hwnd, Rectangle rect);
/// <summary>
/// Called by Preview Handler to start the preview.
/// </summary>
/// <typeparam name="T">File Path or Stream reference for the file.</typeparam>
/// <param name="dataSource">Represents the source of preview data.</param>
void DoPreview<T>(T dataSource);
}
}

View File

@@ -1,35 +1,35 @@
// 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.IO;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// This is test custom control to test the implementation.
/// </summary>
public class CustomControlTest : FormHandlerControl
{
/// <summary>
/// Start the preview on the Control.
/// </summary>
/// <param name="dataSource">Path to the file.</param>
public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
{
var filePath = dataSource as string;
WebBrowser browser = new WebBrowser();
browser.DocumentText = "Test";
browser.Navigate(filePath);
browser.Dock = DockStyle.Fill;
browser.IsWebBrowserContextMenuEnabled = false;
this.Controls.Add(browser);
base.DoPreview(dataSource);
});
}
}
}
// 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.IO;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// This is test custom control to test the implementation.
/// </summary>
public class CustomControlTest : FormHandlerControl
{
/// <summary>
/// Start the preview on the Control.
/// </summary>
/// <param name="dataSource">Path to the file.</param>
public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
{
var filePath = dataSource as string;
WebBrowser browser = new WebBrowser();
browser.DocumentText = "Test";
browser.Navigate(filePath);
browser.Dock = DockStyle.Fill;
browser.IsWebBrowserContextMenuEnabled = false;
this.Controls.Add(browser);
base.DoPreview(dataSource);
});
}
}
}

View File

@@ -1,32 +1,32 @@
// 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.Runtime.InteropServices;
namespace Common
{
/// <summary>
/// This is a example custom handler to show how to extend the library.
/// </summary>
[Guid("22a1a8e8-e929-4732-90ce-91eaff38b614")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class TestCustomHandler : FileBasedPreviewHandler
{
private CustomControlTest previewHandlerControl;
/// <inheritdoc />
public override void DoPreview()
{
this.previewHandlerControl.DoPreview(this.FilePath);
}
/// <inheritdoc />
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
{
this.previewHandlerControl = new CustomControlTest();
return this.previewHandlerControl;
}
}
}
// 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.Runtime.InteropServices;
namespace Common
{
/// <summary>
/// This is a example custom handler to show how to extend the library.
/// </summary>
[Guid("22a1a8e8-e929-4732-90ce-91eaff38b614")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class TestCustomHandler : FileBasedPreviewHandler
{
private CustomControlTest previewHandlerControl;
/// <inheritdoc />
public override void DoPreview()
{
this.previewHandlerControl.DoPreview(this.FilePath);
}
/// <inheritdoc />
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
{
this.previewHandlerControl = new CustomControlTest();
return this.previewHandlerControl;
}
}
}

View File

@@ -1,27 +1,27 @@
// 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.Runtime.InteropServices;
using Common.Cominterop;
namespace Common
{
/// <summary>
/// Extends the <see cref="PreviewHandlerBase" /> by implementing IInitializeWithFile.
/// </summary>
public abstract class FileBasedPreviewHandler : PreviewHandlerBase, IInitializeWithFile
{
/// <summary>
/// Gets the file path.
/// </summary>
public string FilePath { get; private set; }
/// <inheritdoc />
public void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode)
{
// Ignore the grfMode always use read mode to access the file.
this.FilePath = pszFilePath;
}
}
}
// 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.Runtime.InteropServices;
using Common.Cominterop;
namespace Common
{
/// <summary>
/// Extends the <see cref="PreviewHandlerBase" /> by implementing IInitializeWithFile.
/// </summary>
public abstract class FileBasedPreviewHandler : PreviewHandlerBase, IInitializeWithFile
{
/// <summary>
/// Gets the file path.
/// </summary>
public string FilePath { get; private set; }
/// <inheritdoc />
public void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode)
{
// Ignore the grfMode always use read mode to access the file.
this.FilePath = pszFilePath;
}
}
}

View File

@@ -1,158 +1,158 @@
// 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 System.Runtime.InteropServices;
using Common.ComInterlop;
using Microsoft.Win32;
namespace Common
{
/// <summary>
/// Preview Handler base class implementing interfaces required by Preview Handler.
/// </summary>
public abstract class PreviewHandlerBase : IPreviewHandler, IOleWindow, IObjectWithSite, IPreviewHandlerVisuals
{
/// <summary>
/// An instance of Preview Control Used by the Handler.
/// </summary>
private IPreviewHandlerControl previewControl;
/// <summary>
/// Hold reference for the window handle.
/// </summary>
private IntPtr parentHwnd;
/// <summary>
/// Hold the bounds of the window.
/// </summary>
private Rectangle windowBounds;
/// <summary>
/// Holds the site pointer.
/// </summary>
private object unkSite;
/// <summary>
/// Holds reference for the IPreviewHandlerFrame.
/// </summary>
private IPreviewHandlerFrame frame;
/// <summary>
/// Initializes a new instance of the <see cref="PreviewHandlerBase"/> class.
/// </summary>
public PreviewHandlerBase()
{
this.previewControl = this.CreatePreviewHandlerControl();
}
/// <inheritdoc />
public abstract void DoPreview();
/// <inheritdoc />
public void SetWindow(IntPtr hwnd, ref RECT rect)
{
this.parentHwnd = hwnd;
this.windowBounds = rect.ToRectangle();
this.previewControl.SetWindow(hwnd, this.windowBounds);
}
/// <inheritdoc />
public void SetRect(ref RECT rect)
{
this.windowBounds = rect.ToRectangle();
this.previewControl.SetRect(this.windowBounds);
}
/// <inheritdoc />
public void Unload()
{
this.previewControl.Unload();
}
/// <inheritdoc />
public void SetFocus()
{
this.previewControl.SetFocus();
}
/// <inheritdoc />
public void QueryFocus(out IntPtr phwnd)
{
this.previewControl.QueryFocus(out IntPtr result);
phwnd = result;
if (phwnd == IntPtr.Zero)
{
throw new Win32Exception();
}
}
/// <inheritdoc />
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://docs.microsoft.com/en-us/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;
}
/// <inheritdoc />
public void GetWindow(out IntPtr phwnd)
{
phwnd = this.previewControl.GetHandle();
}
/// <inheritdoc />
public void ContextSensitiveHelp(bool fEnterMode)
{
// Should always return NotImplementedException. Source: https://docs.microsoft.com/en-us/windows/win32/shell/building-preview-handlers#iolewindowcontextsensitivehelp
throw new NotImplementedException();
}
/// <inheritdoc />
public void SetSite(object pUnkSite)
{
// Implementation logic details: https://docs.microsoft.com/en-us/windows/win32/shell/building-preview-handlers#iobjectwithsitesetsite
this.unkSite = pUnkSite;
this.frame = this.unkSite as IPreviewHandlerFrame;
}
/// <inheritdoc />
public void GetSite(ref Guid riid, out object ppvSite)
{
ppvSite = this.unkSite;
}
/// <inheritdoc />
public void SetBackgroundColor(COLORREF color)
{
this.previewControl.SetBackgroundColor(color.Color);
}
/// <inheritdoc />
public void SetFont(ref LOGFONT plf)
{
this.previewControl.SetFont(Font.FromLogFont(plf));
}
/// <inheritdoc />
public void SetTextColor(COLORREF color)
{
this.previewControl.SetTextColor(color.Color);
}
/// <summary>
/// Provide instance of the implementation of <see cref="IPreviewHandlerControl"/>. Should be overridden by the implementation class with a control object to be used.
/// </summary>
/// <returns>Instance of the <see cref="IPreviewHandlerControl"/>.</returns>
protected abstract IPreviewHandlerControl CreatePreviewHandlerControl();
}
}
// 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 System.Runtime.InteropServices;
using Common.ComInterlop;
using Microsoft.Win32;
namespace Common
{
/// <summary>
/// Preview Handler base class implementing interfaces required by Preview Handler.
/// </summary>
public abstract class PreviewHandlerBase : IPreviewHandler, IOleWindow, IObjectWithSite, IPreviewHandlerVisuals
{
/// <summary>
/// An instance of Preview Control Used by the Handler.
/// </summary>
private IPreviewHandlerControl previewControl;
/// <summary>
/// Hold reference for the window handle.
/// </summary>
private IntPtr parentHwnd;
/// <summary>
/// Hold the bounds of the window.
/// </summary>
private Rectangle windowBounds;
/// <summary>
/// Holds the site pointer.
/// </summary>
private object unkSite;
/// <summary>
/// Holds reference for the IPreviewHandlerFrame.
/// </summary>
private IPreviewHandlerFrame frame;
/// <summary>
/// Initializes a new instance of the <see cref="PreviewHandlerBase"/> class.
/// </summary>
public PreviewHandlerBase()
{
this.previewControl = this.CreatePreviewHandlerControl();
}
/// <inheritdoc />
public abstract void DoPreview();
/// <inheritdoc />
public void SetWindow(IntPtr hwnd, ref RECT rect)
{
this.parentHwnd = hwnd;
this.windowBounds = rect.ToRectangle();
this.previewControl.SetWindow(hwnd, this.windowBounds);
}
/// <inheritdoc />
public void SetRect(ref RECT rect)
{
this.windowBounds = rect.ToRectangle();
this.previewControl.SetRect(this.windowBounds);
}
/// <inheritdoc />
public void Unload()
{
this.previewControl.Unload();
}
/// <inheritdoc />
public void SetFocus()
{
this.previewControl.SetFocus();
}
/// <inheritdoc />
public void QueryFocus(out IntPtr phwnd)
{
this.previewControl.QueryFocus(out IntPtr result);
phwnd = result;
if (phwnd == IntPtr.Zero)
{
throw new Win32Exception();
}
}
/// <inheritdoc />
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://docs.microsoft.com/en-us/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;
}
/// <inheritdoc />
public void GetWindow(out IntPtr phwnd)
{
phwnd = this.previewControl.GetHandle();
}
/// <inheritdoc />
public void ContextSensitiveHelp(bool fEnterMode)
{
// Should always return NotImplementedException. Source: https://docs.microsoft.com/en-us/windows/win32/shell/building-preview-handlers#iolewindowcontextsensitivehelp
throw new NotImplementedException();
}
/// <inheritdoc />
public void SetSite(object pUnkSite)
{
// Implementation logic details: https://docs.microsoft.com/en-us/windows/win32/shell/building-preview-handlers#iobjectwithsitesetsite
this.unkSite = pUnkSite;
this.frame = this.unkSite as IPreviewHandlerFrame;
}
/// <inheritdoc />
public void GetSite(ref Guid riid, out object ppvSite)
{
ppvSite = this.unkSite;
}
/// <inheritdoc />
public void SetBackgroundColor(COLORREF color)
{
this.previewControl.SetBackgroundColor(color.Color);
}
/// <inheritdoc />
public void SetFont(ref LOGFONT plf)
{
this.previewControl.SetFont(Font.FromLogFont(plf));
}
/// <inheritdoc />
public void SetTextColor(COLORREF color)
{
this.previewControl.SetTextColor(color.Color);
}
/// <summary>
/// Provide instance of the implementation of <see cref="IPreviewHandlerControl"/>. Should be overridden by the implementation class with a control object to be used.
/// </summary>
/// <returns>Instance of the <see cref="IPreviewHandlerControl"/>.</returns>
protected abstract IPreviewHandlerControl CreatePreviewHandlerControl();
}
}

View File

@@ -1,27 +1,27 @@
// 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.Runtime.InteropServices.ComTypes;
using Common.ComInterlop;
namespace Common
{
/// <summary>
/// Extends the <see cref="PreviewHandlerBase" /> by implementing IInitializeWithStream.
/// </summary>
public abstract class StreamBasedPreviewHandler : PreviewHandlerBase, IInitializeWithStream
{
/// <summary>
/// Gets the stream object to access file.
/// </summary>
public IStream Stream { get; private set; }
/// <inheritdoc/>
public void Initialize(IStream pstream, uint grfMode)
{
// Ignore the grfMode always use read mode to access the file.
this.Stream = pstream;
}
}
}
// 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.Runtime.InteropServices.ComTypes;
using Common.ComInterlop;
namespace Common
{
/// <summary>
/// Extends the <see cref="PreviewHandlerBase" /> by implementing IInitializeWithStream.
/// </summary>
public abstract class StreamBasedPreviewHandler : PreviewHandlerBase, IInitializeWithStream
{
/// <summary>
/// Gets the stream object to access file.
/// </summary>
public IStream Stream { get; private set; }
/// <inheritdoc/>
public void Initialize(IStream pstream, uint grfMode)
{
// Ignore the grfMode always use read mode to access the file.
this.Stream = pstream;
}
}
}