RegisterHotKey与无形的形式工作(C#) [英] RegisterHotKey not working with invisible forms (c#)

查看:137
本文介绍了RegisterHotKey与无形的形式工作(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个图标在系统托盘中,然后给它一个全球性的键盘快捷键来进行功能。

I'm trying to put an icon in the system tray and then give it a global keyboard shortcut to carry out a function.

我使用RegisterHotKey到设置全局快捷键,和它的作品,如果与该图标关联的主要形式是可见的。但是如果表单是不可见然后WndProc方法永远不会调用

I'm using RegisterHotKey to set the global keyboard shortcut, and it works if the main form associated with the icon is visible. But if the form is invisible then the WndProc method is never invoked.

任何想法

编辑:$? b $ b口的意思是隐藏的是什么,以下将被添加到主要形式有:

What I mean by "hidden" is that the following is added to the main form:

protected override void OnLoad(EventArgs e)
{
    hotKey = new GlobalHotkey(GlobalHotkey.WIN, Keys.T, this);
    bool registered = hotKey.Register();
    Visible = false;
    ShowInTaskbar = false;
    base.OnLoad(e);
}



注册是显示为真,在弹出的快捷键优良工程如果我离开了可见=虚假的;和ShowInTaskbar = FALSE;

"registered" is showing as "true", and the shortcut key works fine if I leave out the "Visible = false;" and the "ShowInTaskbar = false;".

推荐答案

的WinForms围绕着一个相当严厉的限制在WINAPI。创建窗口时,窗口的某些属性只能被指定的,不能再更改。或者换句话说,他们在本地函数CreateWindowEx()调用指定的。

Winforms works around a pretty draconian restriction in the winapi. Some properties of a window can only be specified when a window is created and can't be changed later. Or in other words, they are specified in the native CreateWindowEx() call.

它的工作原理围绕它由()再次调用函数CreateWindowEx。或者换句话说,破坏现有的窗口,并重新创建它。这是一个漂亮的伎俩,但它确实有一些副作用。你可以看到闪烁一丁点儿例如当新窗口中绘制本身。一些较大的副作用。例如一个TreeView可见。所有当它得到重新节点崩溃。难以避免,有一个与原来的窗口相关的实在太多状态。对于一个表,ShowInTaskBar属性就是这样的一个特性。但也从右至左,FormBorderStyle,控制盒,等等。

It works around it by calling CreateWindowEx() again. Or in other words, destroy the existing window and create it again. That's a nifty trick but it does have some side effects. You can see a wee bit of flicker for example when the new window paints itself. Some bigger side effects are visible on for example a TreeView. All the nodes collapse when it gets recreated. Hard to avoid, there is just too much state associated with the original window. For a Form, the ShowInTaskbar property is one such property. But also RightToLeft, FormBorderStyle, ControlBox, etcetera.

最相关的副作用是你正在运行到的人。重塑窗口总是改变Handle属性,不可避免。这不顺心时,你使用RegisterHotKey(),或者使用它的图书馆,那WINAPI调用使用的窗口句柄。所以,当销毁的WinForms该窗口会不会再有所回调。

The most relevant side-effect is the one you are running into. Recreating the window always changes the Handle property, inevitably. And that goes wrong when you use RegisterHotKey(), or a library that uses it, that winapi call uses the window handle. So when Winforms destroys that window there will never again be a callback.

这是很容易解决,你只是使用了错误的事件处理程序。使为 OnHandleCreated 方法,而不是一个覆盖呼叫。它重新运行窗口被重新创建时。还有一种简单的办法,但几乎没有可靠的,是只在构造函数中设置类似ShowInTaskbar属性。

It is easy to fix, you are just using the wrong event handler. Make the call in an override for the OnHandleCreated method instead. It re-runs when the window gets re-created. Yet another easy fix, but not nearly as reliable, is to only set properties like ShowInTaskbar in the constructor.

这篇关于RegisterHotKey与无形的形式工作(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆