2

我有一个 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最后设置时,在显示之后,所以它在顶部重新绘制它,这是有道理的。

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

4

1 回答 1

1

编辑:您应该只在 中设置Visibility属性Onload(),只需使用Show()andHide()来避免动画,并确保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;
        }
    }
于 2013-01-03T13:29:11.323 回答