最小化的窗口形成系统托盘

时间:2014-01-15 10:09:55

标签: c# .net winforms

以下是我失败的代码:

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

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

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

}

当我点击最小化时,它仍会显示在我的任务栏上。你们知道什么是错的吗?

4 个答案:

答案 0 :(得分:0)

使用ShowInTaskbar属性从任务栏中删除表单:

private void MainForm_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)        
        ShowInTaskbar = false;        
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    ShowInTaskbar = true;
    WindowState = FormWindowState.Normal;
}

答案 1 :(得分:0)

感谢:http://developerpr.wordpress.com/2012/02/06/c-handle-minimize-maximize-restore-in-windows-forms-before-it-happens/

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    var msg = '\x112';
    const int minimize = '\xf020';
    const int maximize = '\xf030';
    const int restore = '\xf120';

    if (m.Msg == msg)
    {
        Console.WriteLine(m.ToString());
        var param = m.WParam.ToInt32();
        switch (param)
        {
            case minimize:
                // handle minimize             
                ShowInTaskbar = false;
                break;
            case maximize:
                // handle maximize             
                break;
            case restore:
                // handle restore      
                ShowInTaskbar = true;
                break;
        }
    }
}

请注意,我更改了调用base.WndProc并处理消息的顺序。否则,首次点击会取消最小化。

答案 2 :(得分:0)

我发现了什么是错的。我在Form1.Designer.cs文件中缺少一个事件处理程序声明。 我在Form1下面添加了以下内容,它可以工作:

this.Resize += new System.EventHandler(this.Form1_Resize);

答案 3 :(得分:0)

请尝试

this.ShowInTaskbar = false;

和show()

this.ShowInTaskbar = true;