简体   繁体   English

系统托盘图标

[英]System Tray Icon

Okay firstly I just started C# so I'm not exactly the most skilled programmer out there. 好吧,首先我刚开始使用C#,所以我不是那里最熟练的程序员。 Okay so here's my problem that may seem stupid to you guys ;) 好的,所以这是我的问题,对你们来说可能看起来很愚蠢;)

I have a simple enough app that a friend asked me to do. 我有一个简单的应用程序,朋友邀请我这样做。 So far I have managed with a bit of Google but I'm stuck with this. 到目前为止,我已经管理了一些谷歌,但我坚持这一点。 The app runs fine and minimizes to the system tray and maximizes from the system tray which is good. 该应用程序运行良好,最小化到系统托盘,并从系统托盘最大化,这是好的。 However, when I open a second form from that application it creates another icon in the system tray and starts duplicating every time I open another form. 但是,当我从该应用程序打开第二个表单时,它会在系统托盘中创建另一个图标,并在每次打开另一个表单时开始复制。 So eventually I have lots of icons and all of them are seperate instances of the main form. 所以最终我有很多图标,所有图标都是主窗体的单独实例。 System Tray events 系统托盘事件

private void notifyIcon_systemTray_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (FormWindowState.Minimized == WindowState)
    {
        Show();
        WindowState = FormWindowState.Normal;
    }
}
private void CronNecessityForm_Resize(object sender, EventArgs e)
{
    notifyIcon_systemTray.Visible = true;
    if (FormWindowState.Minimized == WindowState)
        Hide();

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

To open the Form: 打开表格:

private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
    CronPreferences.formPreferences CronPreferences = new CronPreferences.formPreferences();
    CronPreferences.Show();
}

Close it: 关闭它:

private void button2_Click(object sender, EventArgs e)
{
     this.Hide();
}

How can I have all Forms map to the same icon in the System Tray? 如何将所有表单映射到系统托盘中的相同图标?

You will need a single global tray icon that they all access. 您将需要一个他们都可以访问的单个全局托盘图标。 Do this by using a static variable that stays the same throughout different instances of the class. 通过使用在类的不同实例中保持不变的静态变量来执行此操作。

Then, if you want to: 然后,如果你想:

  • Open one form: keep a reference to the latest form in a variable and open it. 打开一个表单:在变量中保留对最新表单的引用并打开它。
  • Open all minimised forms: iterate through each form and open them again. 打开所有最小化的表单:遍历每个表单并再次打开它们。

If I got it right, you want to keep only a single instance of your application running. 如果我做对了,你只想保持应用程序一个实例运行。 In that case, your title is a bit misleading since your problem has nothing to do with tray icons or multiple forms. 在这种情况下,您的标题有点误导,因为您的问题与托盘图标或多个表单无关。

On the other hand, if you really have a main form in your app, which opens the second form (which creates a tray icon), in that case you simply need to make sure your second form is instantiated only once: 另一方面,如果您的应用程序中确实有一个主窗体 ,它会打开第二个窗体 (创建托盘图标),在这种情况下,您只需要确保第二个窗体仅实例化一次:

public class MainForm
{
    private SecondForm _secondForm;

    public void OpenSecondForm()
    {
         // create it only once
         if (_secondForm == null)
             _secondForm = new SecondForm();

         // otherwise just show it
         _secondForm.Show();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM