C#学习笔记-WinForm

来源:互联网 发布:一直提示安装java 编辑:程序博客网 时间:2024/05/19 10:35

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);
0 0
原创粉丝点击