简体   繁体   English

如何通过按插入使我的表单可见/不可见?

[英]How can I make my Form visible/invisible by pressing insert?

I'm trying to find out how to making my form invisible when I press insert and when I press insert again it makes the form visible. 我试图找出如何在按下插入键时使表格不可见,而当我再次按下插入键时使表格可见。 I try to find out how, but no one seems to have what I am looking for. 我试图找出方法,但似乎没人能找到我想要的东西。

A simple example how you can manipulate Form's visibility by handling the Insert key down: 一个简单的示例,说明如何通过向下按下Insert键来操纵Form的可见性:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        // Don't forget to enable Form.KeyPreview in order to receive key down events
        if (e.KeyCode == Keys.Insert)
        {
            Visible = false;
        }
    }
}

You can set Visible back to true to make it visible. 您可以将Visible设置回true以使其可见。 However, you will not be able to do this, because Form became invisible and doesn't receive key down events anymore. 但是,您将无法执行此操作,因为Form变得不可见,并且不再接收按键事件。 In this case you can try to set the global hotkey using, for example, the GlobalHotKey library described here . 在这种情况下,您可以尝试使用例如此处描述的GlobalHotKey库来设置全局热键。 Note also, that it doesn't make sense to set a single key (eg Insert) as global hotkey as in most cases system or another application will capture it. 还要注意,将单个键(例如,Insert)设置为全局热键是没有意义的,因为在大多数情况下,系统或其他应用程序会捕获它。

You can do this: 你可以这样做:

private void InfoForm_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Insert) Opacity = Opacity == 0 ? 1 : 0;
}

For this to work you need to turn on the Form's KeyPreview property! 为此,您需要打开Form的KeyPreview属性!

But turning visiblity back on (or to be precise turning off opacity) will only work if no other program has received focus in the meantime. 但是, 只有在此期间没有其他程序获得focus ,重新打开可见性(或者精确地说是关闭不透明度) 有效。

If that might happen you need to set a global keyboard hook ; 如果发生这种情况,您需要设置一个全局键盘挂钩 make sure to pass the Insert key back on or else many other programms will no longer work right.. All in all I would not recommend it.. 请确保再次传递插入键,否则许多其他程序将无法正常使用。.总之,我不建议这样做。

I'm not sure when the whole idea might make sense. 我不确定整个想法何时才有意义。 One answer could be to show or hide a popup data window that is only meant to show some additional information popping up from a base data window. 一个答案可能是显示或隐藏一个弹出数据窗口,该窗口仅意味着显示从基本数据窗口弹出的一些其他信息。

In that case you could simply close the window whenever it gets deactivated: 在这种情况下,只要停用该窗口,就可以将其关闭:

private void InfoForm_Deactivate(object sender, EventArgs e)
{
    this.Close();
}

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

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