示例#1
0
        public static IntPtr BringWindowToFront(
            string name,
            bool _,
            bool isRetoreMinimizedWindow = false)
        {
            IntPtr window = InteropWindow.FindWindow((string)null, name);

            if (window == IntPtr.Zero)
            {
                throw new SystemException("Cannot find window '" + name + "'", (Exception) new Win32Exception(Marshal.GetLastWin32Error()));
            }
            if (!InteropWindow.SetForegroundWindow(window))
            {
                throw new SystemException("Cannot set foreground window", (Exception) new Win32Exception(Marshal.GetLastWin32Error()));
            }
            if (isRetoreMinimizedWindow)
            {
                InteropWindow.ShowWindow(window, 9);
            }
            else
            {
                InteropWindow.ShowWindow(window, 5);
            }
            return(window);
        }
示例#2
0
        public static bool ForceSetForegroundWindow(IntPtr h)
        {
            if (h == IntPtr.Zero)
            {
                return(false);
            }
            IntPtr foregroundWindow = InteropWindow.GetForegroundWindow();

            if (foregroundWindow == IntPtr.Zero)
            {
                return(InteropWindow.SetForegroundWindow(h));
            }
            if (h == foregroundWindow)
            {
                return(true);
            }
            uint ProcessId             = 0;
            uint windowThreadProcessId = InteropWindow.GetWindowThreadProcessId(foregroundWindow, ref ProcessId);
            uint currentThreadId       = InteropWindow.GetCurrentThreadId();

            if ((int)currentThreadId == (int)windowThreadProcessId)
            {
                return(InteropWindow.SetForegroundWindow(h));
            }
            if (windowThreadProcessId != 0U)
            {
                if (!InteropWindow.AttachThreadInput(currentThreadId, windowThreadProcessId, true))
                {
                    return(false);
                }
                if (!InteropWindow.SetForegroundWindow(h))
                {
                    InteropWindow.AttachThreadInput(currentThreadId, windowThreadProcessId, false);
                    return(false);
                }
                InteropWindow.AttachThreadInput(currentThreadId, windowThreadProcessId, false);
            }
            return(true);
        }