简体   繁体   English

使用两种KeyDown方法不起作用来最大化/最小化表单

[英]Maximize/Minimize Form With Two KeyDown Methods Not Working

I'm attempting to get a WPF form to maximize or minimize when "enter" is pressed. 我正在尝试获得一个WPF表单,以便在按下“输入”时最大化或最小化。 However,when debugging, it doesn't work. 但是,在调试时,它不起作用。
I can write this so that it can minimize but not maximize, but not be able to do both once one action is performed. 我可以这样编写,以便它可以最小化但不能最大化,但是一旦执行一项操作就不能同时执行这两项操作。
If someone could push me in the right direction, it would be much appreciated. 如果有人可以将我推向正确的方向,将不胜感激。
I'm currently using "Enter" as a placeholder for a combination of two keys that I haven't decided yet. 我目前正在使用“ Enter”作为占位符,用于尚未确定的两个键的组合。 May be annoying for some, I know, but it works for me at the moment. 我知道有些人可能会很烦,但目前对我有用。
Also, I'm attempting to make a general overlay program that can run in the background and can pop up when the key combination is pressed. 另外,我试图制作一个通用的覆盖程序,该程序可以在后台运行,并在按下组合键时弹出。

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (this.WindowState == WindowState.Minimized)
    {
        if (e.Key == Key.Enter)
        {
            this.WindowState = WindowState.Maximized;
        }
    }
}
private void MainWindow_KeyDown2(object sender, KeyEventArgs e)
{
    if (this.WindowState == WindowState.Maximized)
    {
         if (e.Key == Key.Enter)
         {
             this.WindowState = WindowState.Minimized;
         } 
    }
}

Try it like this 像这样尝试

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{        
    if (e.Key == Key.Enter)
    {
         if (this.WindowState == WindowState.Minimized)
         {
             this.WindowState = WindowState.Maximized;
         }
         else
         {
             this.WindowState = WindowState.Minimized;
         }
    }
}

Once minimized, the application will not raise or respond events such as KeyDown. 一旦最小化,该应用程序将不会引发或响应诸如KeyDown之类的事件。

However, this works fine when it's in a normal or maximized view state: 但是,当它处于正常或最大化视图状态时,这可以正常工作:

private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            if (this.WindowState == WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowState = WindowState.Normal;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM