// // Copyright (c) PlaceholderCompany. All rights reserved. // namespace SystemTrayMenu.DllImports { using System; using System.Runtime.InteropServices; /// /// wraps the methodcalls to native windows dll's. /// public static partial class NativeMethods { private const int SwRestore = 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 = SwRestore; } if (foreThread != appThread) { AttachThreadInput(foreThread, appThread, true); BringWindowToTop(hWnd); ShowWindow(hWnd, cmdShow); AttachThreadInput(foreThread, appThread, false); } else { BringWindowToTop(hWnd); ShowWindow(hWnd, cmdShow); } } [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] private static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] private static extern uint GetCurrentThreadId(); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] private static extern bool BringWindowToTop(IntPtr hWnd); } }