1
0
mirror of synced 2024-12-11 23:36:01 +01:00
Switch-Toolbox/Switch_Toolbox_Library/Forms/NonBlickingDialog.cs

56 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Interop;
namespace Switch_Toolbox.Library.Forms
{
public static class NoneBlockingDialog
{
2019-05-18 02:35:40 +02:00
private static DispatcherFrame frame;
private static void closeHandler(object sender, EventArgs args)
{
2019-05-18 02:35:40 +02:00
frame.Continue = false;
}
2019-05-18 02:35:40 +02:00
public static DialogResult ShowDialogNonBlocking(this Form window, Form ownder)
{
frame = new DispatcherFrame();
try
{
SetNativeEnabled(ownder.Handle, false);
window.Closed += closeHandler;
window.Show();
Dispatcher.PushFrame(frame);
}
finally
{
window.Closed -= closeHandler;
SetNativeEnabled(ownder.Handle, true);
}
return window.DialogResult;
}
const int GWL_STYLE = -16;
const int WS_DISABLED = 0x08000000;
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
static void SetNativeEnabled(IntPtr hWnd, bool enabled)
{
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_DISABLED | (enabled ? 0 : WS_DISABLED));
}
}
}