从任务栏打开Winform

时间:2013-01-03 13:08:37

标签: c# winforms system-tray

我有一个Winform,最初在启动时隐藏。

此后,用户可以自由地点击底部的通知图标并显示它们是否需要,当它最小化时,它需要返回系统托盘。

它从托盘开始很好 - 没问题。当你第一次点击显示它时,然后在几分之一秒内看起来它正在消失,然后又回来了。所以它看起来有点闪烁。

然后当你最小化它时,它会进入系统托盘,因为它应该没问题。当你点击再次显示它(在你完成它之后的任何时间)它会从系统托盘或任务栏滑入,我希望它只是出现,没有小动画。

public class Program : Form
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Program());
    }

    private NotifyIcon trayIcon;
    private ContextMenu trayMenu;

    public Program()
    {
        trayMenu = new ContextMenu();
        trayMenu.MenuItems.Add("Exit", OnExit);
        trayMenu.MenuItems.Add("Show", OnShow);
        trayIcon = new NotifyIcon();
        trayIcon.Text = "MyTrayApp";
        trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
        trayIcon.ContextMenu = trayMenu;
        trayIcon.Visible = true;
    }

    protected override void OnLoad(EventArgs e)
    {
        Visible = false;
        ShowInTaskbar = false;
        base.OnLoad(e);
    }

    protected override void OnResize(EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            Visible = false;
            ShowInTaskbar = false;
        }
        base.OnResize(e);
    }

    private void OnExit(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void OnShow(object sender, EventArgs e)
    {
        Visible = true;
        ShowInTaskbar = true;
        TopMost = true;
        WindowState = FormWindowState.Normal;
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            trayIcon.Dispose();
        }
        base.Dispose(isDisposing);
    }
}

关于如何让它正确显示的任何指示都将非常感激。

更新

我找到了闪烁的原因,它发生在最后设置TopMost之后,显示之后,所以它重新绘制在顶部,这是有道理的。

就目前而言,它只是让它在没有动画的情况下出现和消失。

1 个答案:

答案 0 :(得分:1)

编辑:您只应在Visibility中设置Onload()属性,只需使用Show()Hide()即可避免动画,并务必更改WindowState相应的。

我将您的代码放入常规Form(不在Program.cs文件中)并删除所有多余的代码。这就是我最终的结果,并没有显示“双重”动画。

public partial class Form1 : Form
    {
        private NotifyIcon trayIcon;
        private ContextMenu trayMenu;

        public Form1()
        {
            InitializeComponent();

            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "Program";

            trayMenu = new ContextMenu();
            trayMenu.MenuItems.Add("Exit");
            trayMenu.MenuItems.Add("Show", FormShow);
            trayIcon = new NotifyIcon();
            trayIcon.Text = "MyTrayApp";
            trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
            trayIcon.ContextMenu = trayMenu;
            trayIcon.Visible = true;
            TopMost = true;
            Resize += new EventHandler(Form1_Resize);
        }

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

        void FormShow(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }
            ShowInTaskbar = true;
            Show();
            Focus();
            trayIcon.Visible = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            Visible = false;
            ShowInTaskbar = false;
        }
    }