DockPanelBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. }
  129. /// <summary>
  130. /// 取得所有控件
  131. /// </summary>
  132. /// <param name="topControl">控件类型</param>
  133. /// <param name="type">控件类型</param>
  134. /// <returns>所有控件</returns>
  135. public Control[] GetControls(Control topControl, Type type)
  136. {
  137. if (topControl == null)
  138. {
  139. return GetControls(this, type);
  140. }
  141. ArrayList list = new ArrayList();
  142. foreach (Control control in topControl.Controls)
  143. {
  144. if (type == null || type.Equals(control.GetType()))
  145. {
  146. list.Add(control);
  147. }
  148. list.AddRange(GetControls(control, type));
  149. }
  150. return (Control[])list.ToArray(typeof(Control));
  151. }
  152. #endregion
  153. #endregion
  154. #region 程序异步处理
  155. /// <summary>
  156. /// 窗体的异步处理
  157. /// </summary>
  158. /// <param name="method">异步方法</param>
  159. /// <returns></returns>
  160. public object DoAsync(AsyncMethod method)
  161. {
  162. object result = null;
  163. try
  164. {
  165. StartProgress();
  166. IAsyncResult asyncResult = method.BeginInvoke(null, null);
  167. while (!asyncResult.IsCompleted)
  168. {
  169. Application.DoEvents();
  170. System.Threading.Thread.Sleep(1);
  171. }
  172. result = method.EndInvoke(asyncResult);
  173. }
  174. finally
  175. {
  176. EndProgress();
  177. }
  178. return result;
  179. }
  180. /// <summary>
  181. /// 开始异步处理
  182. /// </summary>
  183. public virtual void StartProgress()
  184. {
  185. this._canClosing = false;
  186. BeginAsyncControls(this);
  187. }
  188. public virtual void BeginAsyncControls(Control control)
  189. {
  190. if (control == null)
  191. {
  192. return;
  193. }
  194. if (control is IDKControl)
  195. {
  196. (control as IDKControl).BeginAsync();
  197. return;
  198. }
  199. else
  200. {
  201. if (control is ToolStrip)
  202. {
  203. control.Enabled = false;
  204. return;
  205. }
  206. if (control.Controls == null || control.Controls.Count == 0)
  207. {
  208. //control.Enabled = false;
  209. return;
  210. }
  211. foreach (Control item in control.Controls)
  212. {
  213. BeginAsyncControls(item);
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 结束异步处理
  219. /// </summary>
  220. public virtual void EndProgress()
  221. {
  222. this._canClosing = true;
  223. EndAsyncControls(this);
  224. }
  225. public virtual void EndAsyncControls(Control control)
  226. {
  227. if (control == null)
  228. {
  229. return;
  230. }
  231. if (control is IDKControl)
  232. {
  233. (control as IDKControl).EndAsync();
  234. return;
  235. }
  236. else
  237. {
  238. if (control is ToolStrip)
  239. {
  240. control.Enabled = true;
  241. return;
  242. }
  243. if (control.Controls == null || control.Controls.Count == 0)
  244. {
  245. //control.Enabled = false;
  246. return;
  247. }
  248. foreach (Control item in control.Controls)
  249. {
  250. EndAsyncControls(item);
  251. }
  252. }
  253. }
  254. #endregion
  255. #region 私有方法/函数
  256. private void InitializeComponent()
  257. {
  258. this.components = new System.ComponentModel.Container();
  259. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DockPanelBase));
  260. this.cmsMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
  261. this.tsmiClose = new System.Windows.Forms.ToolStripMenuItem();
  262. this.tsmiCloseAllExcept = new System.Windows.Forms.ToolStripMenuItem();
  263. this.tsmiCloseAll = new System.Windows.Forms.ToolStripMenuItem();
  264. this.cmsMenu.SuspendLayout();
  265. this.SuspendLayout();
  266. //
  267. // cmsMenu
  268. //
  269. this.cmsMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  270. this.tsmiClose,
  271. this.tsmiCloseAllExcept,
  272. this.tsmiCloseAll});
  273. this.cmsMenu.Name = "cmsMenu";
  274. this.cmsMenu.ShowImageMargin = false;
  275. this.cmsMenu.Size = new System.Drawing.Size(163, 70);
  276. //
  277. // tsmiClose
  278. //
  279. this.tsmiClose.Name = "tsmiClose";
  280. this.tsmiClose.Size = new System.Drawing.Size(162, 22);
  281. this.tsmiClose.Text = "关闭(&X)";
  282. this.tsmiClose.Click += new System.EventHandler(this.tsmiClose_Click);
  283. //
  284. // tsmiCloseAllExcept
  285. //
  286. this.tsmiCloseAllExcept.Name = "tsmiCloseAllExcept";
  287. this.tsmiCloseAllExcept.Size = new System.Drawing.Size(162, 22);
  288. this.tsmiCloseAllExcept.Text = "除此之外全部关闭(&E)";
  289. this.tsmiCloseAllExcept.Click += new System.EventHandler(this.tsmiCloseAllExcept_Click);
  290. //
  291. // tsmiCloseAll
  292. //
  293. this.tsmiCloseAll.Name = "tsmiCloseAll";
  294. this.tsmiCloseAll.Size = new System.Drawing.Size(162, 22);
  295. this.tsmiCloseAll.Text = "全部关闭(&A)";
  296. this.tsmiCloseAll.Click += new System.EventHandler(this.tsmiCloseAll_Click);
  297. //
  298. // DockPanelBase
  299. //
  300. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  301. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  302. this.AutoScroll = true;
  303. this.ClientSize = new System.Drawing.Size(284, 262);
  304. this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  305. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  306. this.Name = "DockPanelBase";
  307. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DockPanelBase_FormClosing);
  308. this.cmsMenu.ResumeLayout(false);
  309. this.ResumeLayout(false);
  310. }
  311. private void tsmiClose_Click(object sender, EventArgs e)
  312. {
  313. this.Close();
  314. }
  315. private void tsmiCloseAllExcept_Click(object sender, EventArgs e)
  316. {
  317. IDockContent[] documents = DockPanel.DocumentsToArray();
  318. foreach (IDockContent content in documents)
  319. {
  320. if (!content.Equals(this))
  321. {
  322. content.DockHandler.Close();
  323. }
  324. }
  325. }
  326. private void tsmiCloseAll_Click(object sender, EventArgs e)
  327. {
  328. IDockContent[] documents = DockPanel.DocumentsToArray();
  329. foreach (IDockContent content in documents)
  330. {
  331. content.DockHandler.Close();
  332. }
  333. }
  334. /// <summary>
  335. /// 显示对应的页面中主窗体中
  336. /// </summary>
  337. /// <param name="dockPanelBaseForm">需要停靠的DockPanelBase</param>
  338. public void ShowInDockPanel(DockPanel dockPanel)
  339. {
  340. try
  341. {
  342. this.Show(dockPanel, DockState.Document);
  343. this.Activate();
  344. }
  345. catch (Exception ex)
  346. {
  347. throw ex;
  348. }
  349. }
  350. #endregion
  351. }
  352. }