查找是否已创建窗体

private void button1_Click(object sender, EventArgs e)
        {

            if (SearchForm(new Form2().Text) == false)
            {
                Form2 frm2 = new Form2();
                frm2.Show();
            }
            else
            {
                Application.OpenForms["Form2"].Focus();
            }
           
        }

 [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")] //找子窗体
        private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("User32.dll", EntryPoint = "SendMessage")] //用于发送信息给窗体
        private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
        /// <summary>
        /// 查找是否已创建窗体
        /// </summary>
        /// <param name="FormTitle">标题名称</param>
        /// <returns></returns>
        public static bool SearchForm(string FormTitle)
        {
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr EdithWnd = new IntPtr(0);

            //查到窗体,得到整个窗体
            ParenthWnd = FindWindow(null,FormTitle);
            if (!ParenthWnd.Equals(IntPtr.Zero))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

posted @ 2006-08-30 16:31  DotNet菜园  阅读(489)  评论(0)    收藏  举报