FlowBox.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. namespace Dongke.IBOSS.Basics.FlowSetting
  10. {
  11. /// <summary>
  12. /// 流程设计器
  13. /// </summary>
  14. public partial class FlowBox : UserControl
  15. {
  16. #region 常量
  17. #region 事件
  18. private static readonly object EventNodeDoubleClick = new object();
  19. private static readonly object EventFlowNodeLoading = new object();
  20. #endregion 事件
  21. //private const Version CurrentVersion = new Version(1, 1);
  22. #endregion 常量
  23. #region 成员变量
  24. /// <summary>
  25. /// 流程项目绘制管理器
  26. /// </summary>
  27. private ItemManager _itemManager = null;
  28. /// <summary>
  29. /// 流程编辑模式
  30. /// </summary>
  31. private FlowBoxMode _flowBoxMode = FlowBoxMode.Edit;
  32. /// <summary>
  33. /// 加载流程的版本
  34. /// </summary>
  35. private Version _loadVersion = new Version(1, 1);
  36. /// <summary>
  37. /// 流程名称
  38. /// </summary>
  39. private string _flowName = null;
  40. #endregion 成员变量
  41. #region 构造函数
  42. /// <summary>
  43. /// 构造函数
  44. /// </summary>
  45. public FlowBox()
  46. {
  47. this.InitializeComponent();
  48. this._itemManager = new ItemManager(this, this.canvasBox);
  49. this.canvasBox.SetCanvasSize(Consts.CANVAS_SIZE_DEFAULT);
  50. this.canvasBox.CanvasControl.BackgroundImage = new Bitmap(this.GetType(), "Images.backImage1.gif");
  51. this.canvasBox.CanvasControl.MouseDown += this.flowCanvas_MouseDown;
  52. this.canvasBox.CanvasControl.MouseMove += this.flowCanvas_MouseMove;
  53. this.canvasBox.CanvasControl.MouseUp += this.flowCanvas_MouseUp;
  54. this.canvasBox.CanvasControl.Paint += this.flowCanvas_Paint;
  55. this.canvasBox.CanvasControl.MouseDoubleClick += this.flowCanvas_MouseDoubleClick;
  56. this.canvasBox.KeyControl.KeyDown += this.keyControl_KeyDown;
  57. }
  58. #endregion 构造函数
  59. #region 属性
  60. /// <summary>
  61. /// 流程名称
  62. /// </summary>
  63. public string FlowName
  64. {
  65. get
  66. {
  67. return this._flowName;
  68. }
  69. set
  70. {
  71. this._flowName = value;
  72. }
  73. }
  74. /// <summary>
  75. /// 加载流程的版本
  76. /// </summary>
  77. public Version LoadVersion
  78. {
  79. get
  80. {
  81. return this._loadVersion;
  82. }
  83. }
  84. /// <summary>
  85. /// 流程编辑模式
  86. /// </summary>
  87. public FlowBoxMode BoxMode
  88. {
  89. get
  90. {
  91. return this._flowBoxMode;
  92. }
  93. set
  94. {
  95. if (this._flowBoxMode != value)
  96. {
  97. this._flowBoxMode = value;
  98. this._itemManager.BoxMode = value;
  99. this.ClearSelection();
  100. // todo
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 获取或设置画布大小
  106. /// </summary>
  107. public Size CanvasSize
  108. {
  109. get
  110. {
  111. return this.canvasBox.CanvasSize;
  112. }
  113. set
  114. {
  115. this.canvasBox.SetCanvasSize(value);
  116. this._itemManager.RefreshItems();
  117. }
  118. }
  119. /// <summary>
  120. /// 获取或设置画布背景
  121. /// </summary>
  122. public Image CanvasBackgroundImage
  123. {
  124. get
  125. {
  126. return this.canvasBox.CanvasBackgroundImage;
  127. }
  128. set
  129. {
  130. this.canvasBox.CanvasBackgroundImage = value;
  131. }
  132. }
  133. /// <summary>
  134. /// 获取画布
  135. /// </summary>
  136. public Image CanvasImage
  137. {
  138. get
  139. {
  140. return this.canvasBox.CanvasImage;
  141. }
  142. //set
  143. //{
  144. // this.canvasBox.CanvasImage = value;
  145. //}
  146. }
  147. /// <summary>
  148. /// 获取画布
  149. /// </summary>
  150. public Control CanvasControl
  151. {
  152. get
  153. {
  154. return this.canvasBox.CanvasControl;
  155. }
  156. //set
  157. //{
  158. // this.canvasBox.CanvasImage = value;
  159. //}
  160. }
  161. /// <summary>
  162. /// 获取全部流程项目
  163. /// </summary>
  164. public List<FlowItem> Items
  165. {
  166. get
  167. {
  168. return this._itemManager.Items;
  169. }
  170. }
  171. /// <summary>
  172. /// 获取选择的流程项目
  173. /// </summary>
  174. public List<FlowItem> SelectedItems
  175. {
  176. get
  177. {
  178. return this._itemManager.SelectedItems;
  179. }
  180. }
  181. /// <summary>
  182. /// 获取删除的节点
  183. /// </summary>
  184. public List<FlowNode> DeletedNodes
  185. {
  186. get
  187. {
  188. return this._itemManager.DeletedNodes;
  189. }
  190. }
  191. /// <summary>
  192. /// 获取全部的节点(包括删除的)
  193. /// </summary>
  194. public List<FlowNode> AllNodes
  195. {
  196. get
  197. {
  198. return this._itemManager.AllNodes;
  199. }
  200. }
  201. ///// <summary>
  202. ///// 获取画板
  203. ///// </summary>
  204. //internal CanvasBox CanvasBox
  205. //{
  206. // get
  207. // {
  208. // return this.canvasBox;
  209. // }
  210. //}
  211. #endregion 属性
  212. #region 事件处理
  213. #region 画布事件
  214. /// <summary>
  215. /// 画布绘制
  216. /// </summary>
  217. /// <param name="sender"></param>
  218. /// <param name="e"></param>
  219. private void flowCanvas_Paint(object sender, PaintEventArgs e)
  220. {
  221. if (this._flowBoxMode == FlowBoxMode.Display)
  222. {
  223. return;
  224. }
  225. this.canvasBox.SetSmoothingMode(e.Graphics);
  226. //this._itemManager.RefreshActiveItems(e.Graphics);
  227. this._itemManager.RefreshMouseSelectRect(e.Graphics);
  228. }
  229. /// <summary>
  230. /// 鼠标按下
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. private void flowCanvas_MouseDown(object sender, MouseEventArgs e)
  235. {
  236. if (this._flowBoxMode == FlowBoxMode.Display)
  237. {
  238. return;
  239. }
  240. this._itemManager.MouseDown(e);
  241. }
  242. /// <summary>
  243. /// 鼠标移动
  244. /// </summary>
  245. /// <param name="sender"></param>
  246. /// <param name="e"></param>
  247. private void flowCanvas_MouseMove(object sender, MouseEventArgs e)
  248. {
  249. if (this._flowBoxMode == FlowBoxMode.Display)
  250. {
  251. return;
  252. }
  253. this._itemManager.MouseMove(e);
  254. }
  255. /// <summary>
  256. /// 鼠标抬起
  257. /// </summary>
  258. /// <param name="sender"></param>
  259. /// <param name="e"></param>
  260. private void flowCanvas_MouseUp(object sender, MouseEventArgs e)
  261. {
  262. if (this._flowBoxMode == FlowBoxMode.Display)
  263. {
  264. return;
  265. }
  266. this._itemManager.MouseUp(e);
  267. }
  268. /// <summary>
  269. /// 鼠标双击
  270. /// </summary>
  271. /// <param name="sender"></param>
  272. /// <param name="e"></param>
  273. private void flowCanvas_MouseDoubleClick(object sender, MouseEventArgs e)
  274. {
  275. if (this._flowBoxMode == FlowBoxMode.Display)
  276. {
  277. return;
  278. }
  279. NodeDoubleClickEventHandler nodeDoubleClick = this.GetNodeDoubleClickEventHandler();
  280. if (nodeDoubleClick != null)
  281. {
  282. FlowNode node = this._itemManager.MouseDoubleClick(e);
  283. if (node != null)
  284. {
  285. nodeDoubleClick(this, new NodeEventArgs(node));
  286. }
  287. }
  288. }
  289. #endregion 画布事件
  290. #endregion 事件处理
  291. #region 公有方法
  292. #region 新建项目
  293. /// <summary>
  294. /// 停止新建节点(鼠标操作)
  295. /// </summary>
  296. public void StopDrawingItem()
  297. {
  298. //if (this._flowBoxMode != FlowBoxMode.Edit)
  299. //{
  300. // return;
  301. //}
  302. this._itemManager.StopDrawingItem();
  303. }
  304. /// <summary>
  305. /// 新建节点(鼠标操作,指定位置)
  306. /// </summary>
  307. public FlowNode DrawingNode()
  308. {
  309. if (this._flowBoxMode != FlowBoxMode.Edit)
  310. {
  311. return null;
  312. }
  313. return this._itemManager.DrawingNode();
  314. }
  315. /// <summary>
  316. /// 新建线段(鼠标操作,指定位置)
  317. /// </summary>
  318. public FlowLine DrawingLine()
  319. {
  320. if (this._flowBoxMode != FlowBoxMode.Edit)
  321. {
  322. return null;
  323. }
  324. return this._itemManager.DrawingLine();
  325. }
  326. /// <summary>
  327. /// 新建节点(默认位置)
  328. /// </summary>
  329. /// <returns>节点</returns>
  330. public FlowNode NewNode()
  331. {
  332. if (this._flowBoxMode != FlowBoxMode.Edit)
  333. {
  334. return null;
  335. }
  336. return this._itemManager.NewNode();
  337. }
  338. /// <summary>
  339. /// 新建节点(指定位置)
  340. /// </summary>
  341. /// <param name="location">节点位置</param>
  342. public FlowNode NewNode(Point location)
  343. {
  344. if (this._flowBoxMode != FlowBoxMode.Edit)
  345. {
  346. return null;
  347. }
  348. return this._itemManager.NewNode(location);
  349. }
  350. /// <summary>
  351. /// 新建线段(默认位置)
  352. /// </summary>
  353. /// <returns>线段</returns>
  354. public FlowLine NewLine()
  355. {
  356. if (this._flowBoxMode != FlowBoxMode.Edit)
  357. {
  358. return null;
  359. }
  360. return this._itemManager.NewLine();
  361. }
  362. /// <summary>
  363. /// 新建线段(指定位置)
  364. /// </summary>
  365. /// <param name="pointBegin">起始点</param>
  366. /// <param name="pointEnd">结束点</param>
  367. /// <returns>线段</returns>
  368. public FlowLine NewLine(Point pointBegin, Point pointEnd)
  369. {
  370. if (this._flowBoxMode != FlowBoxMode.Edit)
  371. {
  372. return null;
  373. }
  374. return this._itemManager.NewLine(pointBegin, pointEnd);
  375. }
  376. #endregion 新建项目
  377. #region 刷新项目
  378. /// <summary>
  379. /// 刷新Item的图片
  380. /// </summary>
  381. public void RefreshItems()
  382. {
  383. this._itemManager.RefreshItems();
  384. }
  385. #endregion 刷新项目
  386. #region 操作项目
  387. /// <summary>
  388. /// 全部不选择
  389. /// </summary>
  390. public void ClearSelection()
  391. {
  392. if (this._flowBoxMode == FlowBoxMode.Display)
  393. {
  394. return;
  395. }
  396. this._itemManager.ClearSelection();
  397. this._itemManager.RefreshItems();
  398. }
  399. /// <summary>
  400. /// 全部选择
  401. /// </summary>
  402. public void SelectAllItem()
  403. {
  404. if (this._flowBoxMode == FlowBoxMode.Display)
  405. {
  406. return;
  407. }
  408. this._itemManager.SelectAllItems();
  409. this._itemManager.RefreshItems();
  410. }
  411. /// <summary>
  412. /// 删除全部项目
  413. /// </summary>
  414. public void DeleteAllItem()
  415. {
  416. if (this._flowBoxMode != FlowBoxMode.Edit)
  417. {
  418. return;
  419. }
  420. this._itemManager.DeleteAllItems();
  421. this._itemManager.RefreshItems();
  422. }
  423. /// <summary>
  424. /// 删除选择项目
  425. /// </summary>
  426. public void DeleteSelectedItem()
  427. {
  428. if (this._flowBoxMode != FlowBoxMode.Edit)
  429. {
  430. return;
  431. }
  432. this._itemManager.DeleteSelectedItems();
  433. this._itemManager.RefreshItems();
  434. }
  435. /// <summary>
  436. /// 撤销删除
  437. /// </summary>
  438. /// <param name="nodeCode">节点编码</param>
  439. public void UndeleteNodeByNodeCode(string nodeCode)
  440. {
  441. foreach (FlowNode node in this._itemManager.DeletedNodes)
  442. {
  443. if (node.ItemCode == nodeCode)
  444. {
  445. this._itemManager.DeletedNodes.Remove(node);
  446. this._itemManager.Items.Add(node);
  447. if (node.NodeState == FlowNodeState.Detached ||
  448. node.NodeState == FlowNodeState.Added)
  449. {
  450. node.NodeState = FlowNodeState.Added;
  451. }
  452. else
  453. {
  454. node.NodeState = FlowNodeState.Unchanged;
  455. }
  456. return;
  457. }
  458. }
  459. }
  460. ///// <summary>
  461. ///// 对齐选定的节点
  462. ///// </summary>
  463. ///// <param name="arrangeType">对齐方式</param>
  464. //public void ArrangeNodes(ArrangeType arrangeType)
  465. //{
  466. // this._itemManager.ArrangeNodes(arrangeType);
  467. //}
  468. ///// <summary>
  469. ///// 顶部对齐所有选中的节点
  470. ///// </summary>
  471. //public void ArrangeNodesTop()
  472. //{
  473. // this._itemManager.ArrangeNodesTop();
  474. //}
  475. ///// <summary>
  476. ///// 底部对齐所有选中的节点
  477. ///// </summary>
  478. //public void ArrangeNodesBottom()
  479. //{
  480. // this._itemManager.ArrangeNodesBottom();
  481. //}
  482. ///// <summary>
  483. ///// 左侧对齐所有选中的节点
  484. ///// </summary>
  485. //public void ArrangeNodesLeft()
  486. //{
  487. // this._itemManager.ArrangeNodesLeft();
  488. //}
  489. ///// <summary>
  490. ///// 右侧对齐所有选中的节点
  491. ///// </summary>
  492. //public void ArrangeNodesRight()
  493. //{
  494. // this._itemManager.ArrangeNodesRight();
  495. //}
  496. ///// <summary>
  497. ///// 垂直方向中心对齐所有选中的节点
  498. ///// </summary>
  499. //public void ArrangeNodesVerticalCenter()
  500. //{
  501. // this._itemManager.ArrangeNodesVerticalCenter();
  502. //}
  503. ///// <summary>
  504. ///// 水平方向中心对齐所有选中的节点
  505. ///// </summary>
  506. //public void ArrangeNodesHorizontalCenter()
  507. //{
  508. // this._itemManager.ArrangeNodesHorizontalCenter();
  509. //}
  510. #endregion 操作项目
  511. #region 流程校验
  512. /// <summary>
  513. /// 不完整的线
  514. /// </summary>
  515. private List<FlowLine> _incompleteLines = new List<FlowLine>();
  516. /// <summary>
  517. /// 全部线
  518. /// </summary>
  519. private List<FlowLine> _checkedLines = new List<FlowLine>();
  520. /// <summary>
  521. /// 独立的节点
  522. /// </summary>
  523. private List<FlowNode> _aloneNodes = new List<FlowNode>();
  524. /// <summary>
  525. /// 开始节点(包括前置节点是可以跳过的)
  526. /// </summary>
  527. private List<FlowNode> _beginNodes = new List<FlowNode>();
  528. /// <summary>
  529. /// 结束节点(包括后置节点是可以跳过的)
  530. /// </summary>
  531. private List<FlowNode> _endNodes = new List<FlowNode>();
  532. /// <summary>
  533. /// 中间节点
  534. /// </summary>
  535. private List<FlowNode> _generalNodes = new List<FlowNode>();
  536. /// <summary>
  537. /// 全部节点
  538. /// </summary>
  539. private List<FlowNode> _checkedNodes = new List<FlowNode>();
  540. /// <summary>
  541. /// 不完整的线(CheckFlow调用后)
  542. /// </summary>
  543. public List<FlowLine> IncompleteLines
  544. {
  545. get
  546. {
  547. return this._incompleteLines;
  548. }
  549. }
  550. /// <summary>
  551. /// 全部线(CheckFlow调用后)
  552. /// </summary>
  553. public List<FlowLine> CheckedLines
  554. {
  555. get
  556. {
  557. return this._checkedLines;
  558. }
  559. }
  560. /// <summary>
  561. /// 独立的节点(CheckFlow调用后)
  562. /// </summary>
  563. public List<FlowNode> AloneNodes
  564. {
  565. get
  566. {
  567. return this._aloneNodes;
  568. }
  569. }
  570. /// <summary>
  571. /// 开始节点(包括前置节点是可以跳过的)(CheckFlow调用后)
  572. /// </summary>
  573. public List<FlowNode> BeginNodes
  574. {
  575. get
  576. {
  577. return this._beginNodes;
  578. }
  579. }
  580. /// <summary>
  581. /// 结束节点(包括前置节点是可以跳过的)(CheckFlow调用后)
  582. /// </summary>
  583. public List<FlowNode> EndNodes
  584. {
  585. get
  586. {
  587. return this._endNodes;
  588. }
  589. }
  590. /// <summary>
  591. /// 中间节点(CheckFlow调用后)
  592. /// </summary>
  593. public List<FlowNode> GeneralNodes
  594. {
  595. get
  596. {
  597. return this._generalNodes;
  598. }
  599. }
  600. /// <summary>
  601. /// 全部节点(CheckFlow调用后)
  602. /// </summary>
  603. public List<FlowNode> CheckedNodes
  604. {
  605. get
  606. {
  607. return this._checkedNodes;
  608. }
  609. }
  610. /// <summary>
  611. /// 校验流程
  612. /// </summary>
  613. /// <returns></returns>
  614. public bool CheckFlow()
  615. {
  616. this._incompleteLines.Clear();
  617. this._checkedLines.Clear();
  618. this._beginNodes.Clear();
  619. this._endNodes.Clear();
  620. this._generalNodes.Clear();
  621. this._checkedNodes.Clear();
  622. //this.ErrorNodes.Clear();
  623. //this.ErrorMessages.Clear();
  624. bool checkResult = true;
  625. foreach (FlowItem item in this.Items)
  626. {
  627. if (item is FlowLine)
  628. {
  629. FlowLine line = item as FlowLine;
  630. if (line.AnchorBegin == AnchorKind.None ||
  631. line.AnchorEnd == AnchorKind.None)
  632. {
  633. this._incompleteLines.Add(line);
  634. }
  635. this._checkedLines.Add(line);
  636. }
  637. if (item is FlowNode)
  638. {
  639. FlowNode node = item as FlowNode;
  640. node.PreNodes.Clear();
  641. node.NextNodes.Clear();
  642. this._checkedNodes.Add(node);
  643. // 后续节点
  644. this.SetNextNodes(node, null);
  645. // 前置节点
  646. this.SetPreNodes(node, null);
  647. if (node.NodeType == FlowNodeType.Begin)
  648. {
  649. node.CheckedNodeType = FlowNodeType.Begin;
  650. this._beginNodes.Add(node);
  651. }
  652. else if (node.NodeType == FlowNodeType.End)
  653. {
  654. node.CheckedNodeType = FlowNodeType.End;
  655. this._endNodes.Add(node);
  656. if (node.CanSkip)
  657. {
  658. checkResult = false;
  659. //this.ErrorMessages.Add("结束节点不能跳过");
  660. }
  661. }
  662. else if (node.NodeType == FlowNodeType.Alone)
  663. {
  664. node.CheckedNodeType = FlowNodeType.Alone;
  665. this._aloneNodes.Add(node);
  666. }
  667. //else
  668. {
  669. bool isBegin = false;
  670. bool isEnd = false;
  671. foreach (FlowNode preNode in node.PreNodes)
  672. {
  673. if (preNode.NodeType == FlowNodeType.Begin &&
  674. preNode.CanSkip)
  675. {
  676. isBegin = true;
  677. break;
  678. }
  679. }
  680. foreach (FlowNode nextNode in node.NextNodes)
  681. {
  682. if (nextNode.NodeType == FlowNodeType.End &&
  683. nextNode.CanSkip)
  684. {
  685. isEnd = true;
  686. break;
  687. }
  688. }
  689. if (isBegin && isEnd)
  690. {
  691. node.CheckedNodeType = FlowNodeType.Alone;
  692. this._aloneNodes.Add(node);
  693. }
  694. else if (isBegin)
  695. {
  696. node.CheckedNodeType = FlowNodeType.Begin;
  697. this._beginNodes.Add(node);
  698. }
  699. else if (isEnd)
  700. {
  701. node.CheckedNodeType = FlowNodeType.End;
  702. this._endNodes.Add(node);
  703. }
  704. else
  705. {
  706. node.CheckedNodeType = FlowNodeType.General;
  707. this._generalNodes.Add(node);
  708. }
  709. }
  710. }
  711. }
  712. if (this._incompleteLines.Count > 0)
  713. {
  714. checkResult = false;
  715. //this.ErrorMessages.Add("有未完成的连接线");
  716. }
  717. if (this._beginNodes.Count == 0)
  718. {
  719. checkResult = false;
  720. //this.ErrorMessages.Add("没有开始节点");
  721. }
  722. if (this._endNodes.Count == 0)
  723. {
  724. checkResult = false;
  725. //this.ErrorMessages.Add("没有结束节点");
  726. }
  727. return checkResult;
  728. }
  729. #endregion 流程校验
  730. #region 保存、读取
  731. /// <summary>
  732. /// 清空流程
  733. /// </summary>
  734. public void ClearFlow()
  735. {
  736. //if (this._flowBoxMode != FlowBoxMode.Edit)
  737. //{
  738. // return;
  739. //}
  740. this._flowName = null;
  741. this._itemManager = new ItemManager(this, this.canvasBox);
  742. //this.canvasBox.SetCanvasSize(Consts.CANVAS_SIZE_DEFAULT);
  743. //this.canvasBox.CanvasControl.BackgroundImage = new Bitmap(this.GetType(), "Images.backImage1.gif");
  744. this._itemManager.RefreshItems();
  745. }
  746. /// <summary>
  747. /// 保存流程(1.1)
  748. /// </summary>
  749. /// <returns>二进制</returns>
  750. public byte[] SaveFlow()
  751. {
  752. if (this._flowBoxMode == FlowBoxMode.Display)
  753. {
  754. return null;
  755. }
  756. using (MemoryStream memoryStream = new MemoryStream())
  757. {
  758. if (this.SaveFlow(memoryStream))
  759. {
  760. return memoryStream.ToArray();
  761. }
  762. }
  763. return null;
  764. }
  765. /// <summary>
  766. /// 保存流程(1.1)
  767. /// </summary>
  768. /// <param name="stream">流</param>
  769. /// <returns>是否成功</returns>
  770. public bool SaveFlow(Stream stream)
  771. {
  772. if (this._flowBoxMode == FlowBoxMode.Display)
  773. {
  774. return false;
  775. }
  776. if (stream == null)
  777. {
  778. return false;
  779. }
  780. try
  781. {
  782. XmlDocument xmlDoc = this.SaveFlowXmlDoc();
  783. if (xmlDoc != null)
  784. {
  785. xmlDoc.Save(stream);
  786. return true;
  787. }
  788. return false;
  789. }
  790. catch
  791. {
  792. return false;
  793. }
  794. }
  795. /// <summary>
  796. /// 保存流程(1.1)
  797. /// </summary>
  798. /// <returns>XML</returns>
  799. public string SaveFlowXml()
  800. {
  801. if (this._flowBoxMode == FlowBoxMode.Display)
  802. {
  803. return null;
  804. }
  805. XmlDocument xmlDoc = this.SaveFlowXmlDoc();
  806. return xmlDoc.OuterXml;
  807. }
  808. /// <summary>
  809. /// 保存流程(1.1)
  810. /// </summary>
  811. /// <returns>XML</returns>
  812. public XmlDocument SaveFlowXmlDoc()
  813. {
  814. if (this._flowBoxMode == FlowBoxMode.Display)
  815. {
  816. return null;
  817. }
  818. return this.SaveFlowXmlDoc_1_1();
  819. }
  820. /// <summary>
  821. /// 加载流程
  822. /// </summary>
  823. /// <param name="bytes">二进制</param>
  824. /// <returns>是否成功</returns>
  825. public bool LoadFlow(byte[] bytes)
  826. {
  827. if (bytes != null && 0 < bytes.Length)
  828. {
  829. using (MemoryStream memoryStream = new MemoryStream(bytes))
  830. {
  831. memoryStream.Position = 0;
  832. return this.LoadFlow(memoryStream);
  833. }
  834. }
  835. return false;
  836. }
  837. /// <summary>
  838. /// 加载流程
  839. /// </summary>
  840. /// <param name="stream">流</param>
  841. /// <returns>是否成功</returns>
  842. public bool LoadFlow(Stream stream)
  843. {
  844. //this.ClearFlow();
  845. this._itemManager = new ItemManager(this, this.canvasBox);
  846. if (stream == null)
  847. {
  848. this._itemManager.RefreshItems();
  849. return false;
  850. }
  851. try
  852. {
  853. XmlDocument xmlDoc = new XmlDocument();
  854. xmlDoc.Load(stream);
  855. // 如果要改图里的东西,在这改,再保存。
  856. //xmlDoc.Load("C:\\Users\\Administrator\\流程图.txt");
  857. bool result = this.LoadFlowByVersion(xmlDoc);
  858. this._itemManager.RefreshItems();
  859. return result;
  860. /* 读取xml异常时,将流程xml保存本地后直接读取本地文件,然后再输出导入到db中
  861. XmlDocument xmlDoc = new XmlDocument();
  862. xmlDoc.Load("D:\\yyy");
  863. bool result = this.LoadFlowByVersion(xmlDoc);
  864. File.WriteAllText("D:\\www", this.SaveFlowXml());
  865. */
  866. }
  867. catch
  868. {
  869. this._itemManager.RefreshItems();
  870. return false;
  871. }
  872. }
  873. /// <summary>
  874. /// 加载流程
  875. /// </summary>
  876. /// <param name="xml">xml</param>
  877. /// <returns>是否成功</returns>
  878. public bool LoadFlow(string xml)
  879. {
  880. //this.ClearFlow();
  881. this._itemManager = new ItemManager(this, this.canvasBox);
  882. if (string.IsNullOrWhiteSpace(xml))
  883. {
  884. this._itemManager.RefreshItems();
  885. return false;
  886. }
  887. try
  888. {
  889. XmlDocument xmlDoc = new XmlDocument();
  890. xmlDoc.LoadXml(xml);
  891. bool result = this.LoadFlowByVersion(xmlDoc);
  892. this._itemManager.RefreshItems();
  893. return result;
  894. }
  895. catch
  896. {
  897. this._itemManager.RefreshItems();
  898. return false;
  899. }
  900. }
  901. #endregion 保存、读取
  902. #endregion 公有方法
  903. #region 内部方法
  904. #endregion 内部方法
  905. #region 受保护方法
  906. #endregion 受保护方法
  907. #region 私有方法
  908. #region 事件相关
  909. /// <summary>
  910. /// 获取节点双击的注册事件
  911. /// </summary>
  912. /// <returns></returns>
  913. private NodeDoubleClickEventHandler GetNodeDoubleClickEventHandler()
  914. {
  915. if (this._flowBoxMode < FlowBoxMode.LimitEdit)
  916. {
  917. return null;
  918. }
  919. return (NodeDoubleClickEventHandler)base.Events[FlowBox.EventNodeDoubleClick];
  920. }
  921. /// <summary>
  922. /// 流程节点加载时事件
  923. /// </summary>
  924. /// <returns></returns>
  925. private FlowNodeLoadingEventHandler GetFlowNodeLoadingEventHandler()
  926. {
  927. return (FlowNodeLoadingEventHandler)base.Events[FlowBox.EventFlowNodeLoading];
  928. }
  929. #endregion 事件相关
  930. #region 流程校验
  931. /// <summary>
  932. /// 设置前置节点
  933. /// </summary>
  934. /// <param name="node">需要设置的节点</param>
  935. /// <param name="preNodes">前置节点集合</param>
  936. private void SetPreNodes(FlowNode node, List<FlowNode> preNodes)
  937. {
  938. preNodes = (preNodes == null) ? node.PreNodes : preNodes;
  939. foreach (FlowLine inLine in node.InLines)
  940. {
  941. if (inLine.AnchorBegin != AnchorKind.None)
  942. {
  943. FlowNode preNode = inLine.NodeBegin;
  944. if (preNode != null && !preNodes.Contains(preNode))
  945. {
  946. preNodes.Add(preNode);
  947. if (preNode.CanSkip)
  948. {
  949. this.SetPreNodes(preNode, preNodes);
  950. }
  951. }
  952. }
  953. }
  954. }
  955. /// <summary>
  956. /// 设置后续节点
  957. /// </summary>
  958. /// <param name="node">需要设置的节点</param>
  959. /// <param name="nextNodes">后续节点集合</param>
  960. private void SetNextNodes(FlowNode node, List<FlowNode> nextNodes)
  961. {
  962. nextNodes = (nextNodes == null) ? node.NextNodes : nextNodes;
  963. foreach (FlowLine outLine in node.OutLines)
  964. {
  965. if (outLine.AnchorEnd != AnchorKind.None)
  966. {
  967. FlowNode nextNode = outLine.NodeEnd;
  968. if (nextNode != null && !nextNodes.Contains(nextNode))
  969. {
  970. nextNodes.Add(nextNode);
  971. if (nextNode.CanSkip)
  972. {
  973. this.SetNextNodes(nextNode, nextNodes);
  974. }
  975. }
  976. }
  977. }
  978. }
  979. #endregion 流程校验
  980. #region 保存、读取
  981. /// <summary>
  982. /// 保存流程(1.1)
  983. /// </summary>
  984. /// <returns>XML</returns>
  985. private XmlDocument SaveFlowXmlDoc_1_1()
  986. {
  987. XmlDocument xmlDoc = new XmlDocument();
  988. // 创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
  989. xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
  990. // 根节点
  991. XmlNode root = xmlDoc.CreateElement("FlowSetting");
  992. // 版本
  993. XmlAttribute version = xmlDoc.CreateAttribute("Version");
  994. version.Value = "1.1";
  995. root.Attributes.Append(version);
  996. // 编码
  997. XmlAttribute managerCode = xmlDoc.CreateAttribute("ManagerCode");
  998. managerCode.Value = this._itemManager.ManagerCode;
  999. root.Attributes.Append(managerCode);
  1000. // 新项目ID
  1001. //XmlNode newItemID = xmlDoc.CreateElement("NewItemID");
  1002. //newItemID.AppendChild(xmlDoc.CreateTextNode(this._itemManager.GetNewItemID().ToString()));
  1003. //root.AppendChild(newItemID);
  1004. XmlAttribute newItemID = xmlDoc.CreateAttribute("NewItemID");
  1005. newItemID.Value = this._itemManager.GetNewItemID().ToString();
  1006. root.Attributes.Append(newItemID);
  1007. // 流程名称
  1008. XmlNode flowName = xmlDoc.CreateElement("FlowName");
  1009. flowName.AppendChild(xmlDoc.CreateCDataSection(this._flowName));
  1010. root.AppendChild(flowName);
  1011. // 画布大小
  1012. XmlNode canvasSize = xmlDoc.CreateElement("CanvasSize");
  1013. canvasSize.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(this.CanvasSize)));
  1014. root.AppendChild(canvasSize);
  1015. //// 画布背景
  1016. //XmlNode backgroundImage = xmlDoc.CreateElement("BackgroundImage");
  1017. //backgroundImage.Value = Converter.ToXMLString(this.CanvasBox.CanvasBackgroundImage);
  1018. //backgroundImage.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(this.CanvasBox.CanvasBackgroundImage)));
  1019. //flowNode.AppendChild(backgroundImage);
  1020. // FlowItems
  1021. XmlNode flowItems = xmlDoc.CreateElement("FlowItems");
  1022. foreach (FlowItem item in this._itemManager.Items)
  1023. {
  1024. XmlNode flowItem = xmlDoc.CreateElement("FlowItem");
  1025. // ItemID
  1026. XmlAttribute itemID = xmlDoc.CreateAttribute("ItemID");
  1027. itemID.Value = item.ItemID.ToString();
  1028. flowItem.Attributes.Append(itemID);
  1029. // OnlyCode
  1030. XmlAttribute itemCode = xmlDoc.CreateAttribute("ItemCode");
  1031. itemCode.Value = item.ItemCode;
  1032. flowItem.Attributes.Append(itemCode);
  1033. // ItemType
  1034. XmlAttribute itemType = xmlDoc.CreateAttribute("ItemType");
  1035. itemType.Value = item.GetType().Name;
  1036. flowItem.Attributes.Append(itemType);
  1037. // ID
  1038. XmlNode id = xmlDoc.CreateElement("ID");
  1039. id.AppendChild(xmlDoc.CreateTextNode(item.ID.ToString()));
  1040. flowItem.AppendChild(id);
  1041. // Code
  1042. XmlNode code = xmlDoc.CreateElement("Code");
  1043. code.AppendChild(xmlDoc.CreateCDataSection(item.Code));
  1044. flowItem.AppendChild(code);
  1045. // Name
  1046. XmlNode name = xmlDoc.CreateElement("Name");
  1047. name.AppendChild(xmlDoc.CreateCDataSection(item.Name));
  1048. flowItem.AppendChild(name);
  1049. // Remark
  1050. XmlNode remark = xmlDoc.CreateElement("Remark");
  1051. remark.AppendChild(xmlDoc.CreateCDataSection(item.Remark));
  1052. flowItem.AppendChild(remark);
  1053. // ShowName
  1054. XmlNode showName = xmlDoc.CreateElement("ShowName");
  1055. showName.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(item.ShowName)));
  1056. flowItem.AppendChild(showName);
  1057. // Font
  1058. XmlNode font = xmlDoc.CreateElement("Font");
  1059. font.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(item.Font)));
  1060. flowItem.AppendChild(font);
  1061. // FontColor
  1062. XmlNode fontColor = xmlDoc.CreateElement("FontColor");
  1063. fontColor.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(item.FontColor)));
  1064. flowItem.AppendChild(fontColor);
  1065. // TagSerializable todo object
  1066. XmlNode ts = xmlDoc.CreateElement("TS");
  1067. if (item.TagSerializable != null)
  1068. {
  1069. using (MemoryStream memoryStream = new MemoryStream())
  1070. {
  1071. BinaryFormatter formatter = new BinaryFormatter();
  1072. formatter.Serialize(memoryStream, item.TagSerializable);
  1073. ts.AppendChild(xmlDoc.CreateTextNode(Convert.ToBase64String(memoryStream.ToArray())));
  1074. }
  1075. }
  1076. flowItem.AppendChild(ts);
  1077. if (item is FlowNode)
  1078. {
  1079. FlowNode node = item as FlowNode;
  1080. // Bounds
  1081. XmlNode bounds = xmlDoc.CreateElement("Bounds");
  1082. bounds.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(node.Bounds)));
  1083. flowItem.AppendChild(bounds);
  1084. // BorderWidth
  1085. XmlNode borderWidth = xmlDoc.CreateElement("BorderWidth");
  1086. borderWidth.AppendChild(xmlDoc.CreateTextNode(node.BorderWidth.ToString()));
  1087. flowItem.AppendChild(borderWidth);
  1088. // BorderColor
  1089. XmlNode borderColor = xmlDoc.CreateElement("BorderColor");
  1090. borderColor.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(node.BorderColor)));
  1091. flowItem.AppendChild(borderColor);
  1092. // FillColor
  1093. XmlNode fillColor = xmlDoc.CreateElement("FillColor");
  1094. if (node.FillColor.HasValue)
  1095. {
  1096. fillColor.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(node.FillColor)));
  1097. }
  1098. flowItem.AppendChild(fillColor);
  1099. // CanSkip
  1100. XmlNode canSkip = xmlDoc.CreateElement("CanSkip");
  1101. canSkip.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(node.CanSkip)));
  1102. flowItem.AppendChild(canSkip);
  1103. // NodeImageType
  1104. XmlNode nodeImageType = xmlDoc.CreateElement("NodeImageType");
  1105. if (node.NodeImageType.HasValue)
  1106. {
  1107. nodeImageType.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(node.NodeImageType)));
  1108. }
  1109. flowItem.AppendChild(nodeImageType);
  1110. // NodeImage
  1111. XmlNode nodeImage = xmlDoc.CreateElement("NodeImage");
  1112. if (!node.NodeImageType.HasValue && node.NodeImage != null)
  1113. {
  1114. nodeImage.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(node.NodeImage)));
  1115. }
  1116. flowItem.AppendChild(nodeImage);
  1117. }
  1118. if (item is FlowLine)
  1119. {
  1120. FlowLine line = item as FlowLine;
  1121. // PointBegin
  1122. XmlNode pointBegin = xmlDoc.CreateElement("PointBegin");
  1123. pointBegin.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(line.PointBegin)));
  1124. flowItem.AppendChild(pointBegin);
  1125. // PointEnd
  1126. XmlNode pointEnd = xmlDoc.CreateElement("PointEnd");
  1127. pointEnd.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(line.PointEnd)));
  1128. flowItem.AppendChild(pointEnd);
  1129. // LineColor
  1130. XmlNode lineColor = xmlDoc.CreateElement("LineColor");
  1131. lineColor.AppendChild(xmlDoc.CreateTextNode(Converter.ToXMLString(line.LineColor)));
  1132. flowItem.AppendChild(lineColor);
  1133. // LineWidth
  1134. XmlNode lineWidth = xmlDoc.CreateElement("LineWidth");
  1135. lineWidth.AppendChild(xmlDoc.CreateTextNode(line.LineWidth.ToString()));
  1136. flowItem.AppendChild(lineWidth);
  1137. // AnchorBegin
  1138. XmlNode anchorBegin = xmlDoc.CreateElement("AnchorBegin");
  1139. if (line.NodeBegin == null)
  1140. {
  1141. line.AnchorBegin = AnchorKind.None;
  1142. }
  1143. else
  1144. {
  1145. anchorBegin.AppendChild(xmlDoc.CreateTextNode(line.NodeBegin.ItemCode));
  1146. }
  1147. XmlAttribute kindBegin = xmlDoc.CreateAttribute("Kind");
  1148. kindBegin.Value = Converter.ToXMLString(line.AnchorBegin);
  1149. anchorBegin.Attributes.Append(kindBegin);
  1150. flowItem.AppendChild(anchorBegin);
  1151. // AnchorEnd
  1152. XmlNode anchorEnd = xmlDoc.CreateElement("AnchorEnd");
  1153. if (line.NodeEnd == null)
  1154. {
  1155. line.AnchorEnd = AnchorKind.None;
  1156. }
  1157. else
  1158. {
  1159. anchorEnd.AppendChild(xmlDoc.CreateTextNode(line.NodeEnd.ItemCode));
  1160. }
  1161. XmlAttribute kindEnd = xmlDoc.CreateAttribute("Kind");
  1162. kindEnd.Value = Converter.ToXMLString(line.AnchorEnd);
  1163. anchorEnd.Attributes.Append(kindEnd);
  1164. flowItem.AppendChild(anchorEnd);
  1165. }
  1166. flowItems.AppendChild(flowItem);
  1167. }
  1168. root.AppendChild(flowItems);
  1169. xmlDoc.AppendChild(root);
  1170. return xmlDoc;
  1171. }
  1172. /// <summary>
  1173. /// 根据文档版本加载流程
  1174. /// </summary>
  1175. /// <param name="xmlDoc">xml</param>
  1176. /// <returns>是否成功</returns>
  1177. private bool LoadFlowByVersion(XmlDocument xmlDoc)
  1178. {
  1179. bool result = false;
  1180. // 根节点
  1181. XmlNode root = xmlDoc.DocumentElement;
  1182. if (root == null)
  1183. {
  1184. return result;
  1185. }
  1186. if ("WorkFlow" == root.Name)
  1187. {
  1188. this._loadVersion = new Version(1, 0);
  1189. this.LoadFlow_1_0(root);
  1190. result = true;
  1191. }
  1192. else if ("FlowSetting" == root.Name)
  1193. {
  1194. // 版本
  1195. string version = root.Attributes["Version"].Value;
  1196. switch (version)
  1197. {
  1198. case "1.1":
  1199. this._loadVersion = new Version(1, 1);
  1200. this.LoadFlow_1_1(root);
  1201. result = true;
  1202. break;
  1203. default:
  1204. break;
  1205. }
  1206. }
  1207. return result;
  1208. }
  1209. /// <summary>
  1210. /// 加载流程(1.0)
  1211. /// </summary>
  1212. /// <param name="root">XML</param>
  1213. private void LoadFlow_1_0(XmlNode root)
  1214. {
  1215. int itemCount = 0;
  1216. //获取结点列表
  1217. XmlNodeList xmlNodeChilds = root.SelectNodes("//arrNodeList//node");
  1218. foreach (XmlNode xmlNode in xmlNodeChilds)
  1219. {
  1220. itemCount++;
  1221. XmlNodeList xnl = xmlNode.ChildNodes;
  1222. int index = Convert.ToInt32(xmlNode.Attributes.Item(0).InnerText);
  1223. int nodetype = Convert.ToInt32(xmlNode.Attributes.Item(1).InnerText);
  1224. //itemCount = Math.Max(itemCount, index);
  1225. FlowNode node = new FlowNode(this._itemManager, index, Guid.NewGuid().ToString().ToUpper());
  1226. node.NodeImageType = nodetype;
  1227. node.Name = xnl.Item(0).InnerText;
  1228. string fontname = xnl.Item(1).InnerText;
  1229. int fontSize = Convert.ToInt32(xnl.Item(2).InnerText);
  1230. node.Font = new System.Drawing.Font(fontname, fontSize);
  1231. node.FontColor = System.Drawing.Color.FromArgb(int.Parse(xnl.Item(3).InnerText));
  1232. node.BorderWidth = 0;
  1233. node.BorderColor = Color.FromArgb(int.Parse(xnl.Item(4).InnerText));
  1234. node.FillColor = Color.FromArgb(int.Parse(xnl.Item(5).InnerText));
  1235. int height = Convert.ToInt32(xnl.Item(6).InnerText);
  1236. int width = Convert.ToInt32(xnl.Item(7).InnerText);
  1237. int x = Convert.ToInt32(xnl.Item(8).InnerText);
  1238. int y = Convert.ToInt32(xnl.Item(9).InnerText);
  1239. node.Bounds = new Rectangle(x, y, width, height);
  1240. node.Remark = xnl.Item(17).InnerText;
  1241. FlowNodeLoadingEventHandler flowNodeLoading = this.GetFlowNodeLoadingEventHandler();
  1242. if (flowNodeLoading != null)
  1243. {
  1244. flowNodeLoading(this, new NodeEventArgs(node));
  1245. }
  1246. this._itemManager.Items.Add(node);
  1247. this._itemManager.AllNodes.Add(node);
  1248. }
  1249. //获取线列表
  1250. XmlNodeList xmlLineChilds = root.SelectNodes("//arrLineList//line");
  1251. foreach (XmlNode xmlLine in xmlLineChilds)
  1252. {
  1253. XmlNodeList xnl = xmlLine.ChildNodes;
  1254. int index = Convert.ToInt32(xmlLine.Attributes.Item(0).InnerText);
  1255. int lineType = Convert.ToInt32(xmlLine.Attributes.Item(1).InnerText);
  1256. FlowLine line = new FlowLine(this._itemManager, ++itemCount, Guid.NewGuid().ToString().ToUpper());
  1257. int x0 = Convert.ToInt32(xnl.Item(0).InnerText);
  1258. int y0 = Convert.ToInt32(xnl.Item(1).InnerText);
  1259. int x1 = Convert.ToInt32(xnl.Item(2).InnerText);
  1260. int y1 = Convert.ToInt32(xnl.Item(3).InnerText);
  1261. line.PointBegin = new Point(x0, y0);
  1262. line.PointEnd = new Point(x1, y1);
  1263. if (lineType == 1)
  1264. {
  1265. //line.X2 = Convert.ToInt32(xnl.Item(4).InnerText);
  1266. //line.Y2 = Convert.ToInt32(xnl.Item(5).InnerText);
  1267. //line.X3 = Convert.ToInt32(xnl.Item(6).InnerText);
  1268. //line.Y3 = Convert.ToInt32(xnl.Item(7).InnerText);
  1269. //line.LineNodeCount = Convert.ToInt32(xnl.Item(8).InnerText);
  1270. if (xnl.Item(9).InnerText != "null")
  1271. {
  1272. int firstIndex = Convert.ToInt32(xnl.Item(9).InnerText);
  1273. line.NodeBegin = this._itemManager.Items[firstIndex] as FlowNode;
  1274. if (line.NodeBegin != null)
  1275. {
  1276. line.NodeBegin.OutLines.Add(line);
  1277. int index1 = Convert.ToInt32(xnl.Item(11).InnerText);
  1278. switch (index1)
  1279. {
  1280. case 0:
  1281. line.AnchorBegin = AnchorKind.Top;
  1282. line.NodeBegin.TopLines.Add(line);
  1283. break;
  1284. case 1:
  1285. line.AnchorBegin = AnchorKind.Left;
  1286. line.NodeBegin.LeftLines.Add(line);
  1287. break;
  1288. case 2:
  1289. line.AnchorBegin = AnchorKind.Right;
  1290. line.NodeBegin.RightLines.Add(line);
  1291. break;
  1292. case 3:
  1293. line.AnchorBegin = AnchorKind.Bottom;
  1294. line.NodeBegin.BottomLines.Add(line);
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. if (xnl.Item(10).InnerText != "null")
  1302. {
  1303. int secondIndex = Convert.ToInt32(xnl.Item(10).InnerText);
  1304. line.NodeEnd = this._itemManager.Items[secondIndex] as FlowNode;
  1305. if (line.NodeEnd != null)
  1306. {
  1307. line.NodeEnd.InLines.Add(line);
  1308. int index2 = Convert.ToInt32(xnl.Item(12).InnerText);
  1309. switch (index2)
  1310. {
  1311. case 0:
  1312. line.AnchorEnd = AnchorKind.Top;
  1313. line.NodeEnd.TopLines.Add(line);
  1314. break;
  1315. case 1:
  1316. line.AnchorEnd = AnchorKind.Left;
  1317. line.NodeEnd.LeftLines.Add(line);
  1318. break;
  1319. case 2:
  1320. line.AnchorEnd = AnchorKind.Right;
  1321. line.NodeEnd.RightLines.Add(line);
  1322. break;
  1323. case 3:
  1324. line.AnchorEnd = AnchorKind.Bottom;
  1325. line.NodeEnd.BottomLines.Add(line);
  1326. break;
  1327. default:
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. line.LineWidth = Convert.ToInt32(xnl.Item(13).InnerText);
  1333. line.LineColor = System.Drawing.Color.FromArgb(int.Parse(xnl.Item(14).InnerText));
  1334. line.Name = xnl.Item(15).InnerText;
  1335. }
  1336. else if (lineType == 0)
  1337. {
  1338. //line.LineNodeCount = Convert.ToInt32(xnl.Item(4).InnerText);
  1339. if (xnl.Item(5).InnerText != "null")
  1340. {
  1341. int firstIndex = Convert.ToInt32(xnl.Item(5).InnerText);
  1342. line.NodeBegin = this._itemManager.Items[firstIndex] as FlowNode;
  1343. if (line.NodeBegin != null)
  1344. {
  1345. line.NodeBegin.OutLines.Add(line);
  1346. int index3 = Convert.ToInt32(xnl.Item(7).InnerText);
  1347. switch (index3)
  1348. {
  1349. case 0:
  1350. line.AnchorBegin = AnchorKind.Top;
  1351. line.NodeBegin.TopLines.Add(line);
  1352. break;
  1353. case 1:
  1354. line.AnchorBegin = AnchorKind.Left;
  1355. line.NodeBegin.LeftLines.Add(line);
  1356. break;
  1357. case 2:
  1358. line.AnchorBegin = AnchorKind.Right;
  1359. line.NodeBegin.RightLines.Add(line);
  1360. break;
  1361. case 3:
  1362. line.AnchorBegin = AnchorKind.Bottom;
  1363. line.NodeBegin.BottomLines.Add(line);
  1364. break;
  1365. default:
  1366. break;
  1367. }
  1368. }
  1369. }
  1370. if (xnl.Item(6).InnerText != "null")
  1371. {
  1372. int secondIndex = Convert.ToInt32(xnl.Item(6).InnerText);
  1373. line.NodeEnd = this._itemManager.Items[secondIndex] as FlowNode;
  1374. if (line.NodeEnd != null)
  1375. {
  1376. line.NodeEnd.InLines.Add(line);
  1377. int index4 = Convert.ToInt32(xnl.Item(8).InnerText);
  1378. switch (index4)
  1379. {
  1380. case 0:
  1381. line.AnchorEnd = AnchorKind.Top;
  1382. line.NodeEnd.TopLines.Add(line);
  1383. break;
  1384. case 1:
  1385. line.AnchorEnd = AnchorKind.Left;
  1386. line.NodeEnd.LeftLines.Add(line);
  1387. break;
  1388. case 2:
  1389. line.AnchorEnd = AnchorKind.Right;
  1390. line.NodeEnd.RightLines.Add(line);
  1391. break;
  1392. case 3:
  1393. line.AnchorEnd = AnchorKind.Bottom;
  1394. line.NodeEnd.BottomLines.Add(line);
  1395. break;
  1396. default:
  1397. break;
  1398. }
  1399. }
  1400. }
  1401. line.LineWidth = Convert.ToInt32(xnl.Item(9).InnerText);
  1402. line.LineColor = System.Drawing.Color.FromArgb(int.Parse(xnl.Item(10).InnerText));
  1403. line.Name = xnl.Item(11).InnerText;
  1404. }
  1405. this._itemManager.Items.Add(line);
  1406. }
  1407. // 新项目ID
  1408. this._itemManager.SetNewItemID(itemCount);
  1409. // 画布大小
  1410. this.canvasBox.SetCanvasSize(Consts.CANVAS_SIZE_DEFAULT);
  1411. }
  1412. /// <summary>
  1413. /// 加载流程(1.1)
  1414. /// </summary>
  1415. /// <param name="root">XML</param>
  1416. private void LoadFlow_1_1(XmlNode root)
  1417. {
  1418. //// 根节点
  1419. //XmlNode flowSetting = xmlDoc.SelectSingleNode("FlowSetting");
  1420. //// 版本
  1421. //string version = flowSetting.Attributes["Version"].Value;
  1422. // 编码
  1423. this._itemManager.ManagerCode = root.Attributes["ManagerCode"].Value;
  1424. // 新项目ID
  1425. //this._itemManager.SetNewItemID(Convert.ToInt32(root.SelectSingleNode("NewItemID").InnerText));
  1426. this._itemManager.SetNewItemID(Convert.ToInt32(root.Attributes["NewItemID"].Value));
  1427. // 流程名称
  1428. this._flowName = root.SelectSingleNode("FlowName").InnerText;
  1429. // 画布大小
  1430. this.canvasBox.SetCanvasSize(Converter.ToObject<Size>(root.SelectSingleNode("CanvasSize").InnerText));
  1431. //// 画布背景
  1432. //XmlNode backgroundImage = flowSetting.SelectSingleNode("BackgroundImage");
  1433. //this.canvasBox.CanvasControl.BackgroundImage = Converter.ToImage(backgroundImage.InnerText);
  1434. // FlowItems
  1435. XmlNode flowItems = root.SelectSingleNode("FlowItems");
  1436. foreach (XmlNode item in flowItems.ChildNodes)
  1437. {
  1438. int itemID = Convert.ToInt32(item.Attributes["ItemID"].Value);
  1439. string itemCode = item.Attributes["ItemCode"].Value;
  1440. string itemType = item.Attributes["ItemType"].Value;
  1441. object ts = null;
  1442. XmlNode tsXmlNode = item.SelectSingleNode("TS");
  1443. if (tsXmlNode != null && !string.IsNullOrWhiteSpace(tsXmlNode.InnerText))
  1444. {
  1445. byte[] bytes = Convert.FromBase64String(tsXmlNode.InnerText);
  1446. using (MemoryStream memoryStream = new MemoryStream(bytes))
  1447. {
  1448. BinaryFormatter formatter = new BinaryFormatter();
  1449. ts = formatter.Deserialize(memoryStream);
  1450. }
  1451. }
  1452. if (typeof(FlowNode).Name == itemType)
  1453. {
  1454. FlowNode node = new FlowNode(this._itemManager, itemID, itemCode);
  1455. node.ID = Convert.ToInt32(item.SelectSingleNode("ID").InnerText);
  1456. node.Code = item.SelectSingleNode("Code").InnerText;
  1457. node.Name = item.SelectSingleNode("Name").InnerText;
  1458. node.Remark = item.SelectSingleNode("Remark").InnerText;
  1459. node.ShowName = Converter.ToObject<bool>(item.SelectSingleNode("ShowName").InnerText);
  1460. node.Font = Converter.ToObject<Font>(item.SelectSingleNode("Font").InnerText);
  1461. node.FontColor = Converter.ToObject<Color>(item.SelectSingleNode("FontColor").InnerText);
  1462. node.TagSerializable = ts;
  1463. node.Bounds = Converter.ToObject<Rectangle>(item.SelectSingleNode("Bounds").InnerText);
  1464. node.BorderWidth = Convert.ToInt32(item.SelectSingleNode("BorderWidth").InnerText);
  1465. node.BorderColor = Converter.ToObject<Color>(item.SelectSingleNode("BorderColor").InnerText);
  1466. node.FillColor = Converter.ToObject<Color?>(item.SelectSingleNode("FillColor").InnerText);
  1467. node.CanSkip = Converter.ToObject<bool>(item.SelectSingleNode("CanSkip").InnerText);
  1468. node.NodeImageType = Converter.ToObject<int?>(item.SelectSingleNode("NodeImageType").InnerText);
  1469. if (!node.NodeImageType.HasValue)
  1470. {
  1471. node.NodeImage = Converter.ToImage(item.SelectSingleNode("NodeImage").InnerText);
  1472. }
  1473. FlowNodeLoadingEventHandler flowNodeLoading = this.GetFlowNodeLoadingEventHandler();
  1474. if (flowNodeLoading != null)
  1475. {
  1476. flowNodeLoading(this, new NodeEventArgs(node));
  1477. }
  1478. this._itemManager.Items.Add(node);
  1479. this._itemManager.AllNodes.Add(node);
  1480. continue;
  1481. }
  1482. if (typeof(FlowLine).Name == itemType)
  1483. {
  1484. FlowLine line = new FlowLine(this._itemManager, itemID, itemCode);
  1485. line.ID = Convert.ToInt32(item.SelectSingleNode("ID").InnerText);
  1486. line.Code = item.SelectSingleNode("Code").InnerText;
  1487. line.Name = item.SelectSingleNode("Name").InnerText;
  1488. line.Remark = item.SelectSingleNode("Remark").InnerText;
  1489. line.ShowName = Converter.ToObject<bool>(item.SelectSingleNode("ShowName").InnerText);
  1490. line.Font = Converter.ToObject<Font>(item.SelectSingleNode("Font").InnerText);
  1491. line.FontColor = Converter.ToObject<Color>(item.SelectSingleNode("FontColor").InnerText);
  1492. line.TagSerializable = ts;
  1493. line.PointBegin = Converter.ToObject<Point>(item.SelectSingleNode("PointBegin").InnerText);
  1494. line.PointEnd = Converter.ToObject<Point>(item.SelectSingleNode("PointEnd").InnerText);
  1495. line.LineColor = Converter.ToObject<Color>(item.SelectSingleNode("LineColor").InnerText);
  1496. line.LineWidth = Convert.ToInt32(item.SelectSingleNode("LineWidth").InnerText);
  1497. XmlNode anchorBegin = item.SelectSingleNode("AnchorBegin");
  1498. line.AnchorBegin = Converter.ToObject<AnchorKind>(anchorBegin.Attributes["Kind"].Value);
  1499. if (line.AnchorBegin != AnchorKind.None)
  1500. {
  1501. line.NodeCodeBegin = anchorBegin.InnerText;
  1502. }
  1503. XmlNode anchorEnd = item.SelectSingleNode("AnchorEnd");
  1504. line.AnchorEnd = Converter.ToObject<AnchorKind>(anchorEnd.Attributes["Kind"].Value);
  1505. if (line.AnchorEnd != AnchorKind.None)
  1506. {
  1507. line.NodeCodeEnd = anchorEnd.InnerText;
  1508. }
  1509. this._itemManager.Items.Add(line);
  1510. continue;
  1511. }
  1512. }
  1513. // 连接
  1514. foreach (FlowItem item in this._itemManager.Items)
  1515. {
  1516. this.SetLineToNodeFromXml(item as FlowLine);
  1517. }
  1518. }
  1519. /// <summary>
  1520. /// 从xml加载流程后,把line链接到node上。
  1521. /// </summary>
  1522. /// <param name="line">线段</param>
  1523. private void SetLineToNodeFromXml(FlowLine line)
  1524. {
  1525. if (line == null)
  1526. {
  1527. return;
  1528. }
  1529. bool hasC1 = false;
  1530. bool hasC2 = false;
  1531. if (line.AnchorBegin != AnchorKind.None || line.AnchorEnd != AnchorKind.None)
  1532. {
  1533. foreach (FlowItem item in this._itemManager.Items)
  1534. {
  1535. FlowNode node = item as FlowNode;
  1536. if (node == null)
  1537. {
  1538. continue;
  1539. }
  1540. if (line.AnchorBegin != AnchorKind.None && node.ItemCode == line.NodeCodeBegin)
  1541. {
  1542. hasC1 = true;
  1543. line.NodeBegin = node;
  1544. node.OutLines.Add(line);
  1545. switch (line.AnchorBegin)
  1546. {
  1547. case AnchorKind.Top:
  1548. node.TopLines.Add(line);
  1549. break;
  1550. case AnchorKind.Left:
  1551. node.LeftLines.Add(line);
  1552. break;
  1553. case AnchorKind.Right:
  1554. node.RightLines.Add(line);
  1555. break;
  1556. case AnchorKind.Bottom:
  1557. node.BottomLines.Add(line);
  1558. break;
  1559. default:
  1560. break;
  1561. }
  1562. }
  1563. if (line.AnchorEnd != AnchorKind.None && node.ItemCode == line.NodeCodeEnd)
  1564. {
  1565. hasC2 = true;
  1566. line.NodeEnd = node;
  1567. node.InLines.Add(line);
  1568. switch (line.AnchorEnd)
  1569. {
  1570. case AnchorKind.Top:
  1571. node.TopLines.Add(line);
  1572. break;
  1573. case AnchorKind.Left:
  1574. node.LeftLines.Add(line);
  1575. break;
  1576. case AnchorKind.Right:
  1577. node.RightLines.Add(line);
  1578. break;
  1579. case AnchorKind.Bottom:
  1580. node.BottomLines.Add(line);
  1581. break;
  1582. default:
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. if (!hasC1)
  1588. {
  1589. line.AnchorBegin = AnchorKind.None;
  1590. }
  1591. if (!hasC2)
  1592. {
  1593. line.AnchorEnd = AnchorKind.None;
  1594. }
  1595. }
  1596. }
  1597. #endregion 保存、读取
  1598. #endregion 私有方法
  1599. #region 事件声明
  1600. /// <summary>
  1601. /// 双击流程节点事件
  1602. /// </summary>
  1603. public event NodeDoubleClickEventHandler NodeDoubleClick
  1604. {
  1605. add
  1606. {
  1607. base.Events.AddHandler(FlowBox.EventNodeDoubleClick, value);
  1608. }
  1609. remove
  1610. {
  1611. base.Events.RemoveHandler(FlowBox.EventNodeDoubleClick, value);
  1612. }
  1613. }
  1614. /// <summary>
  1615. /// 流程节点加载时事件
  1616. /// </summary>
  1617. public event FlowNodeLoadingEventHandler FlowNodeLoading
  1618. {
  1619. add
  1620. {
  1621. base.Events.AddHandler(FlowBox.EventFlowNodeLoading, value);
  1622. }
  1623. remove
  1624. {
  1625. base.Events.RemoveHandler(FlowBox.EventFlowNodeLoading, value);
  1626. }
  1627. }
  1628. #endregion 事件声明
  1629. #region 键盘事件
  1630. /// <summary>
  1631. /// 键盘事件
  1632. /// </summary>
  1633. /// <param name="sender"></param>
  1634. /// <param name="e"></param>
  1635. private void keyControl_KeyDown(object sender, KeyEventArgs e)
  1636. {
  1637. if (this._flowBoxMode == FlowBoxMode.Display)
  1638. {
  1639. return;
  1640. }
  1641. switch(e.KeyCode)
  1642. {
  1643. case Keys.Escape:
  1644. //this._itemManager.StopDrawingItem();
  1645. break;
  1646. case Keys.Up:
  1647. this._itemManager.MoveSelectedItems(0, -1);
  1648. break;
  1649. case Keys.Left:
  1650. this._itemManager.MoveSelectedItems(-1, 0);
  1651. break;
  1652. case Keys.Right:
  1653. this._itemManager.MoveSelectedItems(1, 0);
  1654. break;
  1655. case Keys.Down:
  1656. this._itemManager.MoveSelectedItems(0, 1);
  1657. break;
  1658. default:
  1659. return;
  1660. }
  1661. this._itemManager.RefreshItems();
  1662. }
  1663. private void contextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
  1664. {
  1665. e.Cancel = true;
  1666. }
  1667. private void tsmiAllSelect_Click(object sender, EventArgs e)
  1668. {
  1669. this.SelectAllItem();
  1670. }
  1671. private void tsmiDelete_Click(object sender, EventArgs e)
  1672. {
  1673. this.DeleteSelectedItem();
  1674. }
  1675. #endregion 键盘事件
  1676. }
  1677. }