九溪

溪水润知林,滴露启慧心

用户工具

站点工具


wiki:csharp:winform-gzty-ctdkgll

规则统一的窗体打开管理类,限制重复打开窗体

formsmanager.cs
using System;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;
//using FD3DS.Forms;
 
namespace EarthView.Method
{
    class FormsManager
    {
        #region 变量
        /// <summary>
        /// 当前已经打开的窗口对象列表
        /// </summary>
        static ArrayList FormList = new ArrayList();
        #endregion
 
        #region 创建窗体
        ///// <summary>
        ///// 打开图层管理器
        ///// </summary>
        ///// <returns></returns>
        //public static frmLayerManager OpenFrmLayerManager(IWin32Window ower)
        //{
        //    Object frm = OpenForm(typeof(frmLayerManager), ower);
        //    return frm as frmLayerManager;
        //} 
        #endregion
        #region 关闭窗体
        /// <summary>
        /// 关闭窗体时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void ClosingForm(object sender, FormClosingEventArgs e)
        {
            FormList.Remove(sender);
        }
        /// <summary>
        /// 关闭当前所有的窗体
        /// </summary>
        public static void CloseAllForm()
        {
            for (int i = 0; i < FormList.Count; i++)
            {
                if (FormList[i] is Form)
                {
                    Form frm = FormList[i] as Form;
                    frm.Dispose();
                    frm = null;
                    FormList.RemoveAt(i);
                    i--;
                }
            }
        }
 
        #endregion
        #region 已打开窗体检测
        /// <summary>
        /// 查看已打开的窗口是否包括该名称的对象
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        public static bool IsFormExist(ref object form, string formName)
        {
            bool opened = false;
            foreach (object frm in FormList)
            {
                if (!(frm is Form))
                    continue;
 
                if ((frm as Form).Name == formName)
                {
                    opened = true;
                    form = frm;
                    break;
                }
            }
            return opened;
        }
        #endregion
        #region	打开窗体复用代码
        /// <summary>
        /// 打开窗体
        /// </summary>
        /// <param name="objType">窗体对象类型,如: typeof(Form)</param>
        /// <param name="construct_types">构造函数所需类型集合,如: new Type[] { typeof(string), typeof(int) }</param>
        /// <param name="construct_values">构造函数传递参数集合,如: new Object[] { "helloworld", 101 }</param>
        /// <param name="ower">窗体拥有者</param>
        /// <param name="flag">窗体唯一标示,可不填,默认使用窗体类型作为标示</param>
        /// <returns></returns>
        public static Object OpenForm(Type objType, IWin32Window ower, Type[] construct_types=null, object[] construct_values = null, string flag = "")
        {
            #region 判断
            //窗体名称
            if (string.IsNullOrEmpty(flag)) flag = objType.ToString();
            //构造函数类型
            if (construct_types == null)
                construct_types = new Type[] { };
            //构造函数传递参数
            if (construct_values == null)
                construct_values = new object[] { };
            //类型与传递值须一致
            if (construct_types.Length != construct_values.Length)
            { construct_types = new Type[] { }; construct_values = new object[] { }; }
            #endregion
 
            Type type = objType;
            if (type == null)
                return null;
 
            object obj = null;
            object getObj = null;
            //构造
            ConstructorInfo construct = type.GetConstructor(construct_types);
 
            if (IsFormExist(ref getObj, flag))
            {
                obj = getObj;
                //窗体是否已经关闭
                object _isDisposed = type.GetProperty("IsDisposed").GetValue(obj, null);
                if (_isDisposed.ToString().ToLower() == "true")
                {
                    obj = construct.Invoke(construct_values);//构造
                    FormList.Remove(getObj);
                    FormList.Add(obj);
                }
            }
            else
            {
                obj = construct.Invoke(construct_values);//构造
                FormList.Add(obj);
            }
            //名称
            type.GetProperty("Name").SetValue(obj, flag, null);
            //允许透明
            type.GetProperty("AllowTransparency").SetValue(obj, true, null);
            //透明度
            type.GetProperty("Opacity").SetValue(obj, 0.8d, null);
            //启动位置
            type.GetProperty("StartPosition").SetValue(obj, FormStartPosition.CenterScreen, null);
            //状态栏显示
            type.GetProperty("ShowInTaskbar").SetValue(obj, false, null);
            object _visible = type.GetProperty("Visible").GetValue(obj, null);
            //使窗体显示
            if (_visible.ToString().ToLower() == "false")
            { type.GetMethod("Show", new Type[] { typeof(IWin32Window) }).Invoke(obj, new Object[] { ower }); }
            //激活窗体
            type.GetMethod("Activate", new Type[] { }).Invoke(obj, new Object[] { });
            return obj;
        }
        /// <summary>
        /// 打开窗体示例
        /// </summary>
        /// <returns></returns>
        public static Form OpenFrmOldMethod(IWin32Window ower)
        {
            Form frm = null;
            object getfrm = null;
            if (IsFormExist(ref getfrm, "Form1"))
            {
                frm = getfrm as Form;
                if (frm.IsDisposed)
                {
                    frm = new Form();
                    FormList.Remove(getfrm);
                    FormList.Add(frm);
                }
            }
            else
            {
                frm = new Form();
                FormList.Add(frm);
            }
            frm.Name = "Form1";
            frm.AllowTransparency = true;
            frm.Opacity = 0.8;
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowInTaskbar = false;
            if (!frm.Visible)
                frm.Show(ower);
            return frm;
        }
        /// <summary>
        /// 打开窗体新示例
        /// </summary>
        /// <returns></returns>
        public static Form OpenFrmNewMethod(IWin32Window ower)
        {
            //构造函数无参数
            Object frm = OpenForm(typeof(Form), ower);
            return frm as Form;
 
            ////构造函数有参数
            //Object frm2 = OpenForm(typeof(Form), ower, 
            //    new Type[] { typeof(int), typeof(string) }, 
            //    new object[] { 101, "hello world" }, 
            //    "钥匙A");
            //return frm2 as Form;
        }
		#endregion 
    }
}

评论

请输入您的评论. 可以使用维基语法:
 
wiki/csharp/winform-gzty-ctdkgll.txt · 最后更改: 2023/01/03 15:25 由 127.0.0.1