52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows.Forms;
|
|
|
|
namespace VAR.ScreenAutomation.Code
|
|
{
|
|
public class WindowHandling
|
|
{
|
|
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
|
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
|
|
private const UInt32 SWP_NOSIZE = 0x0001;
|
|
private const UInt32 SWP_NOMOVE = 0x0002;
|
|
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
|
|
|
public static void WindowSetTopLevel(Form form, bool top = true)
|
|
{
|
|
if (top)
|
|
{
|
|
SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
|
|
}
|
|
else
|
|
{
|
|
SetWindowPos(form.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
|
|
}
|
|
}
|
|
|
|
public static bool ApplicationIsActivated()
|
|
{
|
|
var activatedHandle = GetForegroundWindow();
|
|
if (activatedHandle == IntPtr.Zero)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var procId = Process.GetCurrentProcess().Id;
|
|
GetWindowThreadProcessId(activatedHandle, out int activeProcId);
|
|
return activeProcId == procId;
|
|
}
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
|
|
}
|
|
}
|