2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="BringWindowToTop.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
namespace SystemTrayMenu.DllImports
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// wraps the methodcalls to native windows dll's.
|
|
|
|
|
/// </summary>
|
2020-03-28 08:43:16 +13:00
|
|
|
|
public static partial class NativeMethods
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
private const int SwRestore = 9;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
cmdShow = SwRestore;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (foreThread != appThread)
|
|
|
|
|
{
|
|
|
|
|
AttachThreadInput(foreThread, appThread, true);
|
|
|
|
|
BringWindowToTop(hWnd);
|
|
|
|
|
ShowWindow(hWnd, cmdShow);
|
|
|
|
|
AttachThreadInput(foreThread, appThread, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BringWindowToTop(hWnd);
|
|
|
|
|
ShowWindow(hWnd, cmdShow);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
|
|
|
|
[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);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|