1,檢查窗體是否被創建
方法
foreach (var FormItem in Application.OpenForms)
{
var vForm = FormItem as 窗體類型名;
if (vForm != null)
{
vForm.Activate();
return;
}
}
2,父子窗體的設置
父窗體屬性IsMdiContainer屬性設置爲true
類型 childWin= new 子窗體類型();
childWin.MdiParent = this;
childWin.Show();
childWin.WindowState = FormWindowState.Maximized;
3,判斷程序是否運行實例
將Program.cs修改一下,添加一個互斥量
方法1:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "OnlyRun");
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("程序已經在運行!", "提示");
Application.Exit();
}
}
方法2:
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr OpenMutex(uint dwDesiredAccess, int bInheritHandle, string lpName);
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, int bInitialOwner, string lpName);