如何将窗户带到最前沿

时间:2016-12-15 13:10:17

标签: c# winforms hook pinvoke

我有一个在系统托盘中运行的应用程序,用于捕获用户的按键。我只关心windows按钮和F12组合。当发生这种情况时,我想将一个特定的应用程序(打开)放在前面,在所有其他窗口的顶部。我已经阅读了很多SO文章,并且已经能够捕获关键组合但是没有运气将应用程序带到前面。

MainForm.cs代码:

[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

readonly KeyboardHook hook = new KeyboardHook();

public MainForm()
{
    InitializeComponent();
    hook.AutoRepeat = true;
    hook.SetKeys(new KeyCombination(KeysEx.WinLogo | KeysEx.F12));
    hook.Pressed += hook_Pressed;
    hook.Engage();
}

void hook_Pressed(object sender, EventArgs e)
{
    Action a = () =>
    {
        Process[] currentProcess = Process.GetProcessesByName("Whiteboard");

        if (currentProcess.Length > 0)
        {
            IntPtr hWnd = currentProcess[0].MainWindowHandle;

            if (hWnd != IntPtr.Zero)
            {
                SetForegroundWindow(hWnd);
                ShowWindow(hWnd, (int)ShowWindowCommands.SW_MAXIMIZE);
                return;
            }
        }
    };

    Task.Run(a);
}

KeyboardHook课程/项目来自此Refactorsaurus Rex文章以及GitHub上的完整来源。

ShowWindowCommands类看起来像这样:

    public static class Enums
{
    public enum ShowWindowCommands : uint
    {
        //Hides the window and activates another window.
        SW_HIDE = 0,

        //Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
        SW_SHOWNORMAL = 1,

        //Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
        SW_NORMAL = 1,

        //Activates the window and displays it as a minimized window.
        SW_SHOWMINIMIZED = 2,

        //Activates the window and displays it as a maximized window.
        SW_SHOWMAXIMIZED = 3,

        //Maximizes the specified window.
        SW_MAXIMIZE = 3,

        //Displays a window in its most recent size and position. This value is similar to <see cref="ShowWindowCommands.SW_SHOWNORMAL"/>, except the window is not activated.
        SW_SHOWNOACTIVATE = 4,

        //Activates the window and displays it in its current size and position.
        SW_SHOW = 5,

        //Minimizes the specified window and activates the next top-level window in the z-order.
        SW_MINIMIZE = 6,

        //Displays the window as a minimized window. This value is similar to <see cref="ShowWindowCommands.SW_SHOWMINIMIZED"/>, except the window is not activated.
        SW_SHOWMINNOACTIVE = 7,

        //Displays the window in its current size and position. This value is similar to <see cref="ShowWindowCommands.SW_SHOW"/>, except the window is not activated.
        SW_SHOWNA = 8,

        //Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
        SW_RESTORE = 9
    }
}

这是从PInvoke.net复制的。

我知道它找到了我想打开的应用程序,它似乎设置了#34;焦点&#34;它,但不是把它带到前面。这是一个例子:

还没有按键: enter image description here

按下组合键: enter image description here

你可以看到其中一个小窗户被移动了#34;在后面,但白板应用程序保持不变,除了它似乎有焦点。那么如何让我的应用程序带来&#34; Whiteboard&#34;窗口在前面(最顶层)?

更新

如果我在正常的窗口状态下运行我的应用程序(未最小化到系统托盘),那么它会将该窗口移到前面。

0 个答案:

没有答案