mirror of
https://github.com/Hofknecht/SystemTrayMenu.git
synced 2024-10-01 01:27:33 +13:00
55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace SystemTrayMenu.DllImports
|
|
{
|
|
public static partial class NativeMethods
|
|
{
|
|
[DllImport("user32.dll")]
|
|
private static extern bool IsIconic(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
|
|
|
|
[DllImport("kernel32.dll")]
|
|
private static extern uint GetCurrentThreadId();
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
private static extern bool BringWindowToTop(IntPtr hWnd);
|
|
|
|
private const int SW_RESTORE = 9;
|
|
|
|
public static void ForceForegroundWindow(IntPtr hWnd)
|
|
{
|
|
uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
|
|
uint appThread = GetCurrentThreadId();
|
|
const int SW_SHOW = 5;
|
|
|
|
int cmdShow = SW_SHOW;
|
|
|
|
if (IsIconic(hWnd))
|
|
{
|
|
cmdShow = SW_RESTORE;
|
|
}
|
|
|
|
if (foreThread != appThread)
|
|
{
|
|
AttachThreadInput(foreThread, appThread, true);
|
|
BringWindowToTop(hWnd);
|
|
ShowWindow(hWnd, cmdShow);
|
|
AttachThreadInput(foreThread, appThread, false);
|
|
}
|
|
else
|
|
{
|
|
BringWindowToTop(hWnd);
|
|
ShowWindow(hWnd, cmdShow);
|
|
}
|
|
}
|
|
}
|
|
}
|