naps2/NAPS2.Lib.WinForms/Platform/Windows/Win32Window.cs

30 lines
678 B
C#
Raw Normal View History

using System.Windows.Forms;
2021-12-17 22:59:54 +03:00
namespace NAPS2.Platform.Windows;
/// <summary>
/// A trivial implementation of IWin32Window for use when serializing window handles cross-process.
/// </summary>
public class Win32Window : IWin32Window
{
2021-12-17 22:59:54 +03:00
public Win32Window(IntPtr hwnd)
{
2021-12-17 22:59:54 +03:00
Handle = hwnd;
}
2018-09-06 06:24:25 +03:00
2021-12-17 22:59:54 +03:00
public IntPtr Handle { get; }
}
public static class Win32WindowExtensions
{
public static IntPtr SafeHandle(this IWin32Window window)
2018-09-06 06:24:25 +03:00
{
2021-12-17 22:59:54 +03:00
if (window is Form form)
2018-09-06 06:24:25 +03:00
{
2021-12-17 22:59:54 +03:00
IntPtr hwnd = IntPtr.Zero;
form.Invoke(new Action(() => hwnd = window.Handle));
return hwnd;
2018-09-06 06:24:25 +03:00
}
2021-12-17 22:59:54 +03:00
return window.Handle;
2018-09-06 06:24:25 +03:00
}
2021-12-17 22:59:54 +03:00
}