在不新开程序的情况下Program.cs里,如何控制form1里的控件值或调用函数呢?请帮小菜改改
Program.cs文件代码
namespace WindowsFormsApp1
{
public delegate void ChangeFormColor(string topmost);
static class Program
{
private static Mutex run = null;
public static Form1 appForm;
public static event ChangeFormColor ChangeColor;
/* public static string strPath;
<summary>
应用程序的主入口点。
</summary>*/
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
/*检查程序是否已经启动,防止多开程序*/
bool runone;
run = new Mutex(true, @"GlobalMutexSampleApp", out runone);
try
{
if (runone)
{
if (args.Length > 0)
{
appForm = new Form1(args[0]);
Application.Run(appForm);
}
else
{
appForm = new Form1();
Application.Run(appForm);
}
}
else
{
ChangeColor("form1修改标题成功");
}
}
finally
{
/*只有第一个实例获得控制权,因此只有在这种情况下才需要ReleaseMutex,否则会引发异常。*/
if (runone)
{
run.ReleaseMutex();
}
run.Close();
run = null;
}
}
}
}
form1里的代码-
public Form1()
{
f = this;
InitializeComponent();
Program.ChangeColor += new ChangeFormColor(this.f_ChangeColor);
}
public Form1(string path)
{
f = this;
playPath = path;
InitializeComponent();
Program.ChangeColor += new ChangeFormColor(this.f_ChangeColor);
}
void f_ChangeColor(string topmost)
{
f.Text = topmost;
}