FormBase.cs 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using Dongke.WinForm.Utilities;
  7. namespace Dongke.WinForm.Controls
  8. {
  9. /// <summary>
  10. /// 窗体基类
  11. /// </summary>
  12. public partial class FormBase : Form, IDKForm
  13. {
  14. #region 事件声明
  15. #region AsyncBegin
  16. /// <summary>
  17. /// 当异步处理开始时发生。
  18. /// </summary>
  19. private static readonly object EventAsyncBegin = new object();
  20. /// <summary>
  21. /// 当异步处理开始时发生。
  22. /// </summary>
  23. [Description("当异步处理开始时发生。"), Category("CustomerEx")]
  24. public event CancelEventHandler AsyncBegin
  25. {
  26. add
  27. {
  28. base.Events.AddHandler(EventAsyncBegin, value);
  29. }
  30. remove
  31. {
  32. base.Events.RemoveHandler(EventAsyncBegin, value);
  33. }
  34. }
  35. /// <summary>
  36. /// 引发 异步处理开始 事件
  37. /// </summary>
  38. /// <param name="e">包含事件数据的 EventArgs</param>
  39. protected virtual void OnAsyncBegin(CancelEventArgs e)
  40. {
  41. CancelEventHandler eventHandler = (CancelEventHandler)base.Events[EventAsyncBegin];
  42. if (eventHandler != null)
  43. {
  44. eventHandler(this, e);
  45. }
  46. }
  47. #endregion
  48. #region AsyncEnd
  49. /// <summary>
  50. /// 当异步处理结束时发生。
  51. /// </summary>
  52. private static readonly object EventAsyncEnd = new object();
  53. /// <summary>
  54. /// 当异步处理结束时发生。
  55. /// </summary>
  56. [Description("当异步处理开始时发生。"), Category("CustomerEx")]
  57. public event AsyncEndEventHandler AsyncEnd
  58. {
  59. add
  60. {
  61. base.Events.AddHandler(EventAsyncEnd, value);
  62. }
  63. remove
  64. {
  65. base.Events.RemoveHandler(EventAsyncEnd, value);
  66. }
  67. }
  68. /// <summary>
  69. /// 引发 异步处理结束 事件
  70. /// </summary>
  71. /// <param name="e">包含事件数据的 EventArgs</param>
  72. protected virtual void OnAsyncEnd(AsyncEndEventArgs e)
  73. {
  74. AsyncEndEventHandler eventHandler = (AsyncEndEventHandler)base.Events[EventAsyncEnd];
  75. if (eventHandler != null)
  76. {
  77. eventHandler(this, e);
  78. }
  79. }
  80. #endregion
  81. #endregion
  82. #region 成员变量
  83. /// <summary>
  84. /// 异步等处理时,能否关闭窗体
  85. /// </summary>
  86. private bool _canClose = true;
  87. private bool _isClosing = false;
  88. private bool _isSaving = false;
  89. /// <summary>
  90. /// 是否取消异步处理。
  91. /// </summary>
  92. private bool _cancelAsync = false;
  93. /// <summary>
  94. /// 正在异步处理。
  95. /// </summary>
  96. private bool _doAsync = false;
  97. /// <summary>
  98. /// 窗体中数据是否被修改过
  99. /// </summary>
  100. private bool _isDirty = false;
  101. private bool _showStatusStrip = true;
  102. /// <summary>
  103. /// 窗体类型
  104. /// </summary>
  105. private FormType _formType = FormType.Select;
  106. private FormWindowState _formWindowState = FormWindowState.Normal;
  107. private string _formCode = null;
  108. private bool _doFocus = true;
  109. private int _loadCount = 0;
  110. #endregion
  111. #region 构造函数
  112. /// <summary>
  113. /// 窗体基类
  114. /// </summary>
  115. protected FormBase()
  116. : this(null)
  117. {
  118. }
  119. /// <summary>
  120. /// 窗体基类
  121. /// </summary>
  122. protected FormBase(string formCode)
  123. {
  124. InitializeComponent();
  125. this.KeyPreview = true;
  126. this._formCode = formCode;
  127. if (this._showStatusStrip)
  128. {
  129. this.Controls.Add(this.ssrAsyncStatusStrip);
  130. }
  131. else
  132. {
  133. this.Controls.Remove(this.ssrAsyncStatusStrip);
  134. }
  135. this.ssrAsyncStatusStrip.Visible = this._showStatusStrip;
  136. }
  137. #endregion
  138. #region 属性
  139. /// <summary>
  140. /// 获取一个值,该值指示窗体的编码(多实例窗体编码不同)。
  141. /// </summary>
  142. [Browsable(false)]
  143. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  144. [EditorBrowsable(EditorBrowsableState.Advanced)]
  145. public string FormCode
  146. {
  147. get
  148. {
  149. return this._formCode;
  150. }
  151. set
  152. {
  153. this._formCode = value;
  154. }
  155. }
  156. /// <summary>
  157. /// 获取一个值,该值指示是否正在异步处理。
  158. /// </summary>
  159. [DefaultValue(false)]
  160. [Browsable(false)]
  161. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  162. [EditorBrowsable(EditorBrowsableState.Advanced)]
  163. [Description("获取一个值,该值指示是否正在异步处理。"), Category("CustomerEx")]
  164. public bool AsyncDoing
  165. {
  166. get
  167. {
  168. return this._doAsync;
  169. }
  170. }
  171. /// <summary>
  172. /// 获取一个值,该值指示异步处理是否被取消。
  173. /// </summary>
  174. [DefaultValue(false)]
  175. [Browsable(false)]
  176. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  177. [EditorBrowsable(EditorBrowsableState.Advanced)]
  178. [Description("获取一个值,该值指示异步处理是否被取消。"), Category("CustomerEx")]
  179. public bool AsyncCanecled
  180. {
  181. get
  182. {
  183. return this._cancelAsync;
  184. }
  185. }
  186. /// <summary>
  187. /// 获取或设置一个值,该值指示窗体中数据是否被修改过。
  188. /// </summary>
  189. [DefaultValue(false)]
  190. [Browsable(false)]
  191. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  192. [EditorBrowsable(EditorBrowsableState.Advanced)]
  193. [Description("获取或设置一个值,该值指示窗体中数据是否被修改过。"), Category("CustomerEx")]
  194. public bool Dirty
  195. {
  196. get
  197. {
  198. return this._isDirty || this.CheckDirty();
  199. }
  200. set
  201. {
  202. this._isDirty = value;
  203. }
  204. }
  205. /// <summary>
  206. /// 窗体类型
  207. /// </summary>
  208. [Description("获取或设置一个值,该值指示窗体中数据是否被修改过。"), Category("CustomerEx")]
  209. [DefaultValue(typeof(FormType), "Select")]
  210. [Browsable(false)]
  211. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  212. [EditorBrowsable(EditorBrowsableState.Advanced)]
  213. public FormType FormType
  214. {
  215. get
  216. {
  217. return this._formType;
  218. }
  219. set
  220. {
  221. this._formType = value;
  222. }
  223. }
  224. /// <summary>
  225. /// 获取或设置一个值,该值指示是否显示状态条。
  226. /// </summary>
  227. [Description("获取或设置一个值,该值指示是否显示状态条。"), Category("CustomerEx")]
  228. [DefaultValue(true)]
  229. public virtual bool ShowStatusStrip
  230. {
  231. get
  232. {
  233. return this._showStatusStrip;
  234. }
  235. set
  236. {
  237. if (this._showStatusStrip != value)
  238. {
  239. this._showStatusStrip = value;
  240. if (this._showStatusStrip)
  241. {
  242. this.Controls.Add(this.ssrAsyncStatusStrip);
  243. }
  244. else
  245. {
  246. this.Controls.Remove(this.ssrAsyncStatusStrip);
  247. }
  248. this.ssrAsyncStatusStrip.Visible = value;
  249. }
  250. }
  251. }
  252. #endregion
  253. #region 重写属性
  254. /// <summary>
  255. /// 获取或设置窗体的边框样式。
  256. /// </summary>
  257. //[Browsable(false)]
  258. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  259. //[EditorBrowsable(EditorBrowsableState.Advanced)]
  260. public virtual new FormBorderStyle FormBorderStyle
  261. {
  262. get
  263. {
  264. return base.FormBorderStyle;
  265. }
  266. set
  267. {
  268. base.FormBorderStyle = value;
  269. this.SetStatusSizingGrip();
  270. }
  271. }
  272. /// <summary>
  273. /// 获取或设置运行时窗体的起始位置
  274. /// </summary>
  275. [Localizable(true)]
  276. [DefaultValue(typeof(FormStartPosition), "CenterScreen")]
  277. public virtual new FormStartPosition StartPosition
  278. {
  279. get
  280. {
  281. return base.StartPosition;
  282. }
  283. set
  284. {
  285. base.StartPosition = value;
  286. }
  287. }
  288. #endregion
  289. #region 重写事件
  290. private bool _closeOnEsc = true;
  291. public bool CloseOnEsc
  292. {
  293. get
  294. {
  295. return this._closeOnEsc;
  296. }
  297. set
  298. {
  299. this._closeOnEsc = value;
  300. }
  301. }
  302. protected override void OnKeyDown(KeyEventArgs e)
  303. {
  304. base.OnKeyDown(e);
  305. if (this.CancelButton == null && e.KeyData == Keys.Escape && this._closeOnEsc)
  306. {
  307. this.Close();
  308. }
  309. }
  310. /// <summary>
  311. /// 窗体大小改变
  312. /// </summary>
  313. /// <param name="e"></param>
  314. protected override void OnSizeChanged(EventArgs e)
  315. {
  316. base.OnSizeChanged(e);
  317. if (this.WindowState != FormWindowState.Minimized)
  318. {
  319. this._formWindowState = this.WindowState;
  320. }
  321. this.SetStatusSizingGrip();
  322. }
  323. /// <summary>
  324. /// 窗体关闭时
  325. /// </summary>
  326. /// <param name="e"></param>
  327. protected override void OnClosed(EventArgs e)
  328. {
  329. base.OnClosed(e);
  330. FormFactory.RemoveForm(this);
  331. try
  332. {
  333. // datagridview控件设置保存(TODO)
  334. System.Collections.Generic.List<Control> items = this.GetControls(this, typeof(Dongke.IBOSS.PRD.Basics.BaseControls.C_DataGridView));
  335. foreach (Dongke.IBOSS.PRD.Basics.BaseControls.C_DataGridView grd in items)
  336. {
  337. if (grd.IsSaveDataGridViewSetting)
  338. {
  339. Dongke.IBOSS.PRD.Basics.Library.GridSettingManager.SaveGridSetting(grd, this.Name + grd.Name);
  340. }
  341. }
  342. }
  343. catch /*(Exception ex)*/
  344. {
  345. //Dongke.IBOSS.MBC.Basics.Library.OutputLog.TraceLog( Dongke.IBOSS.MBC.Basics.Library.LogPriority.Error,
  346. // "Dongke.WinForm.Controls.FormBase", "SaveGridSetting", ex.Message, logFilePath);
  347. }
  348. }
  349. /// <summary>
  350. /// 正在关闭窗体时
  351. /// </summary>
  352. /// <param name="e"></param>
  353. protected override void OnClosing(CancelEventArgs e)
  354. {
  355. // 画面暂不能关闭
  356. if(!this._canClose)
  357. {
  358. e.Cancel = true;
  359. return;
  360. }
  361. // 保存成功后关闭
  362. if (this._isSaving)
  363. {
  364. this.DialogResult = DialogResult.OK;
  365. base.OnClosing(e);
  366. return;
  367. }
  368. this._isClosing = true;
  369. try
  370. {
  371. // 画面数据有更改
  372. if (this.Dirty)
  373. {
  374. DialogResult dr = this.ShowMessageOnDirtyClose();
  375. // 关闭前保存
  376. if (dr == DialogResult.Yes)
  377. {
  378. // 保存成功
  379. if (this.SetData())
  380. {
  381. this._isDirty = false;
  382. this.DialogResult = DialogResult.OK;
  383. }
  384. // 保存失败
  385. else
  386. {
  387. e.Cancel = true;
  388. return;
  389. }
  390. }
  391. // 关闭不保存
  392. else if (dr == DialogResult.No)
  393. {
  394. //if (this.DialogResult == DialogResult.None)
  395. //{
  396. // this.DialogResult = DialogResult.Cancel;
  397. //}
  398. }
  399. // 取消关闭
  400. else //if (dr == DialogResult.Cancel)
  401. {
  402. e.Cancel = true;
  403. return;
  404. }
  405. }
  406. if (this.DialogResult == DialogResult.None)
  407. {
  408. this.DialogResult = DialogResult.Cancel;
  409. }
  410. base.OnClosing(e);
  411. }
  412. finally
  413. {
  414. this._isClosing = false;
  415. }
  416. }
  417. /// <summary>
  418. /// 窗体加载时发生。
  419. /// </summary>
  420. /// <param name="e"></param>
  421. protected override void OnLoad(EventArgs e)
  422. {
  423. this.DialogResult = DialogResult.None;
  424. this._loadCount++;
  425. if (this._loadCount == 1)
  426. {
  427. this.InitForm();
  428. }
  429. if (!this.DesignMode)
  430. {
  431. if (!this.LoadForm())
  432. {
  433. this._isDirty = false;
  434. this.Close();
  435. return;
  436. }
  437. }
  438. base.OnLoad(e);
  439. this._isDirty = false;
  440. try
  441. {
  442. // TODO 临时
  443. //datagridview控件设置保存
  444. System.Collections.Generic.List<Control> controls = this.GetControls(this, typeof(Dongke.IBOSS.PRD.Basics.BaseControls.C_DataGridView));
  445. foreach (Dongke.IBOSS.PRD.Basics.BaseControls.C_DataGridView dgv in controls)
  446. {
  447. if (dgv.IsSaveDataGridViewSetting)
  448. {
  449. Dongke.IBOSS.PRD.Basics.Library.GridSettingManager.InitializeGridSetting(dgv, this.Name + dgv.Name);
  450. }
  451. }
  452. }
  453. catch /*(Exception ex)*/
  454. {
  455. //Dongke.IBOSS.MBC.Basics.Library.OutputLog.TraceLog( Dongke.IBOSS.MBC.Basics.Library.LogPriority.Error,
  456. // "Dongke.WinForm.Controls.FormBase", "SaveGridSetting", ex.Message, logFilePath);
  457. }
  458. }
  459. /// <summary>
  460. /// TODO 临时
  461. /// </summary>
  462. /// <param name="topControl"></param>
  463. /// <param name="type"></param>
  464. /// <returns></returns>
  465. private System.Collections.Generic.List<Control> GetControls(Control topControl, Type type)
  466. {
  467. if (topControl == null)
  468. {
  469. return GetControls(this, type);
  470. }
  471. System.Collections.Generic.List<Control> list = new System.Collections.Generic.List<Control>();
  472. foreach (Control control in topControl.Controls)
  473. {
  474. if (type == null || type.Equals(control.GetType()))
  475. {
  476. list.Add(control);
  477. }
  478. list.AddRange(GetControls(control, type));
  479. }
  480. return list;
  481. }
  482. #endregion
  483. #region 事件处理
  484. #endregion
  485. #region 重写方法
  486. #endregion
  487. #region 公有方法
  488. #region Set
  489. /// <summary>
  490. /// 点击保存或确定按钮时调用。
  491. /// </summary>
  492. /// <returns>操作结果</returns>
  493. public virtual bool SetData()
  494. {
  495. Control ac = this.ActiveControl;
  496. this.txtFoucs.Focus();
  497. bool result = false;
  498. // 验证画面数据有效性
  499. result = this.CheckInputData();
  500. if (!result)
  501. {
  502. return result;
  503. }
  504. // 保存数据到服务端
  505. result = this.SetDataToOther();
  506. if (!result)
  507. {
  508. //this.ShowMessageOnSetDefeated();
  509. ac.Focus();
  510. return result;
  511. }
  512. // 关闭画面的保存
  513. if (this._isClosing)
  514. {
  515. if (this._formType != FormType.Select)
  516. {
  517. this.ShowMessageOnSetSucceed();
  518. }
  519. ac.Focus();
  520. return result;
  521. }
  522. // 新建、复制成功后
  523. if (this._formType == FormType.Add ||
  524. this._formType == FormType.Copy)
  525. {
  526. // 是否继续操作
  527. if (this.ShowMessageOnAddSucceed())
  528. {
  529. this.RefreshData();
  530. this._isDirty = false;
  531. ac.Focus();
  532. return result;
  533. }
  534. }
  535. else if (this._formType != FormType.Select)
  536. {
  537. this.ShowMessageOnSetSucceed();
  538. }
  539. this._isSaving = true;
  540. this.Close();
  541. this._isSaving = false;
  542. return result;
  543. }
  544. /// <summary>
  545. /// 窗体中数据保存后,需要继续操作,并初始化窗体时调用。
  546. /// </summary>
  547. public virtual void RefreshData()
  548. {
  549. this.ClearConditions();
  550. this.ClearQueryResults();
  551. this.ssrAsyncStatusStrip.ClearCommTimes();
  552. base.SelectNextControl(null, true, true, true, false);
  553. this._isDirty = false;
  554. }
  555. #endregion
  556. #region Query
  557. /// <summary>
  558. /// 点击查询按钮时调用。
  559. /// </summary>
  560. /// <returns>操作结果</returns>
  561. public virtual bool QueryData()
  562. {
  563. bool result = false;
  564. // 验证查询条件有效性
  565. result = this.CheckQueryConditions();
  566. if (!result)
  567. {
  568. return result;
  569. }
  570. this.ClearQueryResults();
  571. result = this.QueryDataFromOther();
  572. //if (!result)
  573. //{
  574. // this.ShowMessageOnQueryDefeated();
  575. //}
  576. return result;
  577. }
  578. /// <summary>
  579. /// 清除查询条件
  580. /// </summary>
  581. public virtual void ClearConditions()
  582. {
  583. }
  584. /// <summary>
  585. /// 清除查询结果
  586. /// </summary>
  587. public virtual void ClearQueryResults()
  588. {
  589. }
  590. #endregion
  591. #region 异步处理
  592. /// <summary>
  593. /// 窗体的异步处理
  594. /// </summary>
  595. /// <param name="method">异步方法</param>
  596. /// <param name="result">返回值</param>
  597. /// <returns>处理结果:true处理成功,false处理失败</returns>
  598. public bool DoAsync<TResult>(Func<TResult> method, out TResult result)
  599. {
  600. result = default(TResult);
  601. bool asyncResult = false;
  602. if (this._doAsync)
  603. {
  604. return asyncResult;
  605. }
  606. CancelEventArgs ce = new CancelEventArgs();
  607. this.OnAsyncBegin(ce);
  608. if (ce.Cancel)
  609. {
  610. return asyncResult;
  611. }
  612. try
  613. {
  614. this._cancelAsync = false;
  615. this._doAsync = true;
  616. this._canClose = false;
  617. this.StartProgress();
  618. IAsyncResult iAsyncResult = method.BeginInvoke(null, null);
  619. while (!iAsyncResult.IsCompleted)
  620. {
  621. if (this._cancelAsync)
  622. {
  623. return asyncResult;
  624. }
  625. Application.DoEvents();
  626. Thread.Sleep(1);
  627. }
  628. result = method.EndInvoke(iAsyncResult);
  629. asyncResult = true;
  630. }
  631. finally
  632. {
  633. this._doAsync = false;
  634. this._canClose = true;
  635. this.EndProgress();
  636. AsyncEndEventArgs e = new AsyncEndEventArgs(asyncResult, result);
  637. this.OnAsyncEnd(e);
  638. asyncResult = e.AsyncResult;
  639. }
  640. return asyncResult;
  641. }
  642. /// <summary>
  643. /// 取消异步处理。
  644. /// </summary>
  645. public void CancelAsync()
  646. {
  647. if (this._doAsync)
  648. {
  649. this._cancelAsync = true;
  650. }
  651. }
  652. #endregion
  653. /*
  654. #region 异步处理(不锁定窗体)
  655. /// <summary>
  656. /// 窗体的异步处理(不锁定窗体)
  657. /// </summary>
  658. /// <typeparam name="T">返回值类型</typeparam>
  659. /// <param name="method">异步方法</param>
  660. /// <param name="callback">回调方法</param>
  661. /// <param name="code">方法扩展标识</param>
  662. /// <returns>方法标识</returns>
  663. public static IAsyncInvoker DoAsync<T>(Func<T> method, Action<T> callback, string code = null)
  664. {
  665. return AsyncFactory.Invoke<T>(method, callback, code);
  666. }
  667. /// <summary>
  668. /// 取消异步
  669. /// </summary>
  670. /// <typeparam name="T">返回值类型</typeparam>
  671. /// <param name="method">异步方法</param>
  672. /// <param name="code">方法扩展标识</param>
  673. /// <returns>是否成功取消</returns>
  674. public static bool Cancel<T>(Func<T> method, string code = null)
  675. {
  676. return AsyncFactory.Cancel<T>(method, code);
  677. }
  678. /// <summary>
  679. /// 取消异步
  680. /// </summary>
  681. /// <param name="methodCode">方法标识</param>
  682. /// <returns>是否成功取消</returns>
  683. public static bool Cancel(string methodCode)
  684. {
  685. return AsyncFactory.Cancel(methodCode);
  686. }
  687. /// <summary>
  688. /// 获取异步是否正在处理
  689. /// </summary>
  690. /// <typeparam name="T">返回值类型</typeparam>
  691. /// <param name="method">异步方法</param>
  692. /// <param name="code">方法扩展标识</param>
  693. /// <returns>是否正在处理</returns>
  694. public static bool IsDoing<T>(Func<T> method, string code = null)
  695. {
  696. return AsyncFactory.IsDoing<T>(method, code);
  697. }
  698. /// <summary>
  699. /// 获取异步是否正在处理
  700. /// </summary>
  701. /// <param name="methodCode">方法标识</param>
  702. /// <returns>是否正在处理</returns>
  703. public static bool IsDoing(string methodCode)
  704. {
  705. return AsyncFactory.IsDoing(methodCode);
  706. }
  707. /// <summary>
  708. /// 获取方法标识
  709. /// </summary>
  710. /// <typeparam name="T">返回值类型</typeparam>
  711. /// <param name="method">异步方法</param>
  712. /// <param name="code">方法扩展标识</param>
  713. /// <returns>方法标识</returns>
  714. public static string GetMethodCode<T>(Func<T> method, string code = null)
  715. {
  716. return AsyncFactory.GetMethodCode<T>(method, code);
  717. }
  718. #endregion
  719. */
  720. /// <summary>
  721. /// 向用户显示具有指定所有者的窗体。
  722. /// </summary>
  723. /// <param name="owner">表示将拥有模式对话框的顶级窗口</param>
  724. public virtual new void Show(IWin32Window owner)
  725. {
  726. base.Show(owner);
  727. this.ActivateForm();
  728. }
  729. /// <summary>
  730. /// 向用户显示具有指定所有者的窗体。
  731. /// </summary>
  732. public virtual new void Show()
  733. {
  734. base.Show();
  735. this.ActivateForm();
  736. }
  737. /// <summary>
  738. /// 激活窗体
  739. /// </summary>
  740. public void ActivateForm()
  741. {
  742. this.Visible = true;
  743. if (this.WindowState == FormWindowState.Minimized)
  744. {
  745. this.WindowState = this._formWindowState;
  746. }
  747. base.Activate();
  748. }
  749. #endregion
  750. #region 保护方法
  751. /// <summary>
  752. /// 设置异步处理开始状态
  753. /// </summary>
  754. protected virtual void StartProgress()
  755. {
  756. this._doFocus = true;
  757. this.BeginAsyncControls(this);
  758. this.ssrAsyncStatusStrip.Communicate = true;
  759. }
  760. /// <summary>
  761. /// 设置异步处理结束状态
  762. /// </summary>
  763. protected virtual void EndProgress()
  764. {
  765. this.ssrAsyncStatusStrip.Communicate = false;
  766. this.EndAsyncControls(this);
  767. }
  768. #region NotImplemented
  769. #region Form
  770. /// <summary>
  771. /// 窗体加载前调用(设置Form.Text、按钮等控件Text等)。
  772. /// </summary>
  773. protected virtual void InitForm()
  774. {
  775. }
  776. /// <summary>
  777. /// 窗体加载时调用(获取窗体初始化数据等)。
  778. /// </summary>
  779. protected virtual bool LoadForm()
  780. {
  781. return true;
  782. }
  783. #endregion
  784. #region Close
  785. /// <summary>
  786. /// 验证窗体中数据是否改变。
  787. /// </summary>
  788. /// <returns>数据被更改true,其他false</returns>
  789. protected virtual bool CheckDirty()
  790. {
  791. return false;
  792. }
  793. /// <summary>
  794. /// 关闭数据有改变的画面时,提示消息
  795. /// </summary>
  796. protected virtual DialogResult ShowMessageOnDirtyClose()
  797. {
  798. return DialogResult.Yes;
  799. }
  800. #endregion
  801. #region Set
  802. /// <summary>
  803. /// 验证画面输入项目
  804. /// </summary>
  805. /// <returns>验证通过true,其他false</returns>
  806. protected virtual bool CheckInputData()
  807. {
  808. return true;
  809. }
  810. /// <summary>
  811. /// 保存或设置数据到其他位置(其他画面或DB)
  812. /// </summary>
  813. /// <returns>保存或设置成功true,其他false</returns>
  814. protected virtual bool SetDataToOther()
  815. {
  816. return true;
  817. }
  818. /// <summary>
  819. /// 保存或设置数据成功后提示消息
  820. /// </summary>
  821. protected virtual void ShowMessageOnSetSucceed()
  822. {
  823. }
  824. /// <summary>
  825. /// 保存或设置数据成功后提示消息(新建或复制时,询问是否继续操作)
  826. /// </summary>
  827. /// <returns>继续操作true,其他false</returns>
  828. protected virtual bool ShowMessageOnAddSucceed()
  829. {
  830. return true;
  831. }
  832. ///// <summary>
  833. ///// 保存或设置数据失败后提示消息
  834. ///// </summary>
  835. //protected virtual void ShowMessageOnSetDefeated()
  836. //{
  837. //}
  838. #endregion
  839. #region Query
  840. /// <summary>
  841. /// 验证查询条件。
  842. /// </summary>
  843. /// <returns>验证通过true,其他false</returns>
  844. protected virtual bool CheckQueryConditions()
  845. {
  846. return true;
  847. }
  848. /// <summary>
  849. /// 查询数据。
  850. /// </summary>
  851. /// <returns>验证通过true,其他false</returns>
  852. protected virtual bool QueryDataFromOther()
  853. {
  854. return true;
  855. }
  856. ///// <summary>
  857. ///// 查询失败后提示消息
  858. ///// </summary>
  859. //protected virtual void ShowMessageOnQueryDefeated()
  860. //{
  861. //}
  862. #endregion
  863. #endregion
  864. #endregion
  865. #region 私有方法
  866. // todo 控件混用补丁。
  867. Control _fouced = null;
  868. /// <summary>
  869. /// 异步开始设置控件状态
  870. /// </summary>
  871. /// <param name="control"></param>
  872. private void BeginAsyncControls(Control control)
  873. {
  874. if (control == null)
  875. {
  876. return;
  877. }
  878. if (control is IAsyncControl)
  879. {
  880. // todo 修改为把控件传入
  881. (control as IAsyncControl).BeginAsync(ref this._doFocus);
  882. if (!this._doFocus)
  883. {
  884. this.txtFoucs.Focus();
  885. }
  886. return;
  887. }
  888. else
  889. {
  890. // todo 控件混用补丁。
  891. if (control is DataGridView)
  892. {
  893. if (control.Focused)
  894. {
  895. this._fouced = control;
  896. this.txtFoucs.Focus();
  897. }
  898. control.Enabled = false;
  899. return;
  900. }
  901. if (control.Controls == null || control.Controls.Count == 0)
  902. {
  903. //control.Enabled = false;
  904. return;
  905. }
  906. foreach (Control item in control.Controls)
  907. {
  908. this.BeginAsyncControls(item);
  909. }
  910. }
  911. }
  912. /// <summary>
  913. /// 异步结束设置控件状态
  914. /// </summary>
  915. /// <param name="control"></param>
  916. private void EndAsyncControls(Control control)
  917. {
  918. if (control == null)
  919. {
  920. return;
  921. }
  922. if (control is IAsyncControl)
  923. {
  924. (control as IAsyncControl).EndAsync();
  925. return;
  926. }
  927. else
  928. {
  929. // todo 控件混用补丁。
  930. if (control is DataGridView)
  931. {
  932. control.Enabled = true;
  933. if (this._fouced == control)
  934. {
  935. this._fouced = null;
  936. control.Focus();
  937. }
  938. return;
  939. }
  940. if (control.Controls == null || control.Controls.Count == 0)
  941. {
  942. //control.Enabled = false;
  943. return;
  944. }
  945. foreach (Control item in control.Controls)
  946. {
  947. this.EndAsyncControls(item);
  948. }
  949. }
  950. }
  951. /// <summary>
  952. /// 设置是否在控件的右下角显示大小调整手柄。
  953. /// </summary>
  954. private void SetStatusSizingGrip()
  955. {
  956. switch (base.FormBorderStyle)
  957. {
  958. case FormBorderStyle.Sizable:
  959. case FormBorderStyle.SizableToolWindow:
  960. this.ssrAsyncStatusStrip.SizingGrip = true;
  961. break;
  962. default:
  963. this.ssrAsyncStatusStrip.SizingGrip = false;
  964. break;
  965. }
  966. }
  967. #endregion
  968. }
  969. }