/// <summary> /// 按照包含窗口的容器控件和调整选项,调整窗口大小 /// </summary> /// <param name="parentCtrl">包含窗口的容器控件</param> /// <param name="form">要调整大小的窗口</param> /// <param name="resize">窗体自动调整大小的选项</param> public static void ResizeForm(this Control parentCtrl, Form form, EResize resize) { var scrollWidth = parentCtrl.GetScrollWidth(ScrollBars.Vertical); var scrollHeight = parentCtrl.GetScrollWidth(ScrollBars.Horizontal); if (resize == EResize.Auto) { form.Width = parentCtrl.ClientSize.Width - scrollWidth; form.Height = parentCtrl.ClientSize.Height - scrollHeight; } else if (resize == EResize.AutoWidth) { form.Width = parentCtrl.ClientSize.Width - scrollWidth; } else if (resize == EResize.AutoHeight) { form.Height = parentCtrl.ClientSize.Height - scrollHeight; } }
/// <summary> /// 向控件添加和显示窗体,隐藏窗体标题栏 /// </summary> /// <param name="parentControl">容器控件</param> /// <param name="form">显示在容器控件内的窗体</param> /// <param name="resize">自动调整窗口大小适配控件的大小的选项</param> /// <returns></returns> public static Form ShowSubForm(this Control parentControl, Form form, EResize resize = EResize.Auto) { //if (_reisze == null) //{ // _reisze = (sender, e) => // { // if (form != null && !form.IsDisposed) parentControl.ResizeForm(form, resize); // }; //} //parentControl.Resize -= _reisze; //parentControl.Resize += _reisze; form.TopLevel = false; //窗体设置成非顶级控件 form.FormBorderStyle = FormBorderStyle.None; //去掉窗体边框 form.Parent = parentControl; //指定子窗体显示的容器 parentControl.ResizeForm(form, resize); form.Refresh(); form.Show(); form.BringToFront(); return(form); }
/// <summary> /// 向控件添加和显示窗体,防止重复打开同一窗体 /// </summary> /// <param name="parentControl"></param> /// <param name="formType">要显示的窗体类型,若容器的控件集已存在同类型窗体则直接打开窗体,否则在容器控件集中创建并加入该类型新窗体再显示新窗体</param> /// <param name="args"></param> /// <param name="resize">自动调整窗口大小适配控件的大小的选项</param> /// <returns></returns> public static Form ShowSubForm(this Control parentControl, Type formType, string subFormText = null, object[] args = null, EResize resize = EResize.Auto) { var openedForm = QueryForm(parentControl.Controls.OfType <Form>(), formType, subFormText); if (openedForm == null) { openedForm = (Form)Activator.CreateInstance(formType, args); } return(parentControl.ShowSubForm(openedForm, resize)); }