在 C# 中最小化托盘中的系统窗口窗体而不会看到它挂在任务栏上 [英] Minimizing a system windows form in tray in C# without seeing it hanging where taskbar

查看:45
本文介绍了在 C# 中最小化托盘中的系统窗口窗体而不会看到它挂在任务栏上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
//this.visible = false (Hotkeys stop working forever but the grey rest of the form would disappear from above taskbar)
ReRegisterHotkeys();

我使用上面的代码通过托盘图标最小化我的应用程序.现在最小化的表单其余部分仍然挂在开始按钮所在的任务栏上方的左上角.可见的只有带有小x"的灰色标题栏和标题栏上方的标题文本.这很奇怪.我将表单设置为最小化"并设置为不在任务栏中显示,但它仍然显示.我已经用我的表单注册了热键,所以我可能不会将它设置为不可见",否则即使我之后再次重新注册热键,热键也会以某种方式停止工作.除了将其设置为不可见"之外,我还没有找到删除此最小化表单标题的替代方法或者删除它的标题栏我也不想做的.本程序需要标题栏、标题栏图标和标题栏控制区,窗体不得成为工具窗口或无边框.

I use the code above to minimize my application with a tray icon. Now the minimized rest of my form hangs still in the left right corner a little above the taskbar where the start button resides. Visible is only the forms grey titlebar with the little "x" to close it and its caption text above the title bar. This is very weird. I set my form to "mimimized" and set not to show in taskbar and it still does. I have registered Hotkeys with my form so I may not set it to "invisible" else the hotkeys somehow cease to work even if I re-register the Hotkeys afterwards again. I found no alternative yet to remove this minimized form caption other than set it to "invisible" or removing its titlebar what I also don't want to do. I need titlebar, titlebar icon and titlebar control area in this program, the form shall not become a toolwindow or without borders.

如何在不将我的表单设置为工具窗口并且不将其设置为完全不可见的情况下,使任务栏上方表单的灰色其余部分消失.我的热键在它之后仍然可以工作,并且当我再次将其恢复正常时,表单必须仍然保留其标题栏、图标和控制区域.

How do I make this grey rest of the form above the taskbar disappearing without setting my form to a toolwindow and without setting it totally invisible. My Hotkeys must still work after it and the form must still keep its titlebar, icon and control area when I set it back to normal again.

我从这个示例.唯一的区别是我将注册热键的过程打包到一个名为ReRegisterHotkeys()"的子函数中.

I took my hotkey code from this example. The only difference is that I packed the procedure to register the hotkey into a sub-function named "ReRegisterHotkeys()".

重要:窗体最小化时显示的标题栏问题与注册的热键无关.这是一个常见的C# 问题".如果我有一个表单并将其最小化并将其设置为在任务栏中不可见它仍然显示在任务栏中带有x"的标题栏.这个我想删除而不做窗体不可见或不删除窗口样式.this.show"或this.hide"的行为与this.visible=true/false"一样致命,因为热键已经消失.我创建了默认显示的表单,但不想将其创建为隐藏状态.

Important: The issue with the titlebar showing when form is minimized is not connected with the registered hotkeys. It's a common "C# issue". If I have a form and minimize it and set it to invisible in taskbar it is still shown the titlebar with the "x" in the taskbar. This I want to remove without making the form invisible or without removing the window style. "this.show" or "this.hide" behaves the same fatal as "this.visible=true/false" since the hotkeys are gone. I create my form as shown by default and want not to create it hidden already.

这是不应该存在的 - 如何在不伤害的情况下移除它:

推荐答案

当你想隐藏时,你只需要调用 Hide()Show()并展示你的表格.注意:Hide() 也会从任务栏隐藏.

All you have to do is call Hide() and Show() when you want to hide and show your form. NOTE: Hide() will hide from taskbar as well.

private void Form1_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
        Hide();
}

您可以隐藏和显示与表单相对的 NotifyIcon,以便在显示表单时没有图标.

You may hide and show the NotifyIcon opposite to the form to not have a icon when the form is shown.

显然你需要一个 NotifyIcon 来在系统托盘中显示你的应用.

Obviously you need a NotifyIcon to display your app in the system tray.

最终您的代码将如下所示:

Finally your code will look like this:

private void Form1_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        Hide();
        notifyIcon1.Visible = true;
    }
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    Show();
    notifyIcon1.Visible = false;
    WindowState = FormWindowState.Normal;
}

这篇关于在 C# 中最小化托盘中的系统窗口窗体而不会看到它挂在任务栏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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