C# WindowsForm程序同时启动多个窗口类

C# WindowsForm程序同时启动多个窗口类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MVCProject
{
    /// <summary>
    /// 多窗口同时启动类
    /// <remarks>继承ApplicationContext的原因是Application.Run(ApplicationContext context);参数的需要</remarks>
    /// <remarks>另一个是关闭同时启动的窗口</remarks>
    /// </summary>
    class MultiFormApplictionStart : ApplicationContext
    {
        private void onFormClosed(object sender, EventArgs e)
        {
            if (Application.OpenForms.Count == 0)
            {
                ExitThread();
            }
        }
        public MultiFormApplictionStart()
        {
            /*
             *里面添加启动的窗口
             */
            var formList = new List<Form>(){
            new DJControl(), 
            new DJView()
        };
            foreach (var item in formList)
            {
                item.FormClosed += onFormClosed;
            }
            foreach (var item in formList)
            {
                item.Show();
            }
        }
    }
}

最后在Program的类中调用这个类即可

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MultiFormApplictionStart());
        }
    }

运行后的截图如下:

这里写图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页