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


    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)
            {

            }
        }
    }

  • 相关阅读:
    java监听者模式
    使用tc编写流量控制脚本
    Android apk集成
    就这样
    嘴不笨来试试??太好玩儿了,看看谁厉害?
    老板的三句话
    电脑设置wifi
    JDBC
    使用git的一般操作
    模板引擎Velocity学习系列
  • 原文地址:https://www.cnblogs.com/zhangtao/p/1756020.html
Copyright © 2020-2023  润新知