/*******************************************************************************
* 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 异步处理的委托
///
/// 异步处理
///
///
public delegate object BaseAsyncMethod();
#endregion
///
/// 扩展的窗口
///
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 属性
///
/// 窗体尺寸保存属性
///
[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;
}
}
///
/// 窗体位置保存属性
///
[System.ComponentModel.DefaultValue(typeof(bool), "true")]
[Description("窗体关闭的时候,窗体的位置是否保存。")]
public bool IsSaveFormLocation
{
get
{
return _isSaveFormLocation;
}
set
{
_isSaveFormLocation = value;
}
}
///
/// 窗体设置内容
///
[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 事件
///
/// 窗口关闭
///
///
///
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if (!this._canClosing)
{
e.Cancel = true;
}
}
#endregion
#region 重写From相关事件
///
/// 重写Form的OnLoad事件
///
///
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
RestoreFormData();
}
///
/// 重写窗体OnShow事件
///
///
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;
}
///
/// 重写窗体OnFormClosing事件
///
///
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!this._canClosing)
{
e.Cancel = true;
return;
}
base.OnFormClosing(e);
}
///
/// 重写窗体OnFormClosed事件
///
///
protected override void OnFormClosed(FormClosedEventArgs e)
{
SaveFormData();
base.OnFormClosed(e);
// 内存回收 241224
GC.Collect();
GC.WaitForPendingFinalizers();
}
#endregion
#region 程序异步处理
///
/// 窗体的异步处理
///
/// 异步方法
///
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 公有方法
///
/// 取得所有控件
///
/// 所有的控件数组
public Control[] GetControls()
{
return GetControls(this, null);
}
///
/// 取得所有控件
///
/// 控件
/// 所有控件数组
public Control[] GetControls(Control topControl)
{
return GetControls(topControl, null);
}
///
/// 取得所有控件
///
/// 控件类型
/// 所有控件数组
public Control[] GetControls(Type type)
{
return GetControls(this, type);
}
///
/// 取得所有控件
///
/// 控件类型
/// 控件类型
/// 所有控件
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));
}
///
/// 开始异步处理
///
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);
}
}
}
///
/// 结束异步处理
///
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 私有方法
///
/// 恢复窗体数据
///
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;
}
}
}
///
/// 保存窗体数据
///
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())));
///
/// 窗体设置的默认值
///
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
}
}