WindowHandling: Use existing User32 DllImports
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
33
VAR.Toolbox/Code/WindowHandling.cs
Normal file
33
VAR.Toolbox/Code/WindowHandling.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user