[英]how to show already instantiated (running) class Form1 from another class (ApplicationContext)
I have a class for Application Context and in it, among other things i have some methods to send a Form1 class to system-tray, my problem is how to show already instantiated class From1 while still running in system-tray, example: 我有一个用于Application Context的类,除其他外,我还有一些方法可以将Form1类发送到系统托盘,我的问题是如何显示已实例化的类From1,同时仍在系统托盘中运行,例如:
class MyApplicationContext : ApplicationContext
{
private NotifyIcon TrayIcon;
private ContextMenuStrip TrayIconContextMenu;
private ToolStripMenuItem CloseMenuItem;
private void TrayIcon_DoubleClick(object sender, EventArgs e)
{
// How to show Form1 hire.
}
//...
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//...
}
If i instantiate a new class it will give me the new form and all my running processes will be gone example: 如果我实例化一个新类,它将给我新的形式,而我所有正在运行的进程都将消失,例如:
private void TrayIcon_DoubleClick(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.Show(); //How to show my form?
}
Not sure if prototype pattern will work here and is there an easier solution? 不知道原型模式是否可以在这里工作,有没有更简单的解决方案?
This is my class: 这是我的课:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Windows;
namespace DJBackupDB
{
class MyApplicationContext : ApplicationContext
{
//Component declarations
private NotifyIcon TrayIcon;
private ContextMenuStrip TrayIconContextMenu;
private ToolStripMenuItem CloseMenuItem;
public static Form1 form;
public MyApplicationContext()
{
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
InitializeComponent();
TrayIcon.Visible = true;
}
private void InitializeComponent()
{
TrayIcon = new NotifyIcon();
TrayIcon.BalloonTipIcon = ToolTipIcon.Info;
TrayIcon.BalloonTipText =
"I noticed that you double-clicked me! What can I do for you?";
TrayIcon.BalloonTipTitle = "You called Master?";
TrayIcon.Text = "My fabulous tray icon demo application";
//The icon is added to the project resources.
//Here I assume that the name of the file is 'TrayIcon.ico'
TrayIcon.Icon = Properties.Resources.logo;
//Optional - handle doubleclicks on the icon:
TrayIcon.DoubleClick += TrayIcon_DoubleClick;
//Optional - Add a context menu to the TrayIcon:
TrayIconContextMenu = new ContextMenuStrip();
CloseMenuItem = new ToolStripMenuItem();
TrayIconContextMenu.SuspendLayout();
//
// TrayIconContextMenu
//
this.TrayIconContextMenu.Items.AddRange(new ToolStripItem[] {
this.CloseMenuItem});
this.TrayIconContextMenu.Name = "TrayIconContextMenu";
this.TrayIconContextMenu.Size = new Size(153, 70);
//
// CloseMenuItem
//
this.CloseMenuItem.Name = "CloseMenuItem";
this.CloseMenuItem.Size = new Size(152, 22);
this.CloseMenuItem.Text = "Close the tray icon program";
this.CloseMenuItem.Click += new EventHandler(this.CloseMenuItem_Click);
TrayIconContextMenu.ResumeLayout(false);
TrayIcon.ContextMenuStrip = TrayIconContextMenu;
}
private void OnApplicationExit(object sender, EventArgs e)
{
//Cleanup so that the icon will be removed when the application is closed
TrayIcon.Visible = false;
}
private void TrayIcon_DoubleClick(object sender, EventArgs e)
{
form.Show();
}
private void CloseMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to close me?",
"Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
You'll have to store your form object somewhere you can access it in the event. 您必须将表单对象存储在可以在事件中访问它的地方。 Make a class member field for the form just like you have for the NotifyIcon and other things. 就像为NotifyIcon和其他东西一样,为表单创建一个类成员字段。
private Form1 form;
Then when you create your ApplicationContext, set the form field to the Form1 object. 然后,当您创建ApplicationContext时,将表单字段设置为Form1对象。
Then inside the click event handler, you can just call: 然后在click事件处理程序中,您可以调用:
this.form.Show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.