DockPanelBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DockPanelBase.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Windows.Forms;
  15. using Dongke.IBOSS.PRD.Basics.BaseControls;
  16. using Dongke.IBOSS.PRD.Basics.Library;
  17. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  18. {
  19. #region 异步处理的委托
  20. /// <summary>
  21. /// 异步处理
  22. /// </summary>
  23. /// <returns></returns>
  24. public delegate object AsyncMethod();
  25. #endregion
  26. public partial class DockPanelBase : DockContent
  27. {
  28. #region 成员变量
  29. private bool _canClosing = true;
  30. private DataSet _dataSource = null;
  31. private ContextMenuStrip cmsMenu;
  32. private ToolStripMenuItem tsmiClose;
  33. private ToolStripMenuItem tsmiCloseAll;
  34. private ToolStripMenuItem tsmiCloseAllExcept;
  35. private IContainer components = null;
  36. #endregion
  37. #region 属性
  38. /// <summary>
  39. /// 窗体的数据源的取得和设定
  40. /// </summary>
  41. [Browsable(true)]
  42. [DefaultValue(null)]
  43. [Description("窗体的数据源的取得和设定。")]
  44. public DataSet DataSource
  45. {
  46. get
  47. {
  48. return _dataSource;
  49. }
  50. set
  51. {
  52. _dataSource = value;
  53. }
  54. }
  55. #endregion
  56. #region 构造函数
  57. /// <summary>
  58. /// 构造函数
  59. /// </summary>
  60. public DockPanelBase()
  61. {
  62. InitializeComponent();
  63. this.Font = ControlsConst.FONT_SYSTEM_DEFAULT;
  64. this.TabPageContextMenuStrip = cmsMenu;
  65. this.BackgroundImage = ResourceBackGroundImage.bg;
  66. }
  67. #endregion
  68. #region 事件
  69. /// <summary>
  70. /// 关闭事件
  71. /// </summary>
  72. /// <param name="sender"></param>
  73. /// <param name="e"></param>
  74. private void DockPanelBase_FormClosing(object sender, FormClosingEventArgs e)
  75. {
  76. if (!this._canClosing)
  77. {
  78. e.Cancel = true;
  79. }
  80. }
  81. #region 重写方法或者函数
  82. /// <summary>
  83. /// 重写Form的OnLoad事件
  84. /// </summary>
  85. /// <param name="e"></param>
  86. protected override void OnLoad(EventArgs e)
  87. {
  88. base.OnLoad(e);
  89. //datagridview控件设置保存
  90. Control[] controls = this.GetControls(this, typeof(C_DataGridView));
  91. foreach (C_DataGridView dgv in controls)
  92. {
  93. if (dgv.IsSaveDataGridViewSetting)
  94. {
  95. GridSettingManager.InitializeGridSetting(dgv, this.Name + dgv.Name);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 重写Form的OnFormClosing事件
  101. /// </summary>
  102. /// <param name="e"></param>
  103. protected override void OnFormClosing(FormClosingEventArgs e)
  104. {
  105. if (!this._canClosing)
  106. {
  107. e.Cancel = true;
  108. return;
  109. }
  110. base.OnFormClosing(e);
  111. }
  112. /// <summary>
  113. /// 重写Form的OnFormClosed事件
  114. /// </summary>
  115. /// <param name="e"></param>
  116. protected override void OnFormClosed(FormClosedEventArgs e)
  117. {
  118. // datagridview控件设置保存(TODO)
  119. Control[] items = this.GetControls(this, typeof(C_DataGridView));
  120. foreach (C_DataGridView grd in items)
  121. {
  122. if (grd.IsSaveDataGridViewSetting)
  123. {
  124. GridSettingManager.SaveGridSetting(grd, this.Name + grd.Name);
  125. }
  126. }
  127. base.OnFormClosed(e);
  128. // 内存回收 241224
  129. GC.Collect();
  130. GC.WaitForPendingFinalizers();
  131. }
  132. /// <summary>
  133. /// 取得所有控件
  134. /// </summary>
  135. /// <param name="topControl">控件类型</param>
  136. /// <param name="type">控件类型</param>
  137. /// <returns>所有控件</returns>
  138. public Control[] GetControls(Control topControl, Type type)
  139. {
  140. if (topControl == null)
  141. {
  142. return GetControls(this, type);
  143. }
  144. ArrayList list = new ArrayList();
  145. foreach (Control control in topControl.Controls)
  146. {
  147. if (type == null || type.Equals(control.GetType()))
  148. {
  149. list.Add(control);
  150. }
  151. list.AddRange(GetControls(control, type));
  152. }
  153. return (Control[])list.ToArray(typeof(Control));
  154. }
  155. #endregion
  156. #endregion
  157. #region 程序异步处理
  158. /// <summary>
  159. /// 窗体的异步处理
  160. /// </summary>
  161. /// <param name="method">异步方法</param>
  162. /// <returns></returns>
  163. public object DoAsync(AsyncMethod method)
  164. {
  165. object result = null;
  166. try
  167. {
  168. StartProgress();
  169. IAsyncResult asyncResult = method.BeginInvoke(null, null);
  170. while (!asyncResult.IsCompleted)
  171. {
  172. Application.DoEvents();
  173. System.Threading.Thread.Sleep(1);
  174. }
  175. result = method.EndInvoke(asyncResult);
  176. }
  177. finally
  178. {
  179. EndProgress();
  180. }
  181. return result;
  182. }
  183. /// <summary>
  184. /// 开始异步处理
  185. /// </summary>
  186. public virtual void StartProgress()
  187. {
  188. this._canClosing = false;
  189. BeginAsyncControls(this);
  190. }
  191. public virtual void BeginAsyncControls(Control control)
  192. {
  193. if (control == null)
  194. {
  195. return;
  196. }
  197. if (control is IDKControl)
  198. {
  199. (control as IDKControl).BeginAsync();
  200. return;
  201. }
  202. else
  203. {
  204. if (control is ToolStrip)
  205. {
  206. control.Enabled = false;
  207. return;
  208. }
  209. if (control.Controls == null || control.Controls.Count == 0)
  210. {
  211. //control.Enabled = false;
  212. return;
  213. }
  214. foreach (Control item in control.Controls)
  215. {
  216. BeginAsyncControls(item);
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// 结束异步处理
  222. /// </summary>
  223. public virtual void EndProgress()
  224. {
  225. this._canClosing = true;
  226. EndAsyncControls(this);
  227. }
  228. public virtual void EndAsyncControls(Control control)
  229. {
  230. if (control == null)
  231. {
  232. return;
  233. }
  234. if (control is IDKControl)
  235. {
  236. (control as IDKControl).EndAsync();
  237. return;
  238. }
  239. else
  240. {
  241. if (control is ToolStrip)
  242. {
  243. control.Enabled = true;
  244. return;
  245. }
  246. if (control.Controls == null || control.Controls.Count == 0)
  247. {
  248. //control.Enabled = false;
  249. return;
  250. }
  251. foreach (Control item in control.Controls)
  252. {
  253. EndAsyncControls(item);
  254. }
  255. }
  256. }
  257. #endregion
  258. #region 私有方法/函数
  259. private void InitializeComponent()
  260. {
  261. this.components = new System.ComponentModel.Container();
  262. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DockPanelBase));
  263. this.cmsMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
  264. this.tsmiClose = new System.Windows.Forms.ToolStripMenuItem();
  265. this.tsmiCloseAllExcept = new System.Windows.Forms.ToolStripMenuItem();
  266. this.tsmiCloseAll = new System.Windows.Forms.ToolStripMenuItem();
  267. this.cmsMenu.SuspendLayout();
  268. this.SuspendLayout();
  269. //
  270. // cmsMenu
  271. //
  272. this.cmsMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  273. this.tsmiClose,
  274. this.tsmiCloseAllExcept,
  275. this.tsmiCloseAll});
  276. this.cmsMenu.Name = "cmsMenu";
  277. this.cmsMenu.ShowImageMargin = false;
  278. this.cmsMenu.Size = new System.Drawing.Size(163, 70);
  279. //
  280. // tsmiClose
  281. //
  282. this.tsmiClose.Name = "tsmiClose";
  283. this.tsmiClose.Size = new System.Drawing.Size(162, 22);
  284. this.tsmiClose.Text = "关闭(&X)";
  285. this.tsmiClose.Click += new System.EventHandler(this.tsmiClose_Click);
  286. //
  287. // tsmiCloseAllExcept
  288. //
  289. this.tsmiCloseAllExcept.Name = "tsmiCloseAllExcept";
  290. this.tsmiCloseAllExcept.Size = new System.Drawing.Size(162, 22);
  291. this.tsmiCloseAllExcept.Text = "除此之外全部关闭(&E)";
  292. this.tsmiCloseAllExcept.Click += new System.EventHandler(this.tsmiCloseAllExcept_Click);
  293. //
  294. // tsmiCloseAll
  295. //
  296. this.tsmiCloseAll.Name = "tsmiCloseAll";
  297. this.tsmiCloseAll.Size = new System.Drawing.Size(162, 22);
  298. this.tsmiCloseAll.Text = "全部关闭(&A)";
  299. this.tsmiCloseAll.Click += new System.EventHandler(this.tsmiCloseAll_Click);
  300. //
  301. // DockPanelBase
  302. //
  303. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  304. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  305. this.AutoScroll = true;
  306. this.ClientSize = new System.Drawing.Size(284, 262);
  307. this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  308. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  309. this.Name = "DockPanelBase";
  310. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DockPanelBase_FormClosing);
  311. this.cmsMenu.ResumeLayout(false);
  312. this.ResumeLayout(false);
  313. }
  314. private void tsmiClose_Click(object sender, EventArgs e)
  315. {
  316. this.Close();
  317. }
  318. private void tsmiCloseAllExcept_Click(object sender, EventArgs e)
  319. {
  320. IDockContent[] documents = DockPanel.DocumentsToArray();
  321. foreach (IDockContent content in documents)
  322. {
  323. if (!content.Equals(this))
  324. {
  325. content.DockHandler.Close();
  326. }
  327. }
  328. }
  329. private void tsmiCloseAll_Click(object sender, EventArgs e)
  330. {
  331. IDockContent[] documents = DockPanel.DocumentsToArray();
  332. foreach (IDockContent content in documents)
  333. {
  334. content.DockHandler.Close();
  335. }
  336. }
  337. /// <summary>
  338. /// 显示对应的页面中主窗体中
  339. /// </summary>
  340. /// <param name="dockPanelBaseForm">需要停靠的DockPanelBase</param>
  341. public void ShowInDockPanel(DockPanel dockPanel)
  342. {
  343. try
  344. {
  345. this.Show(dockPanel, DockState.Document);
  346. this.Activate();
  347. }
  348. catch (Exception ex)
  349. {
  350. throw ex;
  351. }
  352. }
  353. #endregion
  354. }
  355. }