C# winform程序中,怎么判断当前程序已经打开的窗口 
是否有指定的窗体类的 实例?
如果有 就激活这个窗体类实例,没有就新建一个窗体。

解决方案 »

  1.   

     public   static   bool   SearchAllForm(string   FormName) 
                    { 
                            for   (int   i   =   0;   i   <   Application.OpenForms.Count;   i++) 
                            { 
                                    if   (Application.OpenForms[i].Name   ==   FormName) 
                                    { 
                                            return   true; 
                                    } 
                            } 
                            return   false; 
                    } 
      

  2.   

    foreach   (Form   forms   in   Application.OpenForms)                                               
    {      }public delegate bool EnumWindowsProc(IntPtr p_Handle, int p_Param);
      [DllImport("user32.dll")]
      public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
      [DllImport("User32.dll", CharSet = CharSet.Auto)]
      public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int nMaxCount);   
      [DllImport("user32.dll")]
      public static extern bool IsWindowVisible(IntPtr hWnd);
      [DllImport("user32.dll")]
      public static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, ref int lpdwProcessId);
      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
      public struct STRINGBUFFER
      {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
      public string szText;
      }  private void button1_Click(object sender, EventArgs e)
      {
      MessageWindwos();
      }  public void MessageWindwos()
      {     EnumWindowsProc _EunmWindows = new EnumWindowsProc(NetEnumWindows);
      EnumWindows(_EunmWindows, 0);   
      }
        
      private bool NetEnumWindows(IntPtr p_Handle, int p_Param)
      {
      if (!IsWindowVisible(p_Handle))return true;  STRINGBUFFER _TitleString = new STRINGBUFFER();
      GetWindowText(p_Handle, out _TitleString, 256);  MessageBox.Show(_TitleString.szText); ///获取的窗体
        
      return true;
      }