WindowHandling: Use existing User32 DllImports

This commit is contained in:
2022-04-09 16:11:50 +02:00
parent 050ad1ad9c
commit 1881974311
4 changed files with 49 additions and 51 deletions

View File

@@ -1,50 +0,0 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
// ReSharper disable InconsistentNaming
namespace VAR.ScreenAutomation.Code
{
public static 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)
{
SetWindowPos(form.Handle, top
? HWND_TOPMOST
: 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);
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Diagnostics;
using System.Windows.Forms;
using VAR.Toolbox.Code.Windows;
// ReSharper disable InconsistentNaming
namespace VAR.Toolbox.Code
{
public static class WindowHandling
{
public static void WindowSetTopLevel(Form form, bool top = true)
{
User32.SetWindowPos(form.Handle, top
? User32.HWND_TOPMOST
: User32.HWND_NOTOPMOST,
0, 0, 0, 0, User32.TOPMOST_FLAGS);
}
public static bool ApplicationIsActivated()
{
var activatedHandle = User32.GetForegroundWindow();
if (activatedHandle == IntPtr.Zero)
{
return false;
}
var procId = Process.GetCurrentProcess().Id;
User32.GetWindowThreadProcessId(activatedHandle, out int activeProcId);
return activeProcId == procId;
}
}
}

View File

@@ -175,6 +175,9 @@ namespace VAR.Toolbox.Code.Windows
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
public static string GetActiveWindowTitle()
{
const int NChars = 256;
@@ -188,5 +191,17 @@ namespace VAR.Toolbox.Code.Windows
return null;
}
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public const UInt32 SWP_NOSIZE = 0x0001;
public const UInt32 SWP_NOMOVE = 0x0002;
public const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy,
uint uFlags);
}
}

View File

@@ -81,7 +81,6 @@
<Compile Include="Code\Bots\Mouse.cs" />
<Compile Include="Code\Bots\Screenshoter.cs" />
<Compile Include="Code\Bots\TetrisBot.cs" />
<Compile Include="Code\Bots\WindowHandling.cs" />
<Compile Include="Code\Configuration\FileBackedConfiguration.cs" />
<Compile Include="Code\Configuration\IConfiguration.cs" />
<Compile Include="Code\Configuration\MemoryBackedConfiguration.cs" />
@@ -122,6 +121,7 @@
<Compile Include="Code\DirectShow\Tools.cs" />
<Compile Include="Code\DirectShow\Uuids.cs" />
<Compile Include="Code\WebServicesUtils.cs" />
<Compile Include="Code\WindowHandling.cs" />
<Compile Include="Code\Windows\GDI32.cs" />
<Compile Include="Code\TextCoders\ITextCoder.cs" />
<Compile Include="Code\IOutputHandler.cs" />