2021-12-17 23:18:20 +03:00
|
|
|
|
using System.Windows.Forms;
|
2018-08-25 01:49:04 +03:00
|
|
|
|
|
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
|
2018-08-25 01:49:04 +03:00
|
|
|
|
{
|
2021-12-17 22:59:54 +03:00
|
|
|
|
public Win32Window(IntPtr hwnd)
|
2018-08-25 01:49:04 +03:00
|
|
|
|
{
|
2021-12-17 22:59:54 +03:00
|
|
|
|
Handle = hwnd;
|
2018-08-25 01:49:04 +03:00
|
|
|
|
}
|
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
|
|
|
|
}
|