| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:DKForm.cs
- * 2.功能描述:扩展的窗口:便于修改背景颜色及字体、颜色
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/08/13 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- using Dongke.IBOSS.PRD.Basics.Library;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- #region 异步处理的委托
- /// <summary>
- /// 异步处理
- /// </summary>
- /// <returns></returns>
- public delegate object BaseAsyncMethod();
- #endregion
- /// <summary>
- /// 扩展的窗口
- /// </summary>
- public partial class DKForm : Form
- {
- #region 成员变量
- private bool _isSaveFormSize = false;
- // 窗体的位置保存标识
- private bool _isSaveFormLocation = false;
- // 窗体的设置
- private FormSettings _settings = null;
- private bool _canClosing = true;
- #endregion
- #region 构造函数
- public DKForm()
- {
- InitializeComponent();
- this.Font = ControlsConst.FONT_SYSTEM_DEFAULT;
- this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form_FormClosing);
- }
- #endregion
- #region 属性
- /// <summary>
- /// 窗体尺寸保存属性
- /// </summary>
- [System.ComponentModel.DefaultValue(typeof(bool), "true")]
- [Description("窗体关闭的时候,窗体的尺寸是否保存。")]
- public bool IsSaveFormSize
- {
- get
- {
- if (FormBorderStyle.Fixed3D.Equals(this.FormBorderStyle)
- || FormBorderStyle.FixedDialog.Equals(this.FormBorderStyle)
- || FormBorderStyle.FixedSingle.Equals(this.FormBorderStyle)
- || FormBorderStyle.FixedToolWindow.Equals(this.FormBorderStyle)
- )
- {
- return false;
- }
- else
- {
- return _isSaveFormSize;
- }
- }
- set
- {
- _isSaveFormSize = value;
- }
- }
- /// <summary>
- /// 窗体位置保存属性
- /// </summary>
- [System.ComponentModel.DefaultValue(typeof(bool), "true")]
- [Description("窗体关闭的时候,窗体的位置是否保存。")]
- public bool IsSaveFormLocation
- {
- get
- {
- return _isSaveFormLocation;
- }
- set
- {
- _isSaveFormLocation = value;
- }
- }
- /// <summary>
- /// 窗体设置内容
- /// </summary>
- [Description("窗体相关设置内容的取得。")]
- private FormSettings FormSetting
- {
- get
- {
- if (_settings == null)
- {
- _settings = ((FormSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new FormSettings())));
- _settings.SettingsKey = this.Name;
- }
- return _settings;
- }
- }
- #endregion
- #region 事件
- /// <summary>
- /// 窗口关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Form_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!this._canClosing)
- {
- e.Cancel = true;
- }
- }
- #endregion
- #region 重写From相关事件
- /// <summary>
- /// 重写Form的OnLoad事件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- RestoreFormData();
- }
- /// <summary>
- /// 重写窗体OnShow事件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnShown(EventArgs e)
- {
- base.OnShown(e);
- Point location = this.Location;
- if (location.X < 0)
- {
- location.X = 0;
- }
- if (location.Y < 0)
- {
- location.Y = 0;
- }
- this.Location = location;
- }
- /// <summary>
- /// 重写窗体OnFormClosing事件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnFormClosing(FormClosingEventArgs e)
- {
- if (!this._canClosing)
- {
- e.Cancel = true;
- return;
- }
- base.OnFormClosing(e);
- }
- /// <summary>
- /// 重写窗体OnFormClosed事件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnFormClosed(FormClosedEventArgs e)
- {
- SaveFormData();
- base.OnFormClosed(e);
- // 内存回收 241224
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- #endregion
- #region 程序异步处理
- /// <summary>
- /// 窗体的异步处理
- /// </summary>
- /// <param name="method">异步方法</param>
- /// <returns></returns>
- public object DoAsync(BaseAsyncMethod method)
- {
- object result = null;
- //if (!this._canClosing)
- //{
- // return result;
- //}
- try
- {
- StartProgress();
- IAsyncResult asyncResult = method.BeginInvoke(null, null);
- while (!asyncResult.IsCompleted)
- {
- Application.DoEvents();
- System.Threading.Thread.Sleep(1);
- }
- result = method.EndInvoke(asyncResult);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- EndProgress();
- }
- return result;
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 取得所有控件
- /// </summary>
- /// <returns>所有的控件数组</returns>
- public Control[] GetControls()
- {
- return GetControls(this, null);
- }
- /// <summary>
- /// 取得所有控件
- /// </summary>
- /// <param name="topControl">控件</param>
- /// <returns>所有控件数组</returns>
- public Control[] GetControls(Control topControl)
- {
- return GetControls(topControl, null);
- }
- /// <summary>
- /// 取得所有控件
- /// </summary>
- /// <param name="type">控件类型</param>
- /// <returns>所有控件数组</returns>
- public Control[] GetControls(Type type)
- {
- return GetControls(this, type);
- }
- /// <summary>
- /// 取得所有控件
- /// </summary>
- /// <param name="topControl">控件类型</param>
- /// <param name="type">控件类型</param>
- /// <returns>所有控件</returns>
- public Control[] GetControls(Control topControl, Type type)
- {
- if (topControl == null)
- {
- return GetControls(this, type);
- }
- ArrayList list = new ArrayList();
- foreach (Control control in topControl.Controls)
- {
- if (type == null || type.Equals(control.GetType()))
- {
- list.Add(control);
- }
- list.AddRange(GetControls(control, type));
- }
- return (Control[])list.ToArray(typeof(Control));
- }
- /// <summary>
- /// 开始异步处理
- /// </summary>
- public virtual void StartProgress()
- {
- this._canClosing = false;
- BeginAsyncControls(this);
- }
- public virtual void BeginAsyncControls(Control control)
- {
- if (control == null)
- {
- return;
- }
- if (control is IDKControl)
- {
- (control as IDKControl).BeginAsync();
- return;
- }
- else
- {
- if (control.Controls == null || control.Controls.Count == 0)
- {
- //control.Enabled = false;
- return;
- }
- foreach (Control item in control.Controls)
- {
- BeginAsyncControls(item);
- }
- }
- }
- /// <summary>
- /// 结束异步处理
- /// </summary>
- public virtual void EndProgress()
- {
- this._canClosing = true;
- EndAsyncControls(this);
- }
- public virtual void EndAsyncControls(Control control)
- {
- if (control == null)
- {
- return;
- }
- if (control is IDKControl)
- {
- (control as IDKControl).EndAsync();
- return;
- }
- else
- {
- if (control.Controls == null || control.Controls.Count == 0)
- {
- //control.Enabled = false;
- return;
- }
- foreach (Control item in control.Controls)
- {
- EndAsyncControls(item);
- }
- }
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 恢复窗体数据
- /// </summary>
- private void RestoreFormData()
- {
- if ((!this.IsSaveFormSize) && (!this.IsSaveFormLocation))
- {
- return;
- }
- if (this.IsSaveFormSize && (!(FormSetting.Size.IsEmpty)))
- {
- this.Size = FormSetting.Size;
- }
- if (this.IsSaveFormLocation)
- {
- if (FormSetting.Location.IsEmpty)
- {
- this.StartPosition = FormStartPosition.CenterScreen;
- }
- else
- {
- this.Location = FormSetting.Location;
- }
- }
- }
- /// <summary>
- /// 保存窗体数据
- /// </summary>
- private void SaveFormData()
- {
- if ((!this.IsSaveFormSize) && (!this.IsSaveFormLocation))
- {
- return;
- }
- if (FormWindowState.Minimized.Equals(this.WindowState)
- || FormWindowState.Maximized.Equals(this.WindowState)
- )
- {
- return;
- }
- if (this.IsSaveFormSize)
- {
- FormSetting.Size = this.Size;
- }
- if (this.IsSaveFormLocation)
- {
- FormSetting.Location = this.Location;
- }
- if (this.IsSaveFormSize || this.IsSaveFormLocation)
- {
- FormSetting.Save();
- }
- }
- #endregion
- #region Form信息保存的设定类
- [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("dongke.iboss.Framework.Controls.FormEx", "0.0.0.0")]
- internal sealed class FormSettings : global::System.Configuration.ApplicationSettingsBase
- {
- private static FormSettings _defaultInstance =
- ((FormSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new FormSettings())));
- /// <summary>
- /// 窗体设置的默认值
- /// </summary>
- public static FormSettings Default
- {
- get
- {
- return _defaultInstance;
- }
- }
- [global::System.Configuration.UserScopedSettingAttribute()]
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
- [global::System.Configuration.DefaultSettingValueAttribute("")]
- public Size Size
- {
- get
- {
- return (Size)this["Size"];
- }
- set
- {
- this["Size"] = value;
- }
- }
- [global::System.Configuration.UserScopedSettingAttribute()]
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
- [global::System.Configuration.DefaultSettingValueAttribute("")]
- public Point Location
- {
- get
- {
- return (Point)this["Location"];
- }
- set
- {
- this["Location"] = value;
- }
- }
- }
- #endregion
- }
- }
|