获取应用程序所有打开的窗口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CH5
{
    public partial class CH5_DemoForm001 : Form
    {
        public CH5_DemoForm001()
        {
            InitializeComponent();
        }

        private void CH5_DemoForm001_Load(object sender, EventArgs e)
        {
            RefreshOpenFormCount();
        }

        private void btnGetOpenForms_Click(object sender, EventArgs e)
        {
            // 开启数个窗体并将它们最小化。
           CH5_DemoForm002 myCH5_DemoForm002 = new CH5_DemoForm002();
            myCH5_DemoForm002.Show();
            myCH5_DemoForm002.WindowState = FormWindowState.Minimized;

            CH5_DemoForm004 myCH5_DemoForm004 = new CH5_DemoForm004();
            myCH5_DemoForm004.Show();
            myCH5_DemoForm004.WindowState = FormWindowState.Minimized;

            CH5_DemoForm005 myCH5_DemoForm005 = new CH5_DemoForm005();
            myCH5_DemoForm005.Show();
            myCH5_DemoForm005.WindowState = FormWindowState.Minimized;

            // 取得所有已开启之窗体的标题。
            GetOpenFormTitles();

            // 取得已开启的窗体数目。
            RefreshOpenFormCount();
        }

        private void GetOpenFormTitles()
        {
            try
            {
                string myFormTitle;

                foreach (Form f in Application.OpenForms)
                {
                    // 使用一个进程安全的方法来取得所有已打开的窗体的标题。
                    myFormTitle = GetFormTitle(f);

                    if (!(this.ListBox1.Items.Contains(myFormTitle)))
                    {
                        this.ListBox1.Items.Add(myFormTitle);
                    }
                }
            }
            catch (Exception ex)
            {
                this.ListBox1.Items.Add("错误:" + ex.Message);
            }
        }

        private delegate string GetFormTitleDelegate(Form f);
        private string GetFormTitle(Form f)
        {
            // 检查窗体是否能够从目前的进程来访问。
            if (!(f.InvokeRequired))
            {
                // 直接访问窗体。
                return f.Text;
            }
            else
            {
                // 封送处理至拥有窗体的进程。
                GetFormTitleDelegate del = GetFormTitle;
                object[] param = { f };
                System.IAsyncResult result = f.BeginInvoke(del, param);

                // 让窗体的进程有机会来处理函数。
                System.Threading.Thread.Sleep(10);

                // 检查结果。
                if (result.IsCompleted)
                {
                    // 取得函式的传回值。
                    return "不同的进程:" + f.EndInvoke(result).ToString();
                }
                else
                {
                    return "没有响应的进程";
                }
            }
        }

        private void btnCloseOpenForms_Click(object sender, EventArgs e)
        {
            try
            {
                System.Collections.Generic.List<Form> myList = new System.Collections.Generic.List<Form>(Application.OpenForms.Count - 2);

                foreach (Form f in Application.OpenForms)
                {
                    if (f.Name != "StartUpForm" && f.Name != "CH5_DemoForm001")
                    {
                        myList.Add(f);
                    }
                }

                foreach (Form f in myList)
                {
                    this.ListBox1.Items.Remove(f.Text);
                    f.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            RefreshOpenFormCount();
        }

        private void RefreshOpenFormCount()
        {
            this.lblOpenFormsCount.Text = Application.OpenForms.Count.ToString();
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

作者:wpf之家
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/wpf123/p/2052961.html