Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用键盘快捷键从系统托盘最大化C#应用程序_C#_Winforms_Maximize Window - Fatal编程技术网

使用键盘快捷键从系统托盘最大化C#应用程序

使用键盘快捷键从系统托盘最大化C#应用程序,c#,winforms,maximize-window,C#,Winforms,Maximize Window,我是否可以知道我是否可以使用键盘快捷键而不是单击系统托盘来最大化我的windows窗体应用程序 我目前正在使用这段代码 //Minimize to Tray with over-ride for short cut private void MinimiseToTray(bool shortCutPressed) { notifyIcon.BalloonTipTitle = "Minimize to Tray App"; notifyIcon.BalloonTipText = "

我是否可以知道我是否可以使用键盘快捷键而不是单击系统托盘来最大化我的windows窗体应用程序

我目前正在使用这段代码

//Minimize to Tray with over-ride for short cut
private void MinimiseToTray(bool shortCutPressed)
{
    notifyIcon.BalloonTipTitle = "Minimize to Tray App";
    notifyIcon.BalloonTipText = "You have successfully minimized your app.";

    if (FormWindowState.Minimized == this.WindowState || shortCutPressed)
    {
        notifyIcon.Visible = true;
        notifyIcon.ShowBalloonTip(500);
        this.Hide();
    }
    else if (FormWindowState.Normal == this.WindowState)
    {
        notifyIcon.Visible = false;
    }
}
因此,我需要一个键盘快捷键,应该最大限度地发挥它。非常感谢

要执行此操作,必须使用“低级挂钩”。 您可以在本文中找到有关它的所有信息:

再看看这一点:

编辑:如果你只是想“保留一个组合键”在你的应用程序上执行某些操作,那么一个低级键盘钩子让你看到每个按键都指向任何其他应用程序不仅仅是一种过度使用,但是,在我个人看来,糟糕的做法很可能会让人们认为你是在使用键盘记录!坚持使用热键

考虑到图标没有键盘焦点,您需要注册一个全局键盘热键

其他类似问题:

示例来自:


我赞同弗兰克关于全球键盘挂钩的建议。就个人而言,我对CodeProject文章“”有很好的体验

正如他们在文章中所写,你可以做如下事情:

private UserActivityHook _actHook;

private void MainFormLoad(object sender, System.EventArgs e)
{
    _actHook = new UserActivityHook();

    _actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
}
然后,您可以在
MyKeyPress
处理程序中调用打开窗口的函数。

如果您按照指南进行操作。它将向您展示如何注册全局快捷键

public partial class Form1 : Form
{
    KeyboardHook hook = new KeyboardHook();

    public Form1()
    {
        InitializeComponent();

        // register the event that is fired after the key press.
        hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);

        // register the CONTROL + ALT + F12 combination as hot key.
        // You can change this.
        hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt, Keys.F12);
    }

    private void hook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        // Trigger your function
        MinimiseToTray(true);
    }

    private void MinimiseToTray(bool shortCutPressed)
    {
        // ... Your code
    }
}
公共部分类表单1:表单
{
键盘挂钩=新键盘挂钩();
公共表格1()
{
初始化组件();
//注册按键后触发的事件。
hook.KeyPressed+=新事件处理程序(hook\u KeyPressed);
//将CONTROL+ALT+F12组合注册为热键。
//你可以改变这个。
hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt,Keys.F12);
}
私有无效挂钩(按下键)(对象发送器,按下键Eventargs e)
{
//触发你的功能
最低死亡率(真);
}
私人空间最小总辐射(bool shortCutPressed)
{
//…您的代码
}
}

如果你想拥有一个全局热键,就不需要去“低级”。低级的倾向于窥探任何键盘活动,而不是保留一个供自己使用的组合键。这里必须同意@RayHayes,你甚至可能会被一些反恶意软件调用。但现在你已经从想要一个简单的键盘组合来显示应用程序,变成需要处理整个应用程序中的每一次击键……我们可以使用比你需要的更多的键(例如,所有键,而不是特定的组合),你不仅增加了更多的工作——需要过滤结果以获得你想要的,而且你也没有坚持“只要求你需要的”原则。想象一下,如果安卓手机上的“记事本”应用程序要求“拨打电话”访问,你会担心的。如果托盘实用程序希望看到我输入的所有密码,以便显示自己,我会担心。一个防病毒工具可能也代表我关注@RayHayes是的,但真的:这对最终用户有什么影响?不是最终用户问的问题——我想,是开发人员问的。如果防病毒工具发现键盘挂钩的使用并将其添加到恶意软件列表中,可能会影响最终用户!再次回到我的“android”示例,如果一个记事本应用程序想要访问我的手机制作功能,它不会真正影响我,除非它被滥用。如果全局热键不能完成你需要的任务,低级别的键盘挂钩应该作为最后手段。当您使用这样的低级钩子时,您正在为每个应用程序中的每个击键运行代码,这不仅会降低它的速度或可能与其他击键事件处理程序产生问题,还可能被防病毒等标记。为了避免不必要的开销和问题,这并不“深奥”。使用这样的钩子更等同于使用文本框来存储字符串,而不是数组或列表-它可以工作,但编码很差。我希望使用它,但我必须#include/Using.etc做什么?它说热键不存在有一个从页面链接到的包装器类(“带有.NET的全局热键”),这些东西中的大多数实际上是Win32功能。
public partial class Form1 : Form
{
    KeyboardHook hook = new KeyboardHook();

    public Form1()
    {
        InitializeComponent();

        // register the event that is fired after the key press.
        hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);

        // register the CONTROL + ALT + F12 combination as hot key.
        // You can change this.
        hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt, Keys.F12);
    }

    private void hook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        // Trigger your function
        MinimiseToTray(true);
    }

    private void MinimiseToTray(bool shortCutPressed)
    {
        // ... Your code
    }
}