DKForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DKForm.cs
  5. * 2.功能描述:扩展的窗口:便于修改背景颜色及字体、颜色
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Drawing;
  15. using System.Windows.Forms;
  16. using Dongke.IBOSS.PRD.Basics.Library;
  17. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  18. {
  19. #region 异步处理的委托
  20. /// <summary>
  21. /// 异步处理
  22. /// </summary>
  23. /// <returns></returns>
  24. public delegate object BaseAsyncMethod();
  25. #endregion
  26. /// <summary>
  27. /// 扩展的窗口
  28. /// </summary>
  29. public partial class DKForm : Form
  30. {
  31. #region 成员变量
  32. private bool _isSaveFormSize = false;
  33. // 窗体的位置保存标识
  34. private bool _isSaveFormLocation = false;
  35. // 窗体的设置
  36. private FormSettings _settings = null;
  37. private bool _canClosing = true;
  38. #endregion
  39. #region 构造函数
  40. public DKForm()
  41. {
  42. InitializeComponent();
  43. this.Font = ControlsConst.FONT_SYSTEM_DEFAULT;
  44. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form_FormClosing);
  45. }
  46. #endregion
  47. #region 属性
  48. /// <summary>
  49. /// 窗体尺寸保存属性
  50. /// </summary>
  51. [System.ComponentModel.DefaultValue(typeof(bool), "true")]
  52. [Description("窗体关闭的时候,窗体的尺寸是否保存。")]
  53. public bool IsSaveFormSize
  54. {
  55. get
  56. {
  57. if (FormBorderStyle.Fixed3D.Equals(this.FormBorderStyle)
  58. || FormBorderStyle.FixedDialog.Equals(this.FormBorderStyle)
  59. || FormBorderStyle.FixedSingle.Equals(this.FormBorderStyle)
  60. || FormBorderStyle.FixedToolWindow.Equals(this.FormBorderStyle)
  61. )
  62. {
  63. return false;
  64. }
  65. else
  66. {
  67. return _isSaveFormSize;
  68. }
  69. }
  70. set
  71. {
  72. _isSaveFormSize = value;
  73. }
  74. }
  75. /// <summary>
  76. /// 窗体位置保存属性
  77. /// </summary>
  78. [System.ComponentModel.DefaultValue(typeof(bool), "true")]
  79. [Description("窗体关闭的时候,窗体的位置是否保存。")]
  80. public bool IsSaveFormLocation
  81. {
  82. get
  83. {
  84. return _isSaveFormLocation;
  85. }
  86. set
  87. {
  88. _isSaveFormLocation = value;
  89. }
  90. }
  91. /// <summary>
  92. /// 窗体设置内容
  93. /// </summary>
  94. [Description("窗体相关设置内容的取得。")]
  95. private FormSettings FormSetting
  96. {
  97. get
  98. {
  99. if (_settings == null)
  100. {
  101. _settings = ((FormSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new FormSettings())));
  102. _settings.SettingsKey = this.Name;
  103. }
  104. return _settings;
  105. }
  106. }
  107. #endregion
  108. #region 事件
  109. /// <summary>
  110. /// 窗口关闭
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void Form_FormClosing(object sender, FormClosingEventArgs e)
  115. {
  116. if (!this._canClosing)
  117. {
  118. e.Cancel = true;
  119. }
  120. }
  121. #endregion
  122. #region 重写From相关事件
  123. /// <summary>
  124. /// 重写Form的OnLoad事件
  125. /// </summary>
  126. /// <param name="e"></param>
  127. protected override void OnLoad(EventArgs e)
  128. {
  129. base.OnLoad(e);
  130. RestoreFormData();
  131. }
  132. /// <summary>
  133. /// 重写窗体OnShow事件
  134. /// </summary>
  135. /// <param name="e"></param>
  136. protected override void OnShown(EventArgs e)
  137. {
  138. base.OnShown(e);
  139. Point location = this.Location;
  140. if (location.X < 0)
  141. {
  142. location.X = 0;
  143. }
  144. if (location.Y < 0)
  145. {
  146. location.Y = 0;
  147. }
  148. this.Location = location;
  149. }
  150. /// <summary>
  151. /// 重写窗体OnFormClosing事件
  152. /// </summary>
  153. /// <param name="e"></param>
  154. protected override void OnFormClosing(FormClosingEventArgs e)
  155. {
  156. if (!this._canClosing)
  157. {
  158. e.Cancel = true;
  159. return;
  160. }
  161. base.OnFormClosing(e);
  162. }
  163. /// <summary>
  164. /// 重写窗体OnFormClosed事件
  165. /// </summary>
  166. /// <param name="e"></param>
  167. protected override void OnFormClosed(FormClosedEventArgs e)
  168. {
  169. SaveFormData();
  170. base.OnFormClosed(e);
  171. // 内存回收 241224
  172. GC.Collect();
  173. GC.WaitForPendingFinalizers();
  174. }
  175. #endregion
  176. #region 程序异步处理
  177. /// <summary>
  178. /// 窗体的异步处理
  179. /// </summary>
  180. /// <param name="method">异步方法</param>
  181. /// <returns></returns>
  182. public object DoAsync(BaseAsyncMethod method)
  183. {
  184. object result = null;
  185. //if (!this._canClosing)
  186. //{
  187. // return result;
  188. //}
  189. try
  190. {
  191. StartProgress();
  192. IAsyncResult asyncResult = method.BeginInvoke(null, null);
  193. while (!asyncResult.IsCompleted)
  194. {
  195. Application.DoEvents();
  196. System.Threading.Thread.Sleep(1);
  197. }
  198. result = method.EndInvoke(asyncResult);
  199. }
  200. catch (Exception ex)
  201. {
  202. throw ex;
  203. }
  204. finally
  205. {
  206. EndProgress();
  207. }
  208. return result;
  209. }
  210. #endregion
  211. #region 公有方法
  212. /// <summary>
  213. /// 取得所有控件
  214. /// </summary>
  215. /// <returns>所有的控件数组</returns>
  216. public Control[] GetControls()
  217. {
  218. return GetControls(this, null);
  219. }
  220. /// <summary>
  221. /// 取得所有控件
  222. /// </summary>
  223. /// <param name="topControl">控件</param>
  224. /// <returns>所有控件数组</returns>
  225. public Control[] GetControls(Control topControl)
  226. {
  227. return GetControls(topControl, null);
  228. }
  229. /// <summary>
  230. /// 取得所有控件
  231. /// </summary>
  232. /// <param name="type">控件类型</param>
  233. /// <returns>所有控件数组</returns>
  234. public Control[] GetControls(Type type)
  235. {
  236. return GetControls(this, type);
  237. }
  238. /// <summary>
  239. /// 取得所有控件
  240. /// </summary>
  241. /// <param name="topControl">控件类型</param>
  242. /// <param name="type">控件类型</param>
  243. /// <returns>所有控件</returns>
  244. public Control[] GetControls(Control topControl, Type type)
  245. {
  246. if (topControl == null)
  247. {
  248. return GetControls(this, type);
  249. }
  250. ArrayList list = new ArrayList();
  251. foreach (Control control in topControl.Controls)
  252. {
  253. if (type == null || type.Equals(control.GetType()))
  254. {
  255. list.Add(control);
  256. }
  257. list.AddRange(GetControls(control, type));
  258. }
  259. return (Control[])list.ToArray(typeof(Control));
  260. }
  261. /// <summary>
  262. /// 开始异步处理
  263. /// </summary>
  264. public virtual void StartProgress()
  265. {
  266. this._canClosing = false;
  267. BeginAsyncControls(this);
  268. }
  269. public virtual void BeginAsyncControls(Control control)
  270. {
  271. if (control == null)
  272. {
  273. return;
  274. }
  275. if (control is IDKControl)
  276. {
  277. (control as IDKControl).BeginAsync();
  278. return;
  279. }
  280. else
  281. {
  282. if (control.Controls == null || control.Controls.Count == 0)
  283. {
  284. //control.Enabled = false;
  285. return;
  286. }
  287. foreach (Control item in control.Controls)
  288. {
  289. BeginAsyncControls(item);
  290. }
  291. }
  292. }
  293. /// <summary>
  294. /// 结束异步处理
  295. /// </summary>
  296. public virtual void EndProgress()
  297. {
  298. this._canClosing = true;
  299. EndAsyncControls(this);
  300. }
  301. public virtual void EndAsyncControls(Control control)
  302. {
  303. if (control == null)
  304. {
  305. return;
  306. }
  307. if (control is IDKControl)
  308. {
  309. (control as IDKControl).EndAsync();
  310. return;
  311. }
  312. else
  313. {
  314. if (control.Controls == null || control.Controls.Count == 0)
  315. {
  316. //control.Enabled = false;
  317. return;
  318. }
  319. foreach (Control item in control.Controls)
  320. {
  321. EndAsyncControls(item);
  322. }
  323. }
  324. }
  325. #endregion
  326. #region 私有方法
  327. /// <summary>
  328. /// 恢复窗体数据
  329. /// </summary>
  330. private void RestoreFormData()
  331. {
  332. if ((!this.IsSaveFormSize) && (!this.IsSaveFormLocation))
  333. {
  334. return;
  335. }
  336. if (this.IsSaveFormSize && (!(FormSetting.Size.IsEmpty)))
  337. {
  338. this.Size = FormSetting.Size;
  339. }
  340. if (this.IsSaveFormLocation)
  341. {
  342. if (FormSetting.Location.IsEmpty)
  343. {
  344. this.StartPosition = FormStartPosition.CenterScreen;
  345. }
  346. else
  347. {
  348. this.Location = FormSetting.Location;
  349. }
  350. }
  351. }
  352. /// <summary>
  353. /// 保存窗体数据
  354. /// </summary>
  355. private void SaveFormData()
  356. {
  357. if ((!this.IsSaveFormSize) && (!this.IsSaveFormLocation))
  358. {
  359. return;
  360. }
  361. if (FormWindowState.Minimized.Equals(this.WindowState)
  362. || FormWindowState.Maximized.Equals(this.WindowState)
  363. )
  364. {
  365. return;
  366. }
  367. if (this.IsSaveFormSize)
  368. {
  369. FormSetting.Size = this.Size;
  370. }
  371. if (this.IsSaveFormLocation)
  372. {
  373. FormSetting.Location = this.Location;
  374. }
  375. if (this.IsSaveFormSize || this.IsSaveFormLocation)
  376. {
  377. FormSetting.Save();
  378. }
  379. }
  380. #endregion
  381. #region Form信息保存的设定类
  382. [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
  383. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("dongke.iboss.Framework.Controls.FormEx", "0.0.0.0")]
  384. internal sealed class FormSettings : global::System.Configuration.ApplicationSettingsBase
  385. {
  386. private static FormSettings _defaultInstance =
  387. ((FormSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new FormSettings())));
  388. /// <summary>
  389. /// 窗体设置的默认值
  390. /// </summary>
  391. public static FormSettings Default
  392. {
  393. get
  394. {
  395. return _defaultInstance;
  396. }
  397. }
  398. [global::System.Configuration.UserScopedSettingAttribute()]
  399. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
  400. [global::System.Configuration.DefaultSettingValueAttribute("")]
  401. public Size Size
  402. {
  403. get
  404. {
  405. return (Size)this["Size"];
  406. }
  407. set
  408. {
  409. this["Size"] = value;
  410. }
  411. }
  412. [global::System.Configuration.UserScopedSettingAttribute()]
  413. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
  414. [global::System.Configuration.DefaultSettingValueAttribute("")]
  415. public Point Location
  416. {
  417. get
  418. {
  419. return (Point)this["Location"];
  420. }
  421. set
  422. {
  423. this["Location"] = value;
  424. }
  425. }
  426. }
  427. #endregion
  428. }
  429. }