using System; using System.Drawing; using System.Runtime.InteropServices; namespace JD.UserControls.QuickOpenDialogs { /// /// (Taken from CodeProject) /// public static class NativeMethods { internal const int WM_NCHITTEST = 0x0084, WM_NCACTIVATE = 0x0086, WS_EX_NOACTIVATE = 0x08000000, HTTRANSPARENT = -1, HTLEFT = 10, HTRIGHT = 11, HTTOP = 12, HTTOPLEFT = 13, HTTOPRIGHT = 14, HTBOTTOM = 15, HTBOTTOMLEFT = 16, HTBOTTOMRIGHT = 17, WM_USER = 0x0400, WM_REFLECT = WM_USER + 0x1C00, WM_COMMAND = 0x0111, CBN_DROPDOWN = 7, WM_GETMINMAXINFO = 0x0024; internal static int HIWORD(int n) { return (n >> 16) & 0xffff; } internal static int HIWORD(IntPtr n) { long ptrValue = n.ToInt64(); // 64-bit compatibility? return HIWORD(unchecked((int)ptrValue)); } internal static int LOWORD(int n) { return n & 0xffff; } internal static int LOWORD(IntPtr n) { long ptrValue = n.ToInt64(); // 64-bit compatibility? return LOWORD(unchecked((int)ptrValue)); } [StructLayout(LayoutKind.Sequential)] internal struct MINMAXINFO { public Point reserved; public Size maxSize; public Point maxPosition; public Size minTrackSize; public Size maxTrackSize; } [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] 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")] private static extern IntPtr GetLastActivePopup(IntPtr hWnd); public static void SetForegroundWindowEx(IntPtr hWnd) { // Attach foreground window thread to our thread uint foreGroundID = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero); uint currentID = GetCurrentThreadId(); AttachThreadInput(foreGroundID, currentID, true); // Do our stuff here IntPtr lastActivePopupWnd = GetLastActivePopup(hWnd); SetForegroundWindow(lastActivePopupWnd); // Detach the attached thread AttachThreadInput(foreGroundID, currentID, false); } } }