/// Initializes a new instance of the <see cref="FormHandlerControl"/> class.
/// </summary>
publicFormHandlerControl()
{
// 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.
varforceCreation=this.Handle;
this.FormBorderStyle=FormBorderStyle.None;
this.Visible=false;
}
/// <inheritdoc />
publicIntPtrGetHandle()
{
returnthis.Handle;
}
/// <inheritdoc />
publicvoidQueryFocus(outIntPtrresult)
{
vargetResult=IntPtr.Zero;
this.InvokeOnControlThread(()=>
{
getResult=GetFocus();
});
result=getResult;
}
/// <inheritdoc />
publicvoidSetBackgroundColor(ColorargbColor)
{
this.InvokeOnControlThread(()=>
{
this.BackColor=argbColor;
});
}
/// <inheritdoc />
publicvoidSetFocus()
{
this.InvokeOnControlThread(()=>
{
this.Focus();
});
}
/// <inheritdoc />
publicvoidSetFont(Fontfont)
{
this.InvokeOnControlThread(()=>
{
this.Font=font;
});
}
/// <inheritdoc />
publicvoidSetRect(RectanglewindowBounds)
{
this.UpdateWindowBounds(windowBounds);
}
/// <inheritdoc />
publicvoidSetTextColor(Colorcolor)
{
this.InvokeOnControlThread(()=>
{
this.ForeColor=color;
});
}
/// <inheritdoc />
publicvoidSetWindow(IntPtrhwnd,Rectanglerect)
{
this.parentHwnd=hwnd;
this.UpdateWindowBounds(rect);
}
/// <inheritdoc />
publicvirtualvoidUnload()
{
this.InvokeOnControlThread(()=>
{
this.Visible=false;
foreach(Controlcinthis.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 />
publicvirtualvoidDoPreview<T>(TdataSource)
{
this.Visible=true;
}
/// <summary>
/// Executes the specified delegate on the thread that owns the control's underlying window handle.
/// 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>