Minimizing a system windows form in tray in C# without seeing it hanging where taskbar

旧巷老猫 提交于 2019-12-08 10:28:48

问题


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();

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.

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()".

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.

This is what shall be not there - how to remove it without hurting:


回答1:


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();
}

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

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;
}


来源:https://stackoverflow.com/questions/17641162/minimizing-a-system-windows-form-in-tray-in-c-sharp-without-seeing-it-hanging-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!