DKForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. }
  172. #endregion
  173. #region 程序异步处理
  174. /// <summary>
  175. /// 窗体的异步处理
  176. /// </summary>
  177. /// <param name="method">异步方法</param>
  178. /// <returns></returns>
  179. public object DoAsync(BaseAsyncMethod method)
  180. {
  181. object result = null;
  182. //if (!this._canClosing)
  183. //{
  184. // return result;
  185. //}
  186. try
  187. {
  188. StartProgress();
  189. IAsyncResult asyncResult = method.BeginInvoke(null, null);
  190. while (!asyncResult.IsCompleted)
  191. {
  192. Application.DoEvents();
  193. System.Threading.Thread.Sleep(1);
  194. }
  195. result = method.EndInvoke(asyncResult);
  196. }
  197. catch (Exception ex)
  198. {
  199. throw ex;
  200. }
  201. finally
  202. {
  203. EndProgress();
  204. }
  205. return result;
  206. }
  207. #endregion
  208. #region 公有方法
  209. /// <summary>
  210. /// 取得所有控件
  211. /// </summary>
  212. /// <returns>所有的控件数组</returns>
  213. public Control[] GetControls()
  214. {
  215. return GetControls(this, null);
  216. }
  217. /// <summary>
  218. /// 取得所有控件
  219. /// </summary>
  220. /// <param name="topControl">控件</param>
  221. /// <returns>所有控件数组</returns>
  222. public Control[] GetControls(Control topControl)
  223. {
  224. return GetControls(topControl, null);
  225. }
  226. /// <summary>
  227. /// 取得所有控件
  228. /// </summary>
  229. /// <param name="type">控件类型</param>
  230. /// <returns>所有控件数组</returns>
  231. public Control[] GetControls(Type type)
  232. {
  233. return GetControls(this, type);
  234. }
  235. /// <summary>
  236. /// 取得所有控件
  237. /// </summary>
  238. /// <param name="topControl">控件类型</param>
  239. /// <param name="type">控件类型</param>
  240. /// <returns>所有控件</returns>
  241. public Control[] GetControls(Control topControl, Type type)
  242. {
  243. if (topControl == null)
  244. {
  245. return GetControls(this, type);
  246. }
  247. ArrayList list = new ArrayList();
  248. foreach (Control control in topControl.Controls)
  249. {
  250. if (type == null || type.Equals(control.GetType()))
  251. {
  252. list.Add(control);
  253. }
  254. list.AddRange(GetControls(control, type));
  255. }
  256. return (Control[])list.ToArray(typeof(Control));
  257. }
  258. /// <summary>
  259. /// 开始异步处理
  260. /// </summary>
  261. public virtual void StartProgress()
  262. {
  263. this._canClosing = false;
  264. BeginAsyncControls(this);
  265. }
  266. public virtual void BeginAsyncControls(Control control)
  267. {
  268. if (control == null)
  269. {
  270. return;
  271. }
  272. if (control is IDKControl)
  273. {
  274. (control as IDKControl).BeginAsync();
  275. return;
  276. }
  277. else
  278. {
  279. if (control.Controls == null || control.Controls.Count == 0)
  280. {
  281. //control.Enabled = false;
  282. return;
  283. }
  284. foreach (Control item in control.Controls)
  285. {
  286. BeginAsyncControls(item);
  287. }
  288. }
  289. }
  290. /// <summary>
  291. /// 结束异步处理
  292. /// </summary>
  293. public virtual void EndProgress()
  294. {
  295. this._canClosing = true;
  296. EndAsyncControls(this);
  297. }
  298. public virtual void EndAsyncControls(Control control)
  299. {
  300. if (control == null)
  301. {
  302. return;
  303. }
  304. if (control is IDKControl)
  305. {
  306. (control as IDKControl).EndAsync();
  307. return;
  308. }
  309. else
  310. {
  311. if (control.Controls == null || control.Controls.Count == 0)
  312. {
  313. //control.Enabled = false;
  314. return;
  315. }
  316. foreach (Control item in control.Controls)
  317. {
  318. EndAsyncControls(item);
  319. }
  320. }
  321. }
  322. #endregion
  323. #region 私有方法
  324. /// <summary>
  325. /// 恢复窗体数据
  326. /// </summary>
  327. private void RestoreFormData()
  328. {
  329. if ((!this.IsSaveFormSize) && (!this.IsSaveFormLocation))
  330. {
  331. return;
  332. }
  333. if (this.IsSaveFormSize && (!(FormSetting.Size.IsEmpty)))
  334. {
  335. this.Size = FormSetting.Size;
  336. }
  337. if (this.IsSaveFormLocation)
  338. {
  339. if (FormSetting.Location.IsEmpty)
  340. {
  341. this.StartPosition = FormStartPosition.CenterScreen;
  342. }
  343. else
  344. {
  345. this.Location = FormSetting.Location;
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// 保存窗体数据
  351. /// </summary>
  352. private void SaveFormData()
  353. {
  354. if ((!this.IsSaveFormSize) && (!this.IsSaveFormLocation))
  355. {
  356. return;
  357. }
  358. if (FormWindowState.Minimized.Equals(this.WindowState)
  359. || FormWindowState.Maximized.Equals(this.WindowState)
  360. )
  361. {
  362. return;
  363. }
  364. if (this.IsSaveFormSize)
  365. {
  366. FormSetting.Size = this.Size;
  367. }
  368. if (this.IsSaveFormLocation)
  369. {
  370. FormSetting.Location = this.Location;
  371. }
  372. if (this.IsSaveFormSize || this.IsSaveFormLocation)
  373. {
  374. FormSetting.Save();
  375. }
  376. }
  377. #endregion
  378. #region Form信息保存的设定类
  379. [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
  380. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("dongke.iboss.Framework.Controls.FormEx", "0.0.0.0")]
  381. internal sealed class FormSettings : global::System.Configuration.ApplicationSettingsBase
  382. {
  383. private static FormSettings _defaultInstance =
  384. ((FormSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new FormSettings())));
  385. /// <summary>
  386. /// 窗体设置的默认值
  387. /// </summary>
  388. public static FormSettings Default
  389. {
  390. get
  391. {
  392. return _defaultInstance;
  393. }
  394. }
  395. [global::System.Configuration.UserScopedSettingAttribute()]
  396. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
  397. [global::System.Configuration.DefaultSettingValueAttribute("")]
  398. public Size Size
  399. {
  400. get
  401. {
  402. return (Size)this["Size"];
  403. }
  404. set
  405. {
  406. this["Size"] = value;
  407. }
  408. }
  409. [global::System.Configuration.UserScopedSettingAttribute()]
  410. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
  411. [global::System.Configuration.DefaultSettingValueAttribute("")]
  412. public Point Location
  413. {
  414. get
  415. {
  416. return (Point)this["Location"];
  417. }
  418. set
  419. {
  420. this["Location"] = value;
  421. }
  422. }
  423. }
  424. #endregion
  425. }
  426. }