ItemManager.cs 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Windows.Forms;
  7. namespace Dongke.IBOSS.Basics.FlowSetting
  8. {
  9. /// <summary>
  10. /// 流程项目绘制管理器
  11. /// </summary>
  12. internal class ItemManager
  13. {
  14. #region 成员变量
  15. /// <summary>
  16. /// 流程项目自增ID
  17. /// </summary>
  18. private int _newItemID = 0;
  19. /// <summary>
  20. /// 管理器编码
  21. /// </summary>
  22. private string _managerCode = Guid.NewGuid().ToString().ToUpper();
  23. /// <summary>
  24. /// 画板
  25. /// </summary>
  26. private CanvasBox _canvasBox = null;
  27. /// <summary>
  28. /// 流程设计器
  29. /// </summary>
  30. private FlowBox _flowBox = null;
  31. /// <summary>
  32. /// 画布控件
  33. /// </summary>
  34. private Control _canvasControl = null;
  35. /// <summary>
  36. /// 流程编辑模式
  37. /// </summary>
  38. private FlowBoxMode _flowBoxMode = FlowBoxMode.Edit;
  39. #region 项目集合
  40. // todo 自定义 list 管理 外部不能add,remove只能get,可以通过,itemid,onlycode,itemindex(显示顺序)取得。
  41. /// <summary>
  42. /// 全部流程项目
  43. /// </summary>
  44. private List<FlowItem> _items = new List<FlowItem>();
  45. /// <summary>
  46. /// 选中的流程项目
  47. /// </summary>
  48. private List<FlowItem> _selectedItems = new List<FlowItem>();
  49. ///// <summary>
  50. ///// 删除的流程项目
  51. ///// </summary>
  52. //private List<FlowItem> _deletedItems = new List<FlowItem>();
  53. ///// <summary>
  54. ///// 选中的流程节点
  55. ///// </summary>
  56. //private List<FlowNode> _selectedNodes = new List<FlowNode>();
  57. /// <summary>
  58. /// 全部流程节点
  59. /// </summary>
  60. private List<FlowNode> _allNodes = new List<FlowNode>();
  61. /// <summary>
  62. /// 删除的流程项目
  63. /// </summary>
  64. private List<FlowNode> _deletedNodes = new List<FlowNode>();
  65. #endregion 项目集合
  66. #region 新建项目
  67. /// <summary>
  68. /// 鼠标操作,指定位置,新建Item
  69. /// </summary>
  70. private FlowItem _drawingItem = null;
  71. /// <summary>
  72. /// 鼠标操作,指定位置,新建节点
  73. /// </summary>
  74. private FlowNode _drawingNode = null;
  75. /// <summary>
  76. /// 鼠标操作,指定位置,新建线段
  77. /// </summary>
  78. private FlowLine _drawingLine = null;
  79. #endregion 新建项目
  80. #region 鼠标操作
  81. /// <summary>
  82. /// 鼠标动作
  83. /// </summary>
  84. private MouseOperatingType _mouseOperatingType = MouseOperatingType.None;
  85. /// <summary>
  86. /// 鼠标框选范围
  87. /// </summary>
  88. private Rectangle _mouseSelectRect = Rectangle.Empty;
  89. /// <summary>
  90. /// 鼠标按下的点
  91. /// </summary>
  92. private Point _mouseDownPoint = Point.Empty;
  93. /// <summary>
  94. /// 鼠标移动的点
  95. /// </summary>
  96. private Point _mouseMovePoint = Point.Empty;
  97. /// <summary>
  98. /// 鼠标抬起的点
  99. /// </summary>
  100. private Point _mouseUpPoint = Point.Empty;
  101. /// <summary>
  102. /// 鼠标是否按下
  103. /// </summary>
  104. private bool _isMouseDown = false;
  105. /// <summary>
  106. /// 鼠标是否移动
  107. /// </summary>
  108. private bool _isMouseMove = false;
  109. /// <summary>
  110. /// 鼠标是否抬起
  111. /// </summary>
  112. private bool _isMouseUp = false;
  113. /// <summary>
  114. /// 进行改变大小操作的节点
  115. /// </summary>
  116. private FlowNode _nodeSizeChanging = null;
  117. /// <summary>
  118. /// 进行改变大小操作的线段
  119. /// </summary>
  120. private FlowLine _linePointChanging = null;
  121. /// <summary>
  122. /// 正在显示锚点的节点
  123. /// </summary>
  124. private FlowNode _nodeAnchorShowing = null;
  125. #endregion 鼠标操作
  126. #region 绘制项目
  127. // TODO 使用后销毁??
  128. /// <summary>
  129. /// 画节点笔
  130. /// </summary>
  131. private Pen _penDrawNode = new Pen(Consts.NODE_BORDERCOLOR_DEFAULT);
  132. /// <summary>
  133. /// 画线段笔
  134. /// </summary>
  135. private Pen _penDrawLine = new Pen(Consts.LINE_COLOR_DEFAULT, Consts.LINE_WIDTH_DEFAULT);
  136. /// <summary>
  137. /// 写字笔
  138. /// </summary>
  139. private Pen _penDrawString = new Pen(Consts.FONT_COLOR_DEFAULT);
  140. #endregion 绘制项目
  141. #endregion 成员变量
  142. private StringFormat _stringFormat = new StringFormat();
  143. #region 构造函数
  144. /// <summary>
  145. /// 构造函数
  146. /// </summary>
  147. /// <param name="flowBox">流程设计器</param>
  148. /// <param name="canvasBox">画板</param>
  149. internal ItemManager(FlowBox flowBox, CanvasBox canvasBox)
  150. {
  151. _stringFormat.Alignment = StringAlignment.Center;
  152. _stringFormat.LineAlignment = StringAlignment.Near;
  153. _stringFormat.FormatFlags = StringFormatFlags.FitBlackBox;
  154. this._flowBox = flowBox;
  155. this._canvasBox = canvasBox;
  156. this._canvasControl = canvasBox.CanvasControl;
  157. this._flowBoxMode = flowBox.BoxMode;
  158. // 线段连接线 以圆点开始,箭头结束
  159. AdjustableArrowCap myArrow = new AdjustableArrowCap(4, 4);
  160. CustomLineCap customArrow = myArrow;
  161. this._penDrawLine.CustomEndCap = myArrow;
  162. //this._penDrawLine.StartCap = LineCap.RoundAnchor;
  163. this.SetDefaultCursor();
  164. }
  165. #endregion 构造函数
  166. #region 属性
  167. /// <summary>
  168. /// 获取全部流程项目
  169. /// </summary>
  170. public List<FlowItem> Items
  171. {
  172. get
  173. {
  174. return this._items;
  175. }
  176. }
  177. /// <summary>
  178. /// 获取选择的流程项目
  179. /// </summary>
  180. public List<FlowItem> SelectedItems
  181. {
  182. get
  183. {
  184. return this._selectedItems;
  185. }
  186. }
  187. /// <summary>
  188. /// 获取删除的节点
  189. /// </summary>
  190. public List<FlowNode> DeletedNodes
  191. {
  192. get
  193. {
  194. return this._deletedNodes;
  195. }
  196. }
  197. /// <summary>
  198. /// 获取全部的节点(包括删除的)
  199. /// </summary>
  200. public List<FlowNode> AllNodes
  201. {
  202. get
  203. {
  204. return this._allNodes;
  205. }
  206. }
  207. /// <summary>
  208. /// 获取或设置管理器编码
  209. /// </summary>
  210. internal string ManagerCode
  211. {
  212. get
  213. {
  214. return this._managerCode;
  215. }
  216. set
  217. {
  218. this._managerCode = value;
  219. }
  220. }
  221. /// <summary>
  222. /// 获取或设置流程编辑模式
  223. /// </summary>
  224. internal FlowBoxMode BoxMode
  225. {
  226. get
  227. {
  228. return this._flowBoxMode;
  229. }
  230. set
  231. {
  232. this._flowBoxMode = value;
  233. }
  234. }
  235. /// <summary>
  236. /// 获取或设置新的itemID
  237. /// </summary>
  238. internal int NewItemID
  239. {
  240. get
  241. {
  242. return ++this._newItemID;
  243. }
  244. }
  245. /// <summary>
  246. /// 获取或设置画板的鼠标样式
  247. /// </summary>
  248. private Cursor CanvasCursor
  249. {
  250. get
  251. {
  252. return this._canvasControl.Cursor;
  253. }
  254. set
  255. {
  256. this._canvasControl.Cursor = value;
  257. }
  258. }
  259. #endregion 属性
  260. #region 公有方法
  261. #region 新建项目
  262. /// <summary>
  263. /// 停止新建节点(鼠标操作)
  264. /// </summary>
  265. public void StopDrawingItem()
  266. {
  267. this._drawingItem = null;
  268. this._drawingNode = null;
  269. this._drawingLine = null;
  270. this.SetDefaultCursor();
  271. }
  272. /// <summary>
  273. /// 新建节点(鼠标操作,指定位置)
  274. /// </summary>
  275. public FlowNode DrawingNode()
  276. {
  277. this._drawingNode = new FlowNode(this);
  278. this._drawingLine = null;
  279. this._drawingItem = this._drawingNode;
  280. this.SetDrawingNodeCursor();
  281. return this._drawingNode;
  282. }
  283. /// <summary>
  284. /// 新建线段(鼠标操作,指定位置)
  285. /// </summary>
  286. public FlowLine DrawingLine()
  287. {
  288. this._drawingNode = null;
  289. this._drawingLine = new FlowLine(this);
  290. this._drawingItem = this._drawingLine;
  291. this.SetDrawingLineCursor();
  292. return this._drawingLine;
  293. }
  294. /// <summary>
  295. /// 新建节点(默认位置)
  296. /// </summary>
  297. /// <returns>节点</returns>
  298. public FlowNode NewNode()
  299. {
  300. this._drawingItem = null;
  301. this._drawingNode = null;
  302. this._drawingLine = null;
  303. return this.NewNodeInternal(null);
  304. }
  305. /// <summary>
  306. /// 新建节点(指定位置)
  307. /// </summary>
  308. /// <param name="location">节点位置</param>
  309. public FlowNode NewNode(Point location)
  310. {
  311. this._drawingItem = null;
  312. this._drawingNode = null;
  313. this._drawingLine = null;
  314. return this.NewNodeInternal(location);
  315. }
  316. /// <summary>
  317. /// 新建线段(默认位置)
  318. /// </summary>
  319. /// <returns>线段</returns>
  320. public FlowLine NewLine()
  321. {
  322. this._drawingItem = null;
  323. this._drawingNode = null;
  324. this._drawingLine = null;
  325. return this.NewLineInternal(null, null);
  326. }
  327. /// <summary>
  328. /// 新建线段(指定位置)
  329. /// </summary>
  330. /// <param name="pointBegin">起始点</param>
  331. /// <param name="pointEnd">结束点</param>
  332. /// <returns>线段</returns>
  333. public FlowLine NewLine(Point pointBegin, Point pointEnd)
  334. {
  335. this._drawingItem = null;
  336. this._drawingNode = null;
  337. this._drawingLine = null;
  338. return this.NewLineInternal(pointBegin, pointEnd);
  339. }
  340. #endregion 新建项目
  341. #region 刷项目
  342. ///// <summary>
  343. ///// 刷新未激活Item的图片
  344. ///// </summary>
  345. //public void RefreshStaticItems()
  346. //{
  347. // // 清除画布
  348. // Graphics graphics = this._canvasBox.Canvas;
  349. // graphics.Clear(Color.Transparent);
  350. // // 绘制未激活的Item
  351. // this.DrawStaticItems(graphics);
  352. // // 设定未激活的Item为图片
  353. // this._canvasBox.SetItemsImage(this._canvasBox.CanvasImage);
  354. //}
  355. ///// <summary>
  356. ///// 刷新激活Item的图片
  357. ///// </summary>
  358. //public void RefreshActiveItems()
  359. //{
  360. // // 清除画布
  361. // Graphics graphics = this._canvasBox.Canvas;
  362. // graphics.Clear(Color.Transparent);
  363. // // 绘制激活的Item
  364. // this.DrawActiveItems(graphics);
  365. // // 设定未激活的Item为图片
  366. // this._canvasBox.SetItemsImage(this._canvasBox.CanvasImage);
  367. //}
  368. ///// <summary>
  369. ///// 刷新激活Item
  370. ///// </summary>
  371. ///// <param name="graphics"></param>
  372. //public void RefreshActiveItems(Graphics graphics)
  373. //{
  374. // // todo 在paint绘制事件中绘制激活的item,未激活item作为背景
  375. // // 绘制激活的Item
  376. // this.DrawActiveItems(graphics);
  377. //}
  378. /// <summary>
  379. /// 刷新Item的图片
  380. /// </summary>
  381. public void RefreshItems()
  382. {
  383. // 清除画布
  384. Graphics graphics = this._canvasBox.Canvas;
  385. graphics.Clear(Color.Transparent);
  386. // 绘制激活的Item
  387. this.DrawItems(graphics);
  388. // 设定未激活的Item为图片
  389. this._canvasBox.SetItemsImage(this._canvasBox.CanvasImage);
  390. GC.Collect();
  391. }
  392. #endregion 刷新项目
  393. #region 操作项目
  394. /// <summary>
  395. /// 全部不选择
  396. /// </summary>
  397. public void ClearSelection()
  398. {
  399. foreach (FlowItem item in this._selectedItems)
  400. {
  401. item.MouseOperating = false;
  402. item.Selected = false;
  403. }
  404. this._selectedItems.Clear();
  405. }
  406. /// <summary>
  407. /// 全部选择
  408. /// </summary>
  409. public void SelectAllItems()
  410. {
  411. foreach (FlowItem item in this._items)
  412. {
  413. if (!item.Selected)
  414. {
  415. this._selectedItems.Add(item);
  416. item.Selected = true;
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// 删除全部项目
  422. /// </summary>
  423. public void DeleteAllItems()
  424. {
  425. foreach (FlowItem item in this._items)
  426. {
  427. item.MouseOperating = false;
  428. item.Selected = false;
  429. if (item is FlowNode)
  430. {
  431. FlowNode node = item as FlowNode;
  432. node.ShowAnchor = false;
  433. node.InLines.Clear();
  434. node.OutLines.Clear();
  435. node.TopLines.Clear();
  436. node.LeftLines.Clear();
  437. node.RightLines.Clear();
  438. node.BottomLines.Clear();
  439. node.PreNodes.Clear();
  440. node.NextNodes.Clear();
  441. if (node.NodeState == FlowNodeState.Added ||
  442. node.NodeState == FlowNodeState.Detached)
  443. {
  444. node.NodeState = FlowNodeState.Detached;
  445. }
  446. else
  447. {
  448. node.NodeState = FlowNodeState.Deleted;
  449. }
  450. this._deletedNodes.Add(item as FlowNode);
  451. continue;
  452. }
  453. if (item is FlowLine)
  454. {
  455. FlowLine line = item as FlowLine;
  456. line.AnchorBegin = AnchorKind.None;
  457. line.AnchorEnd = AnchorKind.None;
  458. line.NodeBegin = null;
  459. line.NodeEnd = null;
  460. line.NodeCodeBegin = null;
  461. line.NodeCodeEnd = null;
  462. continue;
  463. }
  464. }
  465. this._selectedItems.Clear();
  466. this._items.Clear();
  467. }
  468. /// <summary>
  469. /// 删除选择项目
  470. /// </summary>
  471. public void DeleteSelectedItems()
  472. {
  473. foreach (FlowItem item in this._selectedItems)
  474. {
  475. item.MouseOperating = false;
  476. item.Selected = false;
  477. if (item is FlowNode)
  478. {
  479. FlowNode node = item as FlowNode;
  480. node.ShowAnchor = false;
  481. foreach (FlowLine line in node.InLines)
  482. {
  483. //if (!line.Selected)
  484. {
  485. line.NodeEnd = null;
  486. line.AnchorEnd = AnchorKind.None;
  487. }
  488. }
  489. foreach (FlowLine line in node.OutLines)
  490. {
  491. //if (!line.Selected)
  492. {
  493. line.NodeBegin = null;
  494. line.AnchorBegin = AnchorKind.None;
  495. }
  496. }
  497. node.InLines.Clear();
  498. node.OutLines.Clear();
  499. node.TopLines.Clear();
  500. node.LeftLines.Clear();
  501. node.RightLines.Clear();
  502. node.BottomLines.Clear();
  503. node.PreNodes.Clear();
  504. node.NextNodes.Clear();
  505. if (node.NodeState == FlowNodeState.Added ||
  506. node.NodeState == FlowNodeState.Detached)
  507. {
  508. node.NodeState = FlowNodeState.Detached;
  509. }
  510. else
  511. {
  512. node.NodeState = FlowNodeState.Deleted;
  513. }
  514. this._items.Remove(node);
  515. this._deletedNodes.Add(item as FlowNode);
  516. continue;
  517. }
  518. if (item is FlowLine)
  519. {
  520. FlowLine line = item as FlowLine;
  521. if (line.NodeBegin != null && !line.NodeBegin.Selected)
  522. {
  523. line.NodeBegin.OutLines.Remove(line);
  524. switch (line.AnchorBegin)
  525. {
  526. case AnchorKind.Top:
  527. line.NodeBegin.TopLines.Remove(line);
  528. break;
  529. case AnchorKind.Left:
  530. line.NodeBegin.LeftLines.Remove(line);
  531. break;
  532. case AnchorKind.Right:
  533. line.NodeBegin.RightLines.Remove(line);
  534. break;
  535. case AnchorKind.Bottom:
  536. line.NodeBegin.BottomLines.Remove(line);
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. if (line.NodeEnd != null && !line.NodeEnd.Selected)
  543. {
  544. line.NodeEnd.InLines.Remove(line);
  545. switch (line.AnchorEnd)
  546. {
  547. case AnchorKind.Top:
  548. line.NodeEnd.TopLines.Remove(line);
  549. break;
  550. case AnchorKind.Left:
  551. line.NodeEnd.LeftLines.Remove(line);
  552. break;
  553. case AnchorKind.Right:
  554. line.NodeEnd.RightLines.Remove(line);
  555. break;
  556. case AnchorKind.Bottom:
  557. line.NodeEnd.BottomLines.Remove(line);
  558. break;
  559. default:
  560. break;
  561. }
  562. }
  563. line.AnchorBegin = AnchorKind.None;
  564. line.AnchorEnd = AnchorKind.None;
  565. line.NodeBegin = null;
  566. line.NodeEnd = null;
  567. this._items.Remove(line);
  568. continue;
  569. }
  570. }
  571. this._selectedItems.Clear();
  572. }
  573. /// <summary>
  574. /// 对齐选定的节点
  575. /// </summary>
  576. /// <param name="arrangeType">对齐方式</param>
  577. public void ArrangeNodes(ArrangeType arrangeType)
  578. {
  579. switch (arrangeType)
  580. {
  581. case ArrangeType.Left:
  582. ArrangeNodesLeft();
  583. break;
  584. case ArrangeType.Right:
  585. ArrangeNodesRight();
  586. break;
  587. case ArrangeType.HorizontalCenter:
  588. ArrangeNodesHorizontalCenter();
  589. break;
  590. case ArrangeType.Top:
  591. ArrangeNodesTop();
  592. break;
  593. case ArrangeType.Bottom:
  594. ArrangeNodesBottom();
  595. break;
  596. case ArrangeType.VerticalCenter:
  597. ArrangeNodesVerticalCenter();
  598. break;
  599. default:
  600. break;
  601. }
  602. }
  603. /// <summary>
  604. /// 顶部对齐所有选中的节点
  605. /// </summary>
  606. public void ArrangeNodesTop()
  607. {
  608. int minTop = this._canvasControl.Height;
  609. foreach (FlowItem item in this._selectedItems)
  610. {
  611. FlowNode node = item as FlowNode;
  612. if (node != null)
  613. {
  614. minTop = Math.Min(node.Top, minTop);
  615. }
  616. }
  617. foreach (FlowItem item in this._selectedItems)
  618. {
  619. FlowNode node = item as FlowNode;
  620. if (node != null)
  621. {
  622. node.Top = minTop;
  623. }
  624. }
  625. }
  626. /// <summary>
  627. /// 底部对齐所有选中的节点
  628. /// </summary>
  629. public void ArrangeNodesBottom()
  630. {
  631. }
  632. /// <summary>
  633. /// 左侧对齐所有选中的节点
  634. /// </summary>
  635. public void ArrangeNodesLeft()
  636. {
  637. }
  638. /// <summary>
  639. /// 右侧对齐所有选中的节点
  640. /// </summary>
  641. public void ArrangeNodesRight()
  642. {
  643. }
  644. /// <summary>
  645. /// 垂直方向中心对齐所有选中的节点
  646. /// </summary>
  647. public void ArrangeNodesVerticalCenter()
  648. {
  649. }
  650. /// <summary>
  651. /// 水平方向中心对齐所有选中的节点
  652. /// </summary>
  653. public void ArrangeNodesHorizontalCenter()
  654. {
  655. }
  656. #endregion 操作项目
  657. #endregion 公有方法
  658. #region 内部方法
  659. /// <summary>
  660. /// 获取新ItemID(用于流程保存)
  661. /// </summary>
  662. /// <returns>ItemID</returns>
  663. internal int GetNewItemID()
  664. {
  665. return this._newItemID;
  666. }
  667. /// <summary>
  668. /// 设置新ItemID(用于流程读取)
  669. /// </summary>
  670. /// <param name="itemID">ItemID</param>
  671. internal void SetNewItemID(int itemID)
  672. {
  673. this._newItemID = itemID;
  674. }
  675. #region 刷新项目
  676. /// <summary>
  677. /// 刷新鼠标框选范围
  678. /// </summary>
  679. internal void RefreshMouseSelectRect(Graphics graphics)
  680. {
  681. if (this._mouseOperatingType == MouseOperatingType.RectSelect)
  682. {
  683. ControlPaint.DrawFocusRectangle(graphics, this._mouseSelectRect,
  684. this._canvasControl.BackColor, Color.Transparent);
  685. }
  686. }
  687. #endregion 刷新项目
  688. #region 鼠标操作
  689. /// <summary>
  690. /// 处理鼠标按下事件
  691. /// </summary>
  692. /// <param name="e"></param>
  693. internal void MouseDown(MouseEventArgs e)
  694. {
  695. if (e.Button != MouseButtons.Left)
  696. {
  697. this._drawingItem = null;
  698. this._drawingNode = null;
  699. this._drawingLine = null;
  700. this.SetDefaultCursor();
  701. return;
  702. }
  703. this._mouseDownPoint = e.Location;
  704. this._mouseMovePoint = e.Location;
  705. this._isMouseDown = true;
  706. this._isMouseMove = false;
  707. this._isMouseUp = false;
  708. if (this._mouseOperatingType != MouseOperatingType.Move)
  709. {
  710. this.CanvasCursor = Cursors.Cross;
  711. }
  712. // 鼠标绘制item
  713. if (this._drawingItem != null)
  714. {
  715. //// 节点
  716. //if (this._drawingItem is FlowNode)
  717. //{
  718. // FlowNode node = this._drawingItem as FlowNode;
  719. // node.Location = e.Location;
  720. // node.Size = Size.Empty;
  721. // return;
  722. //}
  723. //// 线段
  724. //if (this._drawingItem is FlowLine)
  725. //{
  726. // FlowLine line = this._drawingItem as FlowLine;
  727. // line.PointBegin = e.Location;
  728. // line.PointEnd = e.Location;
  729. // return;
  730. //}
  731. return;
  732. }
  733. // 改变节点大小
  734. if (this._mouseOperatingType == MouseOperatingType.NodeTopAndLeft ||
  735. this._mouseOperatingType == MouseOperatingType.NodeTopAndRight ||
  736. this._mouseOperatingType == MouseOperatingType.NodeBottomAndLeft ||
  737. this._mouseOperatingType == MouseOperatingType.NodeBottomAndRight ||
  738. this._mouseOperatingType == MouseOperatingType.NodeTop ||
  739. this._mouseOperatingType == MouseOperatingType.NodeBottom ||
  740. this._mouseOperatingType == MouseOperatingType.NodeLeft ||
  741. this._mouseOperatingType == MouseOperatingType.NodeRight)
  742. {
  743. // 选择的节点随着改变 begin
  744. //foreach (FlowItem selectedItem in this._selectedItems)
  745. //{
  746. // if (selectedItem is FlowNode)
  747. // {
  748. // FlowNode node = selectedItem as FlowNode;
  749. // node.MouseDownRect = node.Bounds;
  750. // }
  751. //}
  752. // 选择的节点随着改变 end
  753. // 只改变当前节点 begin
  754. this.ClearSelection();
  755. if (this._nodeSizeChanging != null)
  756. {
  757. this._nodeSizeChanging.Selected = true;
  758. this._selectedItems.Add(this._nodeSizeChanging);
  759. this._nodeSizeChanging.MouseDownRect = this._nodeSizeChanging.Bounds;
  760. }
  761. this.RefreshItems();
  762. // 只改变当前节点 end
  763. return;
  764. }
  765. // 改变线段端点位置
  766. if (this._mouseOperatingType == MouseOperatingType.LineBegin ||
  767. this._mouseOperatingType == MouseOperatingType.LineEnd)
  768. {
  769. if (this._flowBoxMode != FlowBoxMode.Edit)
  770. {
  771. return;
  772. }
  773. // 取消其他item选择状态 begin
  774. this.ClearSelection();
  775. if (this._linePointChanging != null)
  776. {
  777. this._linePointChanging.Selected = true;
  778. this._selectedItems.Add(this._linePointChanging);
  779. }
  780. this.RefreshItems();
  781. // 取消其他item选择状态 end
  782. return;
  783. }
  784. // 正在进行其他操作
  785. if (this._mouseOperatingType != MouseOperatingType.None)
  786. {
  787. return;
  788. }
  789. // 选择item
  790. FlowItem item = this.SelectItemByPoint(e.Location);
  791. if (item != null)
  792. {
  793. // 点选
  794. if (!item.Selected)
  795. {
  796. this.ClearSelection();
  797. item.Selected = true;
  798. this._selectedItems.Add(item);
  799. }
  800. this.SetMoveCursor();
  801. this.RefreshItems();
  802. return;
  803. }
  804. else
  805. {
  806. // 框选
  807. this._mouseSelectRect = new Rectangle(e.Location, Size.Empty);
  808. this.SetRectSelectCursor();
  809. // 框选开始就清空选择 begin
  810. //this.ClearSelectItem();
  811. //this.RefreshItems();
  812. // 框选开始就清空选择 end
  813. return;
  814. }
  815. }
  816. /// <summary>
  817. /// 处理鼠标移动事件
  818. /// </summary>
  819. /// <param name="e"></param>
  820. internal void MouseMove(MouseEventArgs e)
  821. {
  822. if (e.Button == MouseButtons.None && this._drawingItem == null)
  823. {
  824. this.SetMouseCursorByMove(e.Location);
  825. return;
  826. }
  827. if (e.Button != MouseButtons.Left)
  828. {
  829. return;
  830. }
  831. Point mouseBeginPoint = this._mouseMovePoint;
  832. this._mouseMovePoint = e.Location;
  833. this._isMouseMove = true;
  834. bool hasRefreshItems = false;
  835. #region 鼠标绘制item
  836. if (this._drawingItem != null)
  837. {
  838. // 节点
  839. if (this._mouseOperatingType == MouseOperatingType.DrawingNode)
  840. {
  841. //FlowNode node = this._drawingItem as FlowNode;
  842. this._drawingNode.Location = mouseBeginPoint;
  843. this._drawingNode.Size = Size.Empty;
  844. this._drawingNode.Selected = true;
  845. this._drawingNode.NodeState = FlowNodeState.Added;
  846. this.ClearSelection();
  847. this._items.Add(this._drawingNode);
  848. this._allNodes.Add(this._drawingNode);
  849. this._selectedItems.Add(this._drawingNode);
  850. this._drawingNode.MouseDownRect = this._drawingNode.Bounds;
  851. this._nodeSizeChanging = this._drawingNode;
  852. this._drawingItem = null;
  853. }
  854. // 线段
  855. if (this._mouseOperatingType == MouseOperatingType.DrawingLine)
  856. {
  857. //FlowLine line = this._drawingItem as FlowLine;
  858. this._drawingLine.PointBegin = mouseBeginPoint;
  859. this._drawingLine.PointEnd = e.Location;
  860. this._drawingLine.Selected = true;
  861. this.ClearSelection();
  862. this._items.Add(this._drawingLine);
  863. this._selectedItems.Add(this._drawingLine);
  864. this._linePointChanging = this._drawingLine;
  865. this._drawingItem = null;
  866. }
  867. }
  868. #endregion 鼠标绘制item
  869. #region 激活item
  870. if (this._isMouseDown)
  871. {
  872. hasRefreshItems = true;
  873. foreach (FlowItem item in this._selectedItems)
  874. {
  875. item.MouseOperating = true;
  876. }
  877. this._isMouseDown = false;
  878. // todo 在paint绘制事件中绘制激活的item,未激活item作为背景
  879. //this.RefreshStaticItems();
  880. }
  881. //// 取消激活item
  882. //if (this._isMouseUp)
  883. //{
  884. // hasRefreshItems = true;
  885. // foreach (FlowItem item in this._selectedItems)
  886. // {
  887. // item.MouseOperating = false;
  888. // }
  889. // this._isMouseUp = false;
  890. // // todo 在paint绘制事件中绘制激活的item,未激活item作为背景
  891. // //this.RefreshStaticItems();
  892. //}
  893. #endregion 激活item
  894. #region 框选
  895. if (this._mouseOperatingType == MouseOperatingType.RectSelect)
  896. {
  897. this._mouseSelectRect = this.GetNewRect(this._mouseSelectRect, this._mouseDownPoint, e.Location);
  898. this._canvasControl.Invalidate();
  899. return;
  900. }
  901. #endregion 框选
  902. #region 移动
  903. if (this._mouseOperatingType == MouseOperatingType.Move)
  904. {
  905. int xMove = e.X - mouseBeginPoint.X;
  906. int yMove = e.Y - mouseBeginPoint.Y;
  907. this.MoveSelectedItems(xMove, yMove);
  908. this.RefreshItems();
  909. return;
  910. }
  911. #endregion 移动
  912. #region 左上角
  913. if (this._mouseOperatingType == MouseOperatingType.NodeTopAndLeft)
  914. {
  915. if (this._nodeSizeChanging == null)
  916. {
  917. return;
  918. }
  919. Point point = this.GetRectTopAndLeft(this._nodeSizeChanging.MouseDownRect);
  920. int xRect = e.X - point.X;
  921. int yRect = e.Y - point.Y;
  922. foreach (FlowItem item in this._selectedItems)
  923. {
  924. if (item is FlowNode)
  925. {
  926. FlowNode node = item as FlowNode;
  927. Point fixedPoint = this.GetRectBottomAndRight(node.MouseDownRect);
  928. node.Bounds = new Rectangle(fixedPoint, new Size(xRect - node.MouseDownRect.Width, yRect - node.MouseDownRect.Height));
  929. // 移动连接线
  930. foreach (FlowLine line in node.InLines)
  931. {
  932. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  933. }
  934. // 移动连接线
  935. foreach (FlowLine line in node.OutLines)
  936. {
  937. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  938. }
  939. }
  940. }
  941. this.RefreshItems();
  942. return;
  943. }
  944. #endregion 左上角
  945. #region 右上角
  946. if (this._mouseOperatingType == MouseOperatingType.NodeTopAndRight)
  947. {
  948. if (this._nodeSizeChanging == null)
  949. {
  950. return;
  951. }
  952. Point point = this.GetRectTopAndRight(this._nodeSizeChanging.MouseDownRect);
  953. int xRect = e.X - point.X;
  954. int yRect = e.Y - point.Y;
  955. foreach (FlowItem item in this._selectedItems)
  956. {
  957. if (item is FlowNode)
  958. {
  959. FlowNode node = item as FlowNode;
  960. Point fixedPoint = this.GetRectBottomAndLeft(node.MouseDownRect);
  961. node.Bounds = new Rectangle(fixedPoint, new Size(xRect + node.MouseDownRect.Width, yRect - node.MouseDownRect.Height));
  962. // 移动连接线
  963. foreach (FlowLine line in node.InLines)
  964. {
  965. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  966. }
  967. // 移动连接线
  968. foreach (FlowLine line in node.OutLines)
  969. {
  970. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  971. }
  972. }
  973. }
  974. this.RefreshItems();
  975. return;
  976. }
  977. #endregion 右上角
  978. #region 左下角
  979. if (this._mouseOperatingType == MouseOperatingType.NodeBottomAndLeft)
  980. {
  981. if (this._nodeSizeChanging == null)
  982. {
  983. return;
  984. }
  985. Point point = this.GetRectBottomAndLeft(this._nodeSizeChanging.MouseDownRect);
  986. int xRect = e.X - point.X;
  987. int yRect = e.Y - point.Y;
  988. foreach (FlowItem item in this._selectedItems)
  989. {
  990. if (item is FlowNode)
  991. {
  992. FlowNode node = item as FlowNode;
  993. Point fixedPoint = this.GetRectTopAndRight(node.MouseDownRect);
  994. node.Bounds = new Rectangle(fixedPoint, new Size(xRect - node.MouseDownRect.Width, yRect + node.MouseDownRect.Height));
  995. // 移动连接线
  996. foreach (FlowLine line in node.InLines)
  997. {
  998. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  999. }
  1000. // 移动连接线
  1001. foreach (FlowLine line in node.OutLines)
  1002. {
  1003. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1004. }
  1005. }
  1006. }
  1007. this.RefreshItems();
  1008. return;
  1009. }
  1010. #endregion 左下角
  1011. #region 右下角
  1012. if (this._mouseOperatingType == MouseOperatingType.NodeBottomAndRight ||
  1013. this._mouseOperatingType == MouseOperatingType.DrawingNode)
  1014. {
  1015. if (this._nodeSizeChanging == null)
  1016. {
  1017. return;
  1018. }
  1019. Point point = this.GetRectBottomAndRight(this._nodeSizeChanging.MouseDownRect);
  1020. int xRect = e.X - point.X;
  1021. int yRect = e.Y - point.Y;
  1022. foreach (FlowItem item in this._selectedItems)
  1023. {
  1024. if (item is FlowNode)
  1025. {
  1026. FlowNode node = item as FlowNode;
  1027. Point fixedPoint = this.GetRectTopAndLeft(node.MouseDownRect);
  1028. node.Bounds = new Rectangle(fixedPoint, new Size(xRect + node.MouseDownRect.Width, yRect + node.MouseDownRect.Height));
  1029. // 移动连接线
  1030. foreach (FlowLine line in node.InLines)
  1031. {
  1032. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  1033. }
  1034. // 移动连接线
  1035. foreach (FlowLine line in node.OutLines)
  1036. {
  1037. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1038. }
  1039. }
  1040. }
  1041. this.RefreshItems();
  1042. return;
  1043. }
  1044. #endregion 右下角
  1045. #region 上中
  1046. if (this._mouseOperatingType == MouseOperatingType.NodeTop)
  1047. {
  1048. if (this._nodeSizeChanging == null)
  1049. {
  1050. return;
  1051. }
  1052. Point point = this.GetRectTop(this._nodeSizeChanging.MouseDownRect);
  1053. int xRect = 0;
  1054. int yRect = e.Y - point.Y;
  1055. foreach (FlowItem item in this._selectedItems)
  1056. {
  1057. if (item is FlowNode)
  1058. {
  1059. FlowNode node = item as FlowNode;
  1060. Point fixedPoint = this.GetRectBottomAndLeft(node.MouseDownRect);
  1061. node.Bounds = new Rectangle(fixedPoint, new Size(xRect + node.MouseDownRect.Width, yRect - node.MouseDownRect.Height));
  1062. // 移动连接线
  1063. foreach (FlowLine line in node.InLines)
  1064. {
  1065. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  1066. }
  1067. // 移动连接线
  1068. foreach (FlowLine line in node.OutLines)
  1069. {
  1070. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1071. }
  1072. }
  1073. }
  1074. this.RefreshItems();
  1075. return;
  1076. }
  1077. #endregion 上中
  1078. #region 下中
  1079. if (this._mouseOperatingType == MouseOperatingType.NodeBottom)
  1080. {
  1081. if (this._nodeSizeChanging == null)
  1082. {
  1083. return;
  1084. }
  1085. Point point = this.GetRectBottom(this._nodeSizeChanging.MouseDownRect);
  1086. int xRect = 0;
  1087. int yRect = e.Y - point.Y;
  1088. foreach (FlowItem item in this._selectedItems)
  1089. {
  1090. if (item is FlowNode)
  1091. {
  1092. FlowNode node = item as FlowNode;
  1093. Point fixedPoint = this.GetRectTopAndLeft(node.MouseDownRect);
  1094. node.Bounds = new Rectangle(fixedPoint, new Size(xRect + node.MouseDownRect.Width, yRect + node.MouseDownRect.Height));
  1095. // 移动连接线
  1096. foreach (FlowLine line in node.InLines)
  1097. {
  1098. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  1099. }
  1100. // 移动连接线
  1101. foreach (FlowLine line in node.OutLines)
  1102. {
  1103. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1104. }
  1105. }
  1106. }
  1107. this.RefreshItems();
  1108. return;
  1109. }
  1110. #endregion 下中
  1111. #region 左中
  1112. if (this._mouseOperatingType == MouseOperatingType.NodeLeft)
  1113. {
  1114. if (this._nodeSizeChanging == null)
  1115. {
  1116. return;
  1117. }
  1118. Point point = this.GetRectLeft(this._nodeSizeChanging.MouseDownRect);
  1119. int xRect = e.X - point.X;
  1120. int yRect = 0;
  1121. foreach (FlowItem item in this._selectedItems)
  1122. {
  1123. if (item is FlowNode)
  1124. {
  1125. FlowNode node = item as FlowNode;
  1126. Point fixedPoint = this.GetRectTopAndRight(node.MouseDownRect);
  1127. node.Bounds = new Rectangle(fixedPoint, new Size(xRect - node.MouseDownRect.Width, yRect + node.MouseDownRect.Height));
  1128. // 移动连接线
  1129. foreach (FlowLine line in node.InLines)
  1130. {
  1131. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  1132. }
  1133. // 移动连接线
  1134. foreach (FlowLine line in node.OutLines)
  1135. {
  1136. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1137. }
  1138. }
  1139. }
  1140. this.RefreshItems();
  1141. return;
  1142. }
  1143. #endregion 左中
  1144. #region 右中
  1145. if (this._mouseOperatingType == MouseOperatingType.NodeRight)
  1146. {
  1147. if (this._nodeSizeChanging == null)
  1148. {
  1149. return;
  1150. }
  1151. Point point = this.GetRectRight(this._nodeSizeChanging.MouseDownRect);
  1152. int xRect = e.X - point.X;
  1153. int yRect = 0;
  1154. foreach (FlowItem item in this._selectedItems)
  1155. {
  1156. if (item is FlowNode)
  1157. {
  1158. FlowNode node = item as FlowNode;
  1159. Point fixedPoint = this.GetRectTopAndLeft(node.MouseDownRect);
  1160. node.Bounds = new Rectangle(fixedPoint, new Size(xRect + node.MouseDownRect.Width, yRect + node.MouseDownRect.Height));
  1161. // 移动连接线
  1162. foreach (FlowLine line in node.InLines)
  1163. {
  1164. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  1165. }
  1166. // 移动连接线
  1167. foreach (FlowLine line in node.OutLines)
  1168. {
  1169. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1170. }
  1171. }
  1172. }
  1173. this.RefreshItems();
  1174. return;
  1175. }
  1176. #endregion 右中
  1177. #region 线段开始点
  1178. if (this._mouseOperatingType == MouseOperatingType.LineBegin)
  1179. {
  1180. if (this._linePointChanging == null)
  1181. {
  1182. return;
  1183. }
  1184. FlowLine line = this._linePointChanging;
  1185. line.PointBegin = e.Location;
  1186. // 断开连接
  1187. if (line.NodeBegin != null)
  1188. {
  1189. this.DisconnectL2NPointBegin(line);
  1190. }
  1191. // 查询可连接的锚点
  1192. FlowNode rectNode = this.GetL2N(e.Location);
  1193. this._nodeAnchorShowing = rectNode;
  1194. // 连接
  1195. if (rectNode != null)
  1196. {
  1197. rectNode.ShowAnchor = true;
  1198. Rectangle[] rects = rectNode.GetAnchorRects();
  1199. for (int i = 0; i < rects.Length; i++)
  1200. {
  1201. Rectangle rect = rects[i];
  1202. rect.Inflate(Consts.L2N_PRECISION_EDGE, Consts.L2N_PRECISION_EDGE);
  1203. if (rect.Contains(e.Location))
  1204. {
  1205. AnchorKind anchorKind = (AnchorKind)i;
  1206. this.ConnectL2NPointBegin(line, rectNode, anchorKind);
  1207. }
  1208. }
  1209. }
  1210. this.RefreshItems();
  1211. return;
  1212. }
  1213. #endregion 线段开始点
  1214. #region 线段结束点
  1215. if (this._mouseOperatingType == MouseOperatingType.LineEnd ||
  1216. this._mouseOperatingType == MouseOperatingType.DrawingLine)
  1217. {
  1218. if (this._linePointChanging == null)
  1219. {
  1220. return;
  1221. }
  1222. FlowLine line = this._linePointChanging;
  1223. line.PointEnd = e.Location;
  1224. // 断开连接
  1225. if (line.NodeEnd != null)
  1226. {
  1227. this.DisconnectL2NPointEnd(line);
  1228. }
  1229. // 查询可连接的锚点
  1230. FlowNode rectNode = this.GetL2N(e.Location);
  1231. this._nodeAnchorShowing = rectNode;
  1232. // 连接
  1233. if (rectNode != null)
  1234. {
  1235. rectNode.ShowAnchor = true;
  1236. Rectangle[] rects = rectNode.GetAnchorRects();
  1237. for (int i = 0; i < rects.Length; i++)
  1238. {
  1239. Rectangle rect = rects[i];
  1240. rect.Inflate(Consts.L2N_PRECISION_EDGE, Consts.L2N_PRECISION_EDGE);
  1241. if (rect.Contains(e.Location))
  1242. {
  1243. AnchorKind anchorKind = (AnchorKind)i;
  1244. this.ConnectL2NPointEnd(line, rectNode, anchorKind);
  1245. }
  1246. }
  1247. }
  1248. this.RefreshItems();
  1249. return;
  1250. }
  1251. #endregion 线段结束点
  1252. // todo 在paint绘制事件中绘制激活的item,未激活item作为背景
  1253. //this._canvasControl.Invalidate();
  1254. if (hasRefreshItems)
  1255. {
  1256. this.RefreshItems();
  1257. }
  1258. }
  1259. /// <summary>
  1260. /// 处理鼠标抬起事件
  1261. /// </summary>
  1262. /// <param name="e"></param>
  1263. internal void MouseUp(MouseEventArgs e)
  1264. {
  1265. if (e.Button != MouseButtons.Left)
  1266. {
  1267. this._drawingItem = null;
  1268. this._drawingNode = null;
  1269. this._drawingLine = null;
  1270. return;
  1271. }
  1272. this._mouseUpPoint = e.Location;
  1273. this._isMouseDown = false;
  1274. this._isMouseUp = true;
  1275. bool hasRefreshItems = false;
  1276. #region 取消激活item
  1277. if (this._isMouseMove)
  1278. {
  1279. hasRefreshItems = true;
  1280. foreach (FlowItem item in this._selectedItems)
  1281. {
  1282. item.MouseOperating = false;
  1283. }
  1284. this._isMouseMove = false;
  1285. if (this._nodeAnchorShowing != null)
  1286. {
  1287. this._nodeAnchorShowing.ShowAnchor = false;
  1288. this._nodeAnchorShowing = null;
  1289. }
  1290. this._nodeSizeChanging = null;
  1291. this._linePointChanging = null;
  1292. // todo 在paint绘制事件中绘制激活的item,未激活item作为背景
  1293. //this.RefreshStaticItems();
  1294. }
  1295. #endregion 取消激活item
  1296. #region 鼠标绘制item
  1297. if (this._drawingItem != null)
  1298. {
  1299. if (this._mouseOperatingType == MouseOperatingType.DrawingNode)
  1300. {
  1301. this.NewNodeInternal(e.Location);
  1302. }
  1303. if (this._mouseOperatingType == MouseOperatingType.DrawingLine)
  1304. {
  1305. Point pBegin = e.Location;
  1306. Point pEnd = pBegin;
  1307. pEnd.Offset(Consts.LINE_POINT_END_DEFAULT);
  1308. this.NewLineInternal(pBegin, pEnd);
  1309. }
  1310. this._drawingItem = null;
  1311. this._drawingNode = null;
  1312. this._drawingLine = null;
  1313. this.RefreshItems();
  1314. return;
  1315. }
  1316. else
  1317. {
  1318. // 用新加线段连接节点
  1319. if (this._mouseOperatingType == MouseOperatingType.DrawingLine)
  1320. {
  1321. Point pBegin = this._drawingLine.PointBegin;
  1322. Point pEnd = this._drawingLine.PointEnd;
  1323. FlowNode nodeBegin = this.SelectNodeByPoint(pBegin);
  1324. FlowNode nodeEnd = this._drawingLine.NodeEnd;
  1325. if (nodeEnd == null)
  1326. {
  1327. nodeEnd = this.SelectNodeByPoint(pEnd);
  1328. }
  1329. if ((nodeBegin == null && nodeEnd != null) ||
  1330. (nodeBegin != null && nodeEnd == null) ||
  1331. (nodeBegin != null && nodeEnd != null && nodeBegin.ItemCode != nodeEnd.ItemCode))
  1332. {
  1333. if (nodeBegin != null)
  1334. {
  1335. Point np1 = this.GetRectTopAndLeft(nodeBegin.Bounds);
  1336. Point np2 = this.GetRectTopAndRight(nodeBegin.Bounds);
  1337. Point np3 = this.GetRectBottomAndLeft(nodeBegin.Bounds);
  1338. Point np4 = this.GetRectBottomAndRight(nodeBegin.Bounds);
  1339. if (this.IsSegmentIntersect(pBegin, pEnd, np1, np2))
  1340. {
  1341. this.ConnectL2NPointBegin(this._drawingLine, nodeBegin, AnchorKind.Top);
  1342. }
  1343. else if (this.IsSegmentIntersect(pBegin, pEnd, np3, np4))
  1344. {
  1345. this.ConnectL2NPointBegin(this._drawingLine, nodeBegin, AnchorKind.Bottom);
  1346. }
  1347. else if (this.IsSegmentIntersect(pBegin, pEnd, np1, np3))
  1348. {
  1349. this.ConnectL2NPointBegin(this._drawingLine, nodeBegin, AnchorKind.Left);
  1350. }
  1351. else if (this.IsSegmentIntersect(pBegin, pEnd, np2, np4))
  1352. {
  1353. this.ConnectL2NPointBegin(this._drawingLine, nodeBegin, AnchorKind.Right);
  1354. }
  1355. }
  1356. if (this._drawingLine.AnchorEnd == AnchorKind.None && nodeEnd != null)
  1357. {
  1358. Point np1 = this.GetRectTopAndLeft(nodeEnd.Bounds);
  1359. Point np2 = this.GetRectTopAndRight(nodeEnd.Bounds);
  1360. Point np3 = this.GetRectBottomAndLeft(nodeEnd.Bounds);
  1361. Point np4 = this.GetRectBottomAndRight(nodeEnd.Bounds);
  1362. if (this.IsSegmentIntersect(pBegin, pEnd, np1, np2))
  1363. {
  1364. this.ConnectL2NPointEnd(this._drawingLine, nodeEnd, AnchorKind.Top);
  1365. }
  1366. else if (this.IsSegmentIntersect(pBegin, pEnd, np3, np4))
  1367. {
  1368. this.ConnectL2NPointEnd(this._drawingLine, nodeEnd, AnchorKind.Bottom);
  1369. }
  1370. else if (this.IsSegmentIntersect(pBegin, pEnd, np1, np3))
  1371. {
  1372. this.ConnectL2NPointEnd(this._drawingLine, nodeEnd, AnchorKind.Left);
  1373. }
  1374. else if (this.IsSegmentIntersect(pBegin, pEnd, np2, np4))
  1375. {
  1376. this.ConnectL2NPointEnd(this._drawingLine, nodeEnd, AnchorKind.Right);
  1377. }
  1378. }
  1379. }
  1380. this._drawingItem = null;
  1381. this._drawingNode = null;
  1382. this._drawingLine = null;
  1383. this.RefreshItems();
  1384. return;
  1385. }
  1386. this._drawingNode = null;
  1387. this._drawingLine = null;
  1388. }
  1389. #endregion 鼠标绘制item
  1390. #region 框选
  1391. if (this._mouseOperatingType == MouseOperatingType.RectSelect)
  1392. {
  1393. this.SelectItemByRect(this._mouseSelectRect);
  1394. this._mouseSelectRect = Rectangle.Empty;
  1395. this.RefreshItems();
  1396. //this._canvasControl.Invalidate();
  1397. return;
  1398. }
  1399. #endregion 框选
  1400. //this.SetDefaultCursor();
  1401. if (hasRefreshItems)
  1402. {
  1403. this.RefreshItems();
  1404. }
  1405. }
  1406. /// <summary>
  1407. /// 鼠标双击
  1408. /// </summary>
  1409. /// <param name="e"></param>
  1410. /// <returns></returns>
  1411. internal FlowNode MouseDoubleClick(MouseEventArgs e)
  1412. {
  1413. if (e.Button == MouseButtons.Left)
  1414. {
  1415. if (this._selectedItems.Count == 1)
  1416. {
  1417. FlowItem item = this._selectedItems[0];
  1418. if (item is FlowNode)
  1419. {
  1420. return item as FlowNode;
  1421. }
  1422. }
  1423. }
  1424. return null;
  1425. }
  1426. /// <summary>
  1427. /// 移动选择的项目
  1428. /// </summary>
  1429. /// <param name="xMove">X轴的移动量</param>
  1430. /// <param name="yMove">Y轴的移动量</param>
  1431. internal void MoveSelectedItems(int xMove, int yMove)
  1432. {
  1433. foreach (FlowItem item in this._selectedItems)
  1434. {
  1435. if (item is FlowNode)
  1436. {
  1437. FlowNode node = item as FlowNode;
  1438. node.Left += xMove;
  1439. node.Top += yMove;
  1440. // 移动连接线
  1441. foreach (FlowLine line in node.InLines)
  1442. {
  1443. if (!line.Selected)
  1444. {
  1445. line.PointEnd = node.GetAnchor(line.AnchorEnd);
  1446. }
  1447. }
  1448. // 移动连接线
  1449. foreach (FlowLine line in node.OutLines)
  1450. {
  1451. if (!line.Selected)
  1452. {
  1453. line.PointBegin = node.GetAnchor(line.AnchorBegin);
  1454. }
  1455. }
  1456. continue;
  1457. }
  1458. if (item is FlowLine)
  1459. {
  1460. FlowLine line = item as FlowLine;
  1461. Point p = line.PointBegin;
  1462. p.X += xMove;
  1463. p.Y += yMove;
  1464. line.PointBegin = p;
  1465. p = line.PointEnd;
  1466. p.X += xMove;
  1467. p.Y += yMove;
  1468. line.PointEnd = p;
  1469. // 断开连接
  1470. if (line.NodeBegin != null && !line.NodeBegin.Selected)
  1471. {
  1472. this.DisconnectL2NPointBegin(line);
  1473. }
  1474. if (line.NodeEnd != null && !line.NodeEnd.Selected)
  1475. {
  1476. this.DisconnectL2NPointEnd(line);
  1477. }
  1478. continue;
  1479. }
  1480. }
  1481. }
  1482. #endregion 鼠标操作
  1483. #endregion 内部方法
  1484. #region 私有方法
  1485. #region 新建项目
  1486. /// <summary>
  1487. /// 新建节点
  1488. /// </summary>
  1489. /// <param name="location">节点位置</param>
  1490. /// <returns>节点</returns>
  1491. private FlowNode NewNodeInternal(Point? location)
  1492. {
  1493. //FlowNode node = this._drawingItem as FlowNode;
  1494. FlowNode newNode = (this._drawingNode == null) ? new FlowNode(this) : this._drawingNode;
  1495. newNode.Selected = true;
  1496. if (location.HasValue)
  1497. {
  1498. newNode.Location = location.Value;
  1499. }
  1500. if (newNode.Bounds.IsEmpty)
  1501. {
  1502. newNode.Bounds = Consts.NODE_BOUNDS_DEFAULT;
  1503. }
  1504. if (newNode.Size.IsEmpty)
  1505. {
  1506. newNode.Size = Consts.NODE_BOUNDS_DEFAULT.Size;
  1507. }
  1508. newNode.NodeState = FlowNodeState.Added;
  1509. this.ClearSelection();
  1510. this._items.Add(newNode);
  1511. this._allNodes.Add(newNode);
  1512. this._selectedItems.Add(newNode);
  1513. this._drawingItem = null;
  1514. this._drawingNode = null;
  1515. this._drawingLine = null;
  1516. this.SetDefaultCursor();
  1517. return newNode;
  1518. }
  1519. /// <summary>
  1520. /// 新建线段
  1521. /// </summary>
  1522. /// <param name="pointBegin">起始点</param>
  1523. /// <param name="pointEnd">结算点</param>
  1524. /// <returns>线段</returns>
  1525. private FlowLine NewLineInternal(Point? pointBegin, Point? pointEnd)
  1526. {
  1527. //FlowLine line = this._drawingItem as FlowLine;
  1528. FlowLine newLine = (this._drawingLine == null) ? new FlowLine(this) : this._drawingLine;
  1529. newLine.Selected = true;
  1530. if (pointBegin.HasValue)
  1531. {
  1532. newLine.PointBegin = pointBegin.Value;
  1533. newLine.PointEnd = pointEnd.Value;
  1534. }
  1535. else
  1536. {
  1537. newLine.PointBegin = Consts.LINE_POINT_Begin_DEFAULT;
  1538. newLine.PointEnd = Consts.LINE_POINT_END_DEFAULT;
  1539. }
  1540. this.ClearSelection();
  1541. this._items.Add(newLine);
  1542. this._selectedItems.Add(newLine);
  1543. this._drawingItem = null;
  1544. this._drawingNode = null;
  1545. this._drawingLine = null;
  1546. this.SetDefaultCursor();
  1547. return newLine;
  1548. }
  1549. #endregion 新建项目
  1550. #region 绘制项目
  1551. /// <summary>
  1552. /// 绘制Item
  1553. /// </summary>
  1554. /// <param name="graphics">Graphics</param>
  1555. private void DrawItems(Graphics graphics)
  1556. {
  1557. foreach (FlowItem item in this._items)
  1558. {
  1559. this.DrawItem(graphics, item);
  1560. }
  1561. foreach (FlowItem item in this._items)
  1562. {
  1563. this.DrawSelectedItem(graphics, item);
  1564. }
  1565. }
  1566. ///// <summary>
  1567. ///// 绘制未激活的Item
  1568. ///// </summary>
  1569. ///// <param name="graphics">Graphics</param>
  1570. //private void DrawStaticItems(Graphics graphics)
  1571. //{
  1572. // foreach (FlowItem item in this._items)
  1573. // {
  1574. // // 不绘制激活的item
  1575. // if (item.MouseOperating)
  1576. // {
  1577. // continue;
  1578. // }
  1579. // this.DrawItem(graphics, item);
  1580. // }
  1581. //}
  1582. ///// <summary>
  1583. ///// 绘制激活的Item
  1584. ///// </summary>
  1585. ///// <param name="graphics">Graphics</param>
  1586. //private void DrawActiveItems(Graphics graphics)
  1587. //{
  1588. // foreach (FlowItem item in this._items)
  1589. // {
  1590. // // 绘制激活的item
  1591. // if (!item.MouseOperating)
  1592. // {
  1593. // continue;
  1594. // }
  1595. // this.DrawItem(graphics, item);
  1596. // }
  1597. //}
  1598. /// <summary>
  1599. /// 绘制图片
  1600. /// </summary>
  1601. /// <param name="graphics"></param>
  1602. /// <param name="image"></param>
  1603. /// <param name="rect"></param>
  1604. private void DrawImage(Graphics graphics, Image image, Rectangle rect)
  1605. {
  1606. graphics.DrawImage(image, rect);
  1607. }
  1608. /// <summary>
  1609. /// 绘制Item
  1610. /// </summary>
  1611. /// <param name="graphics">Graphics</param>
  1612. /// <param name="item">item</param>
  1613. private void DrawItem(Graphics graphics, FlowItem item)
  1614. {
  1615. if (item is FlowNode)
  1616. {
  1617. FlowNode node = item as FlowNode;
  1618. this.DrawNode(graphics, node);
  1619. return;
  1620. }
  1621. if (item is FlowLine)
  1622. {
  1623. FlowLine line = item as FlowLine;
  1624. this.DrawLine(graphics, line);
  1625. return;
  1626. }
  1627. }
  1628. /// <summary>
  1629. /// 绘制选中状态Item的附属
  1630. /// </summary>
  1631. /// <param name="graphics">Graphics</param>
  1632. /// <param name="item">item</param>
  1633. private void DrawSelectedItem(Graphics graphics, FlowItem item)
  1634. {
  1635. if (item is FlowNode)
  1636. {
  1637. FlowNode node = item as FlowNode;
  1638. this.DrawSelectedNode(graphics, node);
  1639. return;
  1640. }
  1641. if (item is FlowLine)
  1642. {
  1643. FlowLine line = item as FlowLine;
  1644. this.DrawSelectedLine(graphics, line);
  1645. return;
  1646. }
  1647. }
  1648. /// <summary>
  1649. /// 绘制节点
  1650. /// </summary>
  1651. /// <param name="graphics">Graphics</param>
  1652. /// <param name="node">节点</param>
  1653. private void DrawNode(Graphics graphics, FlowNode node)
  1654. {
  1655. // 填充颜色
  1656. if (node.FillColor.HasValue && node.FillColor != Color.Transparent)
  1657. {
  1658. this._penDrawNode.Color = node.FillColor.Value;
  1659. graphics.FillRectangle(this._penDrawNode.Brush, node.Bounds);
  1660. }
  1661. // 画图
  1662. if (node.NodeImage != null)
  1663. {
  1664. this.DrawImage(graphics, node.NodeImage, node.Bounds);
  1665. }
  1666. // 画边框
  1667. if (node.BorderWidth > 0 && node.BorderColor != Color.Transparent)
  1668. {
  1669. this._penDrawNode.Color = node.BorderColor;
  1670. this._penDrawNode.Width = node.BorderWidth;
  1671. graphics.DrawRectangle(this._penDrawNode, node.Bounds);
  1672. }
  1673. // 显示名称
  1674. if (node.ShowName)
  1675. {
  1676. //Point p5 = this.GetRectBottom(node.Bounds);
  1677. //SizeF size = graphics.MeasureString(node.Name, node.Font);
  1678. int mm = 40;
  1679. int mh = 20; // mm / 2;
  1680. int ww = node.Bounds.Width + mm;
  1681. int xx = node.Bounds.X - mh;
  1682. SizeF size = graphics.MeasureString(node.Name, node.Font, ww, _stringFormat);
  1683. this._penDrawString.Color = node.FontColor;
  1684. //graphics.DrawString(node.Name, node.Font, this._penDrawString.Brush, p5.X - size.Width / 2, p5.Y + Consts.NODE_STRING_MARGIN);
  1685. RectangleF ff = new RectangleF(xx, (float)(node.Bounds.Bottom + Consts.NODE_STRING_MARGIN), ww, size.Height);
  1686. string nn = node.ShortName;
  1687. if (string.IsNullOrEmpty(nn))
  1688. {
  1689. nn = node.Name;
  1690. }
  1691. /*
  1692. int ida = nn.IndexOf('-');
  1693. if (ida > 0)
  1694. {
  1695. nn = nn.Substring(ida + 1);
  1696. ida = nn.IndexOf('-');
  1697. if (ida > 0)
  1698. {
  1699. nn = nn.Substring(ida + 1);
  1700. }
  1701. }
  1702. */
  1703. graphics.DrawString(nn, node.Font, this._penDrawString.Brush, ff, _stringFormat);
  1704. }
  1705. // this.DrawSelectedNode(graphics, node);
  1706. }
  1707. /// <summary>
  1708. /// 绘制选中节点的附属
  1709. /// </summary>
  1710. /// <param name="graphics">Graphics</param>
  1711. /// <param name="node">节点</param>
  1712. private void DrawSelectedNode(Graphics graphics, FlowNode node)
  1713. {
  1714. Rectangle[] rects = node.GetSelectedStyle();
  1715. // 画激活状态
  1716. if (node.MouseOperating)
  1717. {
  1718. for (int i = 0; i < rects.Length; i++)
  1719. {
  1720. bool isAnchor = false;
  1721. switch ((NodePointKind)i)
  1722. {
  1723. case NodePointKind.Top:
  1724. if (node.TopLines != null && node.TopLines.Count > 0)
  1725. {
  1726. isAnchor = true;
  1727. }
  1728. break;
  1729. case NodePointKind.Bottom:
  1730. if (node.BottomLines != null && node.BottomLines.Count > 0)
  1731. {
  1732. isAnchor = true;
  1733. }
  1734. break;
  1735. case NodePointKind.Left:
  1736. if (node.LeftLines != null && node.LeftLines.Count > 0)
  1737. {
  1738. isAnchor = true;
  1739. }
  1740. break;
  1741. case NodePointKind.Right:
  1742. if (node.RightLines != null && node.RightLines.Count > 0)
  1743. {
  1744. isAnchor = true;
  1745. }
  1746. break;
  1747. default:
  1748. break;
  1749. }
  1750. this.DrawSelected(graphics, rects[i], isAnchor, true);
  1751. }
  1752. }
  1753. // 画选择状态
  1754. else if (node.Selected)
  1755. {
  1756. for (int i = 0; i < rects.Length; i++)
  1757. {
  1758. bool isAnchor = false;
  1759. switch ((NodePointKind)i)
  1760. {
  1761. case NodePointKind.Top:
  1762. if (node.TopLines != null && node.TopLines.Count > 0)
  1763. {
  1764. isAnchor = true;
  1765. }
  1766. break;
  1767. case NodePointKind.Bottom:
  1768. if (node.BottomLines != null && node.BottomLines.Count > 0)
  1769. {
  1770. isAnchor = true;
  1771. }
  1772. break;
  1773. case NodePointKind.Left:
  1774. if (node.LeftLines != null && node.LeftLines.Count > 0)
  1775. {
  1776. isAnchor = true;
  1777. }
  1778. break;
  1779. case NodePointKind.Right:
  1780. if (node.RightLines != null && node.RightLines.Count > 0)
  1781. {
  1782. isAnchor = true;
  1783. }
  1784. break;
  1785. default:
  1786. break;
  1787. }
  1788. this.DrawSelected(graphics, rects[i], isAnchor, false);
  1789. }
  1790. }
  1791. if (node.ShowAnchor)
  1792. {
  1793. Rectangle[] anchorRects = node.GetAnchorRects();
  1794. foreach (Rectangle rect in anchorRects)
  1795. {
  1796. this.DrawSelected(graphics, rect, true, true);
  1797. }
  1798. }
  1799. }
  1800. /// <summary>
  1801. /// 绘制线段
  1802. /// </summary>
  1803. /// <param name="graphics">Graphics</param>
  1804. /// <param name="line">线段</param>
  1805. private void DrawLine(Graphics graphics, FlowLine line)
  1806. {
  1807. this._penDrawLine.Color = line.LineColor;
  1808. this._penDrawLine.Width = line.LineWidth;
  1809. // 画线段
  1810. graphics.DrawLine(this._penDrawLine, line.PointBegin, line.PointEnd);
  1811. // 显示名称
  1812. if (line.ShowName)
  1813. {
  1814. Point mp = this.GetLineMidpoint(line.PointBegin, line.PointEnd);
  1815. SizeF size = graphics.MeasureString(line.Name, line.Font);
  1816. this._penDrawString.Color = line.FontColor;
  1817. graphics.DrawString(line.Name, line.Font, this._penDrawString.Brush, mp.X - size.Width / 2, mp.Y - size.Height / 2);
  1818. }
  1819. // this.DrawSelectedLine(graphics, node);
  1820. }
  1821. /// <summary>
  1822. /// 绘制选中线段的附属
  1823. /// </summary>
  1824. /// <param name="graphics">Graphics</param>
  1825. /// <param name="line">线段</param>
  1826. private void DrawSelectedLine(Graphics graphics, FlowLine line)
  1827. {
  1828. Rectangle[] rects = line.GetSelectedStyle();
  1829. // 画激活状态
  1830. if (line.MouseOperating)
  1831. {
  1832. if (line.NodeBegin != null)
  1833. {
  1834. if (!line.NodeBegin.Selected)
  1835. {
  1836. this.DrawSelected(graphics, rects[0], true, true);
  1837. }
  1838. }
  1839. else
  1840. {
  1841. this.DrawSelected(graphics, rects[0], false, true);
  1842. }
  1843. if (line.NodeEnd != null)
  1844. {
  1845. if (!line.NodeEnd.Selected)
  1846. {
  1847. this.DrawSelected(graphics, rects[1], true, true);
  1848. }
  1849. }
  1850. else
  1851. {
  1852. this.DrawSelected(graphics, rects[1], false, true);
  1853. }
  1854. }
  1855. // 画选择状态
  1856. else if (line.Selected)
  1857. {
  1858. if (line.NodeBegin != null)
  1859. {
  1860. if (!line.NodeBegin.Selected)
  1861. {
  1862. this.DrawSelected(graphics, rects[0], true, false);
  1863. }
  1864. }
  1865. else
  1866. {
  1867. this.DrawSelected(graphics, rects[0], false, false);
  1868. }
  1869. if (line.NodeEnd != null)
  1870. {
  1871. if (!line.NodeEnd.Selected)
  1872. {
  1873. this.DrawSelected(graphics, rects[1], true, false);
  1874. }
  1875. }
  1876. else
  1877. {
  1878. this.DrawSelected(graphics, rects[1], false, false);
  1879. }
  1880. }
  1881. }
  1882. /// <summary>
  1883. /// 绘制折线
  1884. /// </summary>
  1885. /// <param name="graphics"></param>
  1886. /// <param name="line"></param>
  1887. private void DrawPolyline(Graphics graphics, FlowPolyline line)
  1888. {
  1889. }
  1890. /// <summary>
  1891. /// 绘制选择Item的附属
  1892. /// </summary>
  1893. /// <param name="graphics"></param>
  1894. /// <param name="rect"></param>
  1895. /// <param name="isAnchor"></param>
  1896. /// <param name="operating"></param>
  1897. private void DrawSelected(Graphics graphics, Rectangle rect, bool isAnchor, bool operating)
  1898. {
  1899. if (operating)
  1900. {
  1901. if (isAnchor)
  1902. {
  1903. graphics.FillEllipse(Consts.SELECT_POINT_BRUSH_LINE_TO_NODE, rect);
  1904. graphics.DrawEllipse(Consts.SELECT_POINT_PEN, rect);
  1905. }
  1906. else
  1907. {
  1908. graphics.FillEllipse(Consts.SELECT_POINT_BRUSH, rect);
  1909. graphics.DrawEllipse(Consts.SELECT_POINT_PEN, rect);
  1910. }
  1911. }
  1912. else
  1913. {
  1914. if (isAnchor)
  1915. {
  1916. graphics.FillRectangle(Consts.SELECT_POINT_BRUSH_LINE_TO_NODE, rect);
  1917. graphics.DrawRectangle(Consts.SELECT_POINT_PEN, rect);
  1918. }
  1919. else
  1920. {
  1921. graphics.FillRectangle(Consts.SELECT_POINT_BRUSH, rect);
  1922. graphics.DrawRectangle(Consts.SELECT_POINT_PEN, rect);
  1923. }
  1924. }
  1925. }
  1926. #endregion 绘制项目
  1927. #region 鼠标样式
  1928. /// <summary>
  1929. /// 设置鼠标样式
  1930. /// </summary>
  1931. /// <param name="type"></param>
  1932. private void SetCursor(MouseOperatingType type)
  1933. {
  1934. this._mouseOperatingType = type;
  1935. switch (type)
  1936. {
  1937. case MouseOperatingType.None:
  1938. this.CanvasCursor = Cursors.Default;
  1939. break;
  1940. case MouseOperatingType.RectSelect:
  1941. this.CanvasCursor = Cursors.Cross;
  1942. break;
  1943. case MouseOperatingType.Move:
  1944. this.CanvasCursor = Cursors.SizeAll;
  1945. break;
  1946. case MouseOperatingType.NodeTopAndLeft:
  1947. case MouseOperatingType.NodeBottomAndRight:
  1948. this.CanvasCursor = Cursors.SizeNWSE;
  1949. break;
  1950. case MouseOperatingType.NodeTopAndRight:
  1951. case MouseOperatingType.NodeBottomAndLeft:
  1952. this.CanvasCursor = Cursors.SizeNESW;
  1953. break;
  1954. case MouseOperatingType.NodeTop:
  1955. case MouseOperatingType.NodeBottom:
  1956. this.CanvasCursor = Cursors.SizeNS;
  1957. break;
  1958. case MouseOperatingType.NodeLeft:
  1959. case MouseOperatingType.NodeRight:
  1960. this.CanvasCursor = Cursors.SizeWE;
  1961. break;
  1962. case MouseOperatingType.LineBegin:
  1963. case MouseOperatingType.LineEnd:
  1964. this.CanvasCursor = Cursors.Hand;
  1965. break;
  1966. case MouseOperatingType.DrawingNode:
  1967. this.CanvasCursor = ItemCursor.NodeCursor;
  1968. break;
  1969. case MouseOperatingType.DrawingLine:
  1970. this.CanvasCursor = ItemCursor.LineCursor;
  1971. break;
  1972. default:
  1973. this.CanvasCursor = Cursors.Default;
  1974. break;
  1975. }
  1976. }
  1977. /// <summary>
  1978. /// 设置手动画节点时鼠标样式
  1979. /// </summary>
  1980. private void SetDrawingNodeCursor()
  1981. {
  1982. this.SetCursor(MouseOperatingType.DrawingNode);
  1983. }
  1984. /// <summary>
  1985. /// 设置手动画节点时鼠标样式
  1986. /// </summary>
  1987. private void SetDrawingLineCursor()
  1988. {
  1989. this.SetCursor(MouseOperatingType.DrawingLine);
  1990. }
  1991. /// <summary>
  1992. /// 设置框选时鼠标样式
  1993. /// </summary>
  1994. private void SetRectSelectCursor()
  1995. {
  1996. this.SetCursor(MouseOperatingType.RectSelect);
  1997. }
  1998. /// <summary>
  1999. /// 设置默认鼠标样式
  2000. /// </summary>
  2001. private void SetDefaultCursor()
  2002. {
  2003. this.SetCursor(MouseOperatingType.None);
  2004. }
  2005. /// <summary>
  2006. /// 设置移动时鼠标样式
  2007. /// </summary>
  2008. private void SetMoveCursor()
  2009. {
  2010. this.SetCursor(MouseOperatingType.Move);
  2011. }
  2012. /// <summary>
  2013. /// 设置改变节点大小时鼠标样式
  2014. /// </summary>
  2015. /// <param name="kind">节点端点</param>
  2016. private void SetNodeResizeCursor(NodePointKind kind)
  2017. {
  2018. MouseOperatingType type = MouseOperatingType.None;
  2019. switch (kind)
  2020. {
  2021. case NodePointKind.TopAndLeft:
  2022. type = MouseOperatingType.NodeTopAndLeft;
  2023. break;
  2024. case NodePointKind.TopAndRight:
  2025. type = MouseOperatingType.NodeTopAndRight;
  2026. break;
  2027. case NodePointKind.BottomAndLeft:
  2028. type = MouseOperatingType.NodeBottomAndLeft;
  2029. break;
  2030. case NodePointKind.BottomAndRight:
  2031. type = MouseOperatingType.NodeBottomAndRight;
  2032. break;
  2033. case NodePointKind.Top:
  2034. type = MouseOperatingType.NodeTop;
  2035. break;
  2036. case NodePointKind.Bottom:
  2037. type = MouseOperatingType.NodeBottom;
  2038. break;
  2039. case NodePointKind.Left:
  2040. type = MouseOperatingType.NodeLeft;
  2041. break;
  2042. case NodePointKind.Right:
  2043. type = MouseOperatingType.NodeRight;
  2044. break;
  2045. default:
  2046. type = MouseOperatingType.None;
  2047. break;
  2048. }
  2049. this.SetCursor(type);
  2050. }
  2051. /// <summary>
  2052. /// 设置改变线段端点时鼠标样式
  2053. /// </summary>
  2054. /// <param name="kind">线段端点</param>
  2055. private void SetLineResizeCursor(LinePointKind kind)
  2056. {
  2057. MouseOperatingType type = MouseOperatingType.None;
  2058. switch (kind)
  2059. {
  2060. case LinePointKind.Begin:
  2061. type = MouseOperatingType.LineBegin;
  2062. break;
  2063. case LinePointKind.End:
  2064. type = MouseOperatingType.LineEnd;
  2065. break;
  2066. default:
  2067. type = MouseOperatingType.None;
  2068. break;
  2069. }
  2070. this.SetCursor(type);
  2071. }
  2072. /// <summary>
  2073. /// 鼠标移动时,根据鼠标位置设置鼠标样式
  2074. /// </summary>
  2075. /// <param name="point">鼠标位置</param>
  2076. private void SetMouseCursorByMove(Point point)
  2077. {
  2078. this._nodeSizeChanging = null;
  2079. this._linePointChanging = null;
  2080. // item附属判断
  2081. for (int i = this._items.Count - 1; i >= 0; i--)
  2082. {
  2083. FlowItem item = this._items[i];
  2084. if (!item.Selected)
  2085. {
  2086. continue;
  2087. }
  2088. Rectangle[] rects = item.GetSelectedStyle();
  2089. if (item is FlowNode)
  2090. {
  2091. FlowNode node = item as FlowNode;
  2092. for (int index = 0; index < rects.Length; index++)
  2093. {
  2094. if (rects[index].Contains(point))
  2095. {
  2096. NodePointKind kind = (NodePointKind)index;
  2097. // 优先操作连接节点的线段
  2098. switch (kind)
  2099. {
  2100. case NodePointKind.Top:
  2101. if (!this.CanOperateNode(node.TopLines))
  2102. {
  2103. continue;
  2104. }
  2105. break;
  2106. case NodePointKind.Bottom:
  2107. if (!this.CanOperateNode(node.BottomLines))
  2108. {
  2109. continue;
  2110. }
  2111. break;
  2112. case NodePointKind.Left:
  2113. if (!this.CanOperateNode(node.LeftLines))
  2114. {
  2115. continue;
  2116. }
  2117. break;
  2118. case NodePointKind.Right:
  2119. if (!this.CanOperateNode(node.RightLines))
  2120. {
  2121. continue;
  2122. }
  2123. break;
  2124. default:
  2125. break;
  2126. }
  2127. this.SetNodeResizeCursor(kind);
  2128. this._nodeSizeChanging = node;
  2129. return;
  2130. }
  2131. }
  2132. }
  2133. if (this._flowBoxMode != FlowBoxMode.Edit)
  2134. {
  2135. continue;
  2136. }
  2137. if (item is FlowLine)
  2138. {
  2139. for (int index = 0; index < rects.Length; index++)
  2140. {
  2141. if (rects[index].Contains(point))
  2142. {
  2143. LinePointKind kind = (LinePointKind)index;
  2144. this.SetLineResizeCursor(kind);
  2145. this._linePointChanging = item as FlowLine;
  2146. return;
  2147. }
  2148. }
  2149. }
  2150. }
  2151. // item内部判断
  2152. for (int i = this._items.Count - 1; i >= 0; i--)
  2153. {
  2154. FlowItem item = this._items[i];
  2155. if (!item.Selected)
  2156. {
  2157. continue;
  2158. }
  2159. if (item is FlowNode)
  2160. {
  2161. if (this.IsPointOnNode(point, item as FlowNode))
  2162. {
  2163. this.SetMoveCursor();
  2164. return;
  2165. }
  2166. }
  2167. if (this._flowBoxMode != FlowBoxMode.Edit)
  2168. {
  2169. continue;
  2170. }
  2171. if (item is FlowLine)
  2172. {
  2173. if (this.IsPointOnLine(point, item as FlowLine))
  2174. {
  2175. this.SetMoveCursor();
  2176. return;
  2177. }
  2178. }
  2179. }
  2180. this._nodeSizeChanging = null;
  2181. this._linePointChanging = null;
  2182. this.SetDefaultCursor();
  2183. }
  2184. #endregion 鼠标样式
  2185. #region 流程控制
  2186. /// <summary>
  2187. /// 连接PointBegin到节点
  2188. /// </summary>
  2189. /// <param name="line">线段</param>
  2190. /// <param name="node">节点</param>
  2191. /// <param name="anchorKind">锚点</param>
  2192. private void ConnectL2NPointBegin(FlowLine line, FlowNode node, AnchorKind anchorKind)
  2193. {
  2194. line.AnchorBegin = anchorKind;
  2195. // 连接节点
  2196. line.PointBegin = node.GetAnchor(anchorKind);
  2197. line.NodeBegin = node;
  2198. node.OutLines.Add(line);
  2199. switch (anchorKind)
  2200. {
  2201. case AnchorKind.Top:
  2202. node.TopLines.Add(line);
  2203. break;
  2204. case AnchorKind.Bottom:
  2205. node.BottomLines.Add(line);
  2206. break;
  2207. case AnchorKind.Left:
  2208. node.LeftLines.Add(line);
  2209. break;
  2210. case AnchorKind.Right:
  2211. node.RightLines.Add(line);
  2212. break;
  2213. default:
  2214. break;
  2215. }
  2216. }
  2217. /// <summary>
  2218. /// 连接PointEnd到节点
  2219. /// </summary>
  2220. /// <param name="line">线段</param>
  2221. /// <param name="node">节点</param>
  2222. /// <param name="anchorKind">锚点</param>
  2223. private void ConnectL2NPointEnd(FlowLine line, FlowNode node, AnchorKind anchorKind)
  2224. {
  2225. line.AnchorEnd = anchorKind;
  2226. // 连接节点
  2227. line.PointEnd = node.GetAnchor(anchorKind);
  2228. line.NodeEnd = node;
  2229. node.InLines.Add(line);
  2230. switch (anchorKind)
  2231. {
  2232. case AnchorKind.Top:
  2233. node.TopLines.Add(line);
  2234. break;
  2235. case AnchorKind.Bottom:
  2236. node.BottomLines.Add(line);
  2237. break;
  2238. case AnchorKind.Left:
  2239. node.LeftLines.Add(line);
  2240. break;
  2241. case AnchorKind.Right:
  2242. node.RightLines.Add(line);
  2243. break;
  2244. default:
  2245. break;
  2246. }
  2247. }
  2248. /// <summary>
  2249. /// 断开PointBegin的连接
  2250. /// </summary>
  2251. /// <param name="line">线段</param>
  2252. private void DisconnectL2NPointBegin(FlowLine line)
  2253. {
  2254. switch (line.AnchorBegin)
  2255. {
  2256. case AnchorKind.Top:
  2257. line.NodeBegin.TopLines.Remove(line);
  2258. break;
  2259. case AnchorKind.Bottom:
  2260. line.NodeBegin.BottomLines.Remove(line);
  2261. break;
  2262. case AnchorKind.Left:
  2263. line.NodeBegin.LeftLines.Remove(line);
  2264. break;
  2265. case AnchorKind.Right:
  2266. line.NodeBegin.RightLines.Remove(line);
  2267. break;
  2268. default:
  2269. break;
  2270. }
  2271. line.NodeBegin.OutLines.Remove(line);
  2272. line.NodeBegin = null;
  2273. line.AnchorBegin = AnchorKind.None;
  2274. }
  2275. /// <summary>
  2276. /// 断开PointEnd的连接
  2277. /// </summary>
  2278. /// <param name="line">线段</param>
  2279. private void DisconnectL2NPointEnd(FlowLine line)
  2280. {
  2281. switch (line.AnchorEnd)
  2282. {
  2283. case AnchorKind.Top:
  2284. line.NodeEnd.TopLines.Remove(line);
  2285. break;
  2286. case AnchorKind.Bottom:
  2287. line.NodeEnd.BottomLines.Remove(line);
  2288. break;
  2289. case AnchorKind.Left:
  2290. line.NodeEnd.LeftLines.Remove(line);
  2291. break;
  2292. case AnchorKind.Right:
  2293. line.NodeEnd.RightLines.Remove(line);
  2294. break;
  2295. default:
  2296. break;
  2297. }
  2298. line.NodeEnd.InLines.Remove(line);
  2299. line.NodeEnd = null;
  2300. line.AnchorEnd = AnchorKind.None;
  2301. }
  2302. /// <summary>
  2303. /// 获得可连接的节点
  2304. /// </summary>
  2305. /// <param name="point">鼠标位置</param>
  2306. /// <returns>可连接的节点</returns>
  2307. private FlowNode GetL2N(Point point)
  2308. {
  2309. FlowNode inNode = null;
  2310. FlowNode edgeNode = null;
  2311. for (int i = this._items.Count - 1; i >= 0; i--)
  2312. {
  2313. FlowNode node = this._items[i] as FlowNode;
  2314. if (node == null)
  2315. {
  2316. continue;
  2317. }
  2318. node.ShowAnchor = false;
  2319. // 在节点范围内
  2320. if (node.Bounds.Contains(point))
  2321. {
  2322. inNode = node;
  2323. }
  2324. // 在节点边缘
  2325. Rectangle rect = node.Bounds;
  2326. rect.Inflate(Consts.L2N_PRECISION_EDGE, Consts.L2N_PRECISION_EDGE);
  2327. if (rect.Contains(point))
  2328. {
  2329. edgeNode = node;
  2330. }
  2331. }
  2332. if (inNode != null)
  2333. {
  2334. inNode.ShowAnchor = true;
  2335. return inNode;
  2336. }
  2337. if (edgeNode != null)
  2338. {
  2339. edgeNode.ShowAnchor = true;
  2340. return edgeNode;
  2341. }
  2342. return null;
  2343. }
  2344. #endregion 流程控制
  2345. #region 鼠标操作
  2346. /// <summary>
  2347. /// 框选Item
  2348. /// </summary>
  2349. /// <param name="rect">框选范围</param>
  2350. private void SelectItemByRect(Rectangle rect)
  2351. {
  2352. this.ClearSelection();
  2353. foreach (FlowItem item in this._items)
  2354. {
  2355. if (item is FlowNode)
  2356. {
  2357. FlowNode node = item as FlowNode;
  2358. if (rect.IntersectsWith(node.Bounds))
  2359. {
  2360. node.Selected = true;
  2361. this._selectedItems.Add(node);
  2362. }
  2363. else
  2364. {
  2365. node.Selected = false;
  2366. }
  2367. continue;
  2368. }
  2369. if (this._flowBoxMode != FlowBoxMode.Edit)
  2370. {
  2371. continue;
  2372. }
  2373. if (item is FlowLine)
  2374. {
  2375. FlowLine line = item as FlowLine;
  2376. if (rect.Contains(line.PointBegin) || rect.Contains(line.PointEnd))
  2377. {
  2378. line.Selected = true;
  2379. this._selectedItems.Add(line);
  2380. }
  2381. else
  2382. {
  2383. line.Selected = false;
  2384. }
  2385. continue;
  2386. }
  2387. }
  2388. }
  2389. /// <summary>
  2390. /// 点选Item
  2391. /// </summary>
  2392. /// <param name="point">鼠标点</param>
  2393. /// <returns>选中的item</returns>
  2394. private FlowItem SelectItemByPoint(Point point)
  2395. {
  2396. for (int i = this._items.Count - 1; i >= 0; i--)
  2397. {
  2398. FlowItem item = this._items[i];
  2399. if (item is FlowNode)
  2400. {
  2401. if (this.IsPointOnNode(point, item as FlowNode))
  2402. {
  2403. return item;
  2404. }
  2405. }
  2406. if (this._flowBoxMode != FlowBoxMode.Edit)
  2407. {
  2408. continue;
  2409. }
  2410. if (item is FlowLine)
  2411. {
  2412. if (this.IsPointOnLine(point, item as FlowLine))
  2413. {
  2414. return item;
  2415. }
  2416. }
  2417. //if (item is FlowPolyline)
  2418. //{
  2419. // if (this.IsPointOnPolyLine(point, item as FlowPolyline))
  2420. // {
  2421. // return item;
  2422. // }
  2423. //}
  2424. }
  2425. return null;
  2426. }
  2427. /// <summary>
  2428. /// 点选节点
  2429. /// </summary>
  2430. /// <param name="point">鼠标点</param>
  2431. /// <returns>选中的节点</returns>
  2432. private FlowNode SelectNodeByPoint(Point point)
  2433. {
  2434. for (int i = this._items.Count - 1; i >= 0; i--)
  2435. {
  2436. FlowItem item = this._items[i];
  2437. if (item is FlowNode)
  2438. {
  2439. FlowNode node = item as FlowNode;
  2440. if (this.IsPointOnNode(point, node))
  2441. {
  2442. return node;
  2443. }
  2444. }
  2445. }
  2446. return null;
  2447. }
  2448. /// <summary>
  2449. /// 能否操作节点(鼠标在锚点)
  2450. /// </summary>
  2451. /// <param name="lines">连接的线段</param>
  2452. /// <returns>能否操作节点</returns>
  2453. private bool CanOperateNode(List<FlowLine> lines)
  2454. {
  2455. if (lines != null && lines.Count > 0)
  2456. {
  2457. foreach (FlowLine line in lines)
  2458. {
  2459. if (line.Selected)
  2460. {
  2461. return false;
  2462. }
  2463. }
  2464. }
  2465. return true;
  2466. }
  2467. #endregion 鼠标操作
  2468. #region 工具
  2469. /// <summary>
  2470. /// 判断线段是否相交
  2471. /// </summary>
  2472. /// <param name="p1">线段1的开始点</param>
  2473. /// <param name="p2">线段1的结束点</param>
  2474. /// <param name="p3">线段2的开始点</param>
  2475. /// <param name="p4">线段2的结束点</param>
  2476. /// <returns>是否相交</returns>
  2477. private bool IsSegmentIntersect(Point p1, Point p2, Point p3, Point p4)
  2478. {
  2479. double d1 = this.GetDirection(p3, p4, p1);
  2480. double d2 = this.GetDirection(p3, p4, p2);
  2481. double d3 = this.GetDirection(p1, p2, p3);
  2482. double d4 = this.GetDirection(p1, p2, p4);
  2483. if (d1 * d2 < 0 && d3 * d4 < 0)
  2484. {
  2485. return true;
  2486. }
  2487. else if (d1 == 0 && this.IsOnSegment(p3, p4, p1))
  2488. {
  2489. return true;
  2490. }
  2491. else if (d2 == 0 && this.IsOnSegment(p3, p4, p2))
  2492. {
  2493. return true;
  2494. }
  2495. else if (d3 == 0 && this.IsOnSegment(p1, p2, p3))
  2496. {
  2497. return true;
  2498. }
  2499. else if (d4 == 0 && this.IsOnSegment(p1, p2, p4))
  2500. {
  2501. return true;
  2502. }
  2503. else
  2504. {
  2505. return false;
  2506. }
  2507. }
  2508. /// <summary>
  2509. /// 获取点1到点2和点3的向量的叉乘
  2510. /// </summary>
  2511. /// <param name="p1">点1</param>
  2512. /// <param name="p2">点2</param>
  2513. /// <param name="p3">点3</param>
  2514. /// <returns></returns>
  2515. private int GetDirection(Point p1, Point p2, Point p3)
  2516. {
  2517. Point v1 = new Point(p3.X - p1.X, p3.Y - p1.Y);
  2518. Point v2 = new Point(p2.X - p1.X, p2.Y - p1.Y);
  2519. return v1.X * v2.Y - v1.Y * v2.X;
  2520. }
  2521. /// <summary>
  2522. /// 点3在线上时,判断点3在线段上,还是在线段的延长线上
  2523. /// </summary>
  2524. /// <param name="p1">线段的开始点</param>
  2525. /// <param name="p2">线段的结束点</param>
  2526. /// <param name="p3">点3</param>
  2527. /// <returns></returns>
  2528. private bool IsOnSegment(Point p1, Point p2, Point p3)
  2529. {
  2530. int x_min, x_max, y_min, y_max;
  2531. if (p1.X < p2.X)
  2532. {
  2533. x_min = p1.X;
  2534. x_max = p2.X;
  2535. }
  2536. else
  2537. {
  2538. x_min = p2.X;
  2539. x_max = p1.X;
  2540. }
  2541. if (p1.Y < p2.Y)
  2542. {
  2543. y_min = p1.Y;
  2544. y_max = p2.Y;
  2545. }
  2546. else
  2547. {
  2548. y_min = p2.Y;
  2549. y_max = p1.Y;
  2550. }
  2551. if (p3.X < x_min || p3.X > x_max || p3.Y < y_min || p3.Y > y_max)
  2552. {
  2553. return false;
  2554. }
  2555. else
  2556. {
  2557. return true;
  2558. }
  2559. }
  2560. /// <summary>
  2561. /// 点是否在节点上
  2562. /// </summary>
  2563. /// <param name="point">点</param>
  2564. /// <param name="node">节点</param>
  2565. /// <returns>是否在节点上</returns>
  2566. private bool IsPointOnNode(Point point, FlowNode node)
  2567. {
  2568. return node.Bounds.Contains(point);
  2569. }
  2570. /// <summary>
  2571. /// 点是否在线段上
  2572. /// </summary>
  2573. /// <param name="point">点</param>
  2574. /// <param name="line">线段</param>
  2575. /// <returns>否在线段上</returns>
  2576. private bool IsPointOnLine(Point point, FlowLine line)
  2577. {
  2578. double p2ls = this.GetP2LSDistance(point, line.PointBegin, line.PointEnd);
  2579. //if (p2ls <= (line.LineWidth - 1) / 2 + Consts.P2L_PRECISION)
  2580. if (p2ls <= (line.LineWidth) / 2 + Consts.P2L_PRECISION)
  2581. {
  2582. return true;
  2583. }
  2584. return false;
  2585. }
  2586. /// <summary>
  2587. /// 获取点到线段的距离
  2588. /// </summary>
  2589. /// <param name="p">点</param>
  2590. /// <param name="pBegin">线段的端点</param>
  2591. /// <param name="pEnd">线段的端点</param>
  2592. /// <returns>点到线段的距离</returns>
  2593. private double GetP2LSDistance(Point p, Point pBegin, Point pEnd)
  2594. {
  2595. // 点在线段端点
  2596. double pp1 = this.GetP2PDistance(p, pBegin);
  2597. if (pp1 < Consts.DOUBLE_PRECISION)
  2598. {
  2599. return pp1;
  2600. }
  2601. double pp2 = this.GetP2PDistance(p, pEnd);
  2602. if (pp2 < Consts.DOUBLE_PRECISION)
  2603. {
  2604. return pp2;
  2605. }
  2606. double p1p2 = this.GetP2PDistance(pBegin, pEnd);
  2607. // 线段端点相同
  2608. if (p1p2 < Consts.DOUBLE_PRECISION)
  2609. {
  2610. return pp1;
  2611. }
  2612. // 点在线段端点外
  2613. double pp12 = pp1 * pp1;
  2614. double pp22 = pp2 * pp2;
  2615. double p1p22 = p1p2 * p1p2;
  2616. if (pp12 >= pp22 + p1p22)
  2617. {
  2618. return pp22;
  2619. }
  2620. if (pp22 >= pp12 + p1p22)
  2621. {
  2622. return pp12;
  2623. }
  2624. // 点在线段端点内
  2625. // 周长的一半
  2626. double l = (pp1 + pp2 + p1p2) / 2;
  2627. // 海伦公式求面积,也可以用矢量求
  2628. double s = Math.Sqrt(l * (l - pp1) * (l - pp2) * (l - p1p2));
  2629. return 2 * s / p1p2;
  2630. }
  2631. /// <summary>
  2632. /// 获取两点间的距离
  2633. /// </summary>
  2634. /// <param name="point1"></param>
  2635. /// <param name="point2"></param>
  2636. /// <returns>两点间的距离</returns>
  2637. private double GetP2PDistance(Point point1, Point point2)
  2638. {
  2639. return Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y));
  2640. }
  2641. /// <summary>
  2642. /// 获取鼠标操作后的新范围
  2643. /// </summary>
  2644. /// <param name="rect">源范围</param>
  2645. /// <param name="fixedPoint">固定点</param>
  2646. /// <param name="newPoint">移动点</param>
  2647. /// <returns>新范围</returns>
  2648. private Rectangle GetNewRect(Rectangle rect, Point fixedPoint, Point newPoint)
  2649. {
  2650. int xRect = newPoint.X - fixedPoint.X;
  2651. int yRect = newPoint.Y - fixedPoint.Y;
  2652. if (xRect < 0)
  2653. {
  2654. rect.X = newPoint.X;
  2655. rect.Width = -xRect;
  2656. }
  2657. else
  2658. {
  2659. rect.X = fixedPoint.X;
  2660. rect.Width = xRect;
  2661. }
  2662. if (yRect < 0)
  2663. {
  2664. rect.Y = newPoint.Y;
  2665. rect.Height = -yRect;
  2666. }
  2667. else
  2668. {
  2669. rect.Y = fixedPoint.Y;
  2670. rect.Height = yRect;
  2671. }
  2672. return rect;
  2673. }
  2674. /// <summary>
  2675. /// 获取鼠标操作后的新范围
  2676. /// </summary>
  2677. /// <param name="rect">源范围</param>
  2678. /// <param name="fixedPoint">固定点</param>
  2679. /// <param name="xRect">x方向移动距离</param>
  2680. /// <param name="yRect">y方向移动距离</param>
  2681. /// <returns>新范围</returns>
  2682. private Rectangle GetNewRect(Rectangle rect, Point fixedPoint, int xRect, int yRect)
  2683. {
  2684. if (xRect < 0)
  2685. {
  2686. rect.X = fixedPoint.X + xRect;
  2687. rect.Width = -xRect;
  2688. }
  2689. else
  2690. {
  2691. rect.X = fixedPoint.X;
  2692. rect.Width = xRect;
  2693. }
  2694. if (yRect < 0)
  2695. {
  2696. rect.Y = fixedPoint.Y + yRect;
  2697. rect.Height = -yRect;
  2698. }
  2699. else
  2700. {
  2701. rect.Y = fixedPoint.Y;
  2702. rect.Height = yRect;
  2703. }
  2704. return rect;
  2705. }
  2706. /// <summary>
  2707. /// 获得矩形的左上角
  2708. /// </summary>
  2709. /// <param name="rect">矩形</param>
  2710. /// <returns>左上角</returns>
  2711. private Point GetRectTopAndLeft(Rectangle rect)
  2712. {
  2713. return rect.Location;
  2714. }
  2715. /// <summary>
  2716. /// 获得矩形的右上角
  2717. /// </summary>
  2718. /// <param name="rect">矩形</param>
  2719. /// <returns>右上角</returns>
  2720. private Point GetRectTopAndRight(Rectangle rect)
  2721. {
  2722. return new Point(rect.Right, rect.Top);
  2723. }
  2724. /// <summary>
  2725. /// 获得矩形的左下角
  2726. /// </summary>
  2727. /// <param name="rect">矩形</param>
  2728. /// <returns>左下角</returns>
  2729. private Point GetRectBottomAndLeft(Rectangle rect)
  2730. {
  2731. return new Point(rect.Left, rect.Bottom);
  2732. }
  2733. /// <summary>
  2734. /// 获得矩形的右下角
  2735. /// </summary>
  2736. /// <param name="rect">矩形</param>
  2737. /// <returns>右下角</returns>
  2738. private Point GetRectBottomAndRight(Rectangle rect)
  2739. {
  2740. return new Point(rect.Right, rect.Bottom);
  2741. }
  2742. /// <summary>
  2743. /// 获得矩形的上中点
  2744. /// </summary>
  2745. /// <param name="rect">矩形</param>
  2746. /// <returns>上中点</returns>
  2747. private Point GetRectTop(Rectangle rect)
  2748. {
  2749. return new Point(rect.Left + rect.Width / 2, rect.Top);
  2750. }
  2751. /// <summary>
  2752. /// 获得矩形的下中点
  2753. /// </summary>
  2754. /// <param name="rect">矩形</param>
  2755. /// <returns>下中点</returns>
  2756. private Point GetRectBottom(Rectangle rect)
  2757. {
  2758. return new Point(rect.Left + rect.Width / 2, rect.Bottom);
  2759. }
  2760. /// <summary>
  2761. /// 获得矩形的左中点
  2762. /// </summary>
  2763. /// <param name="rect">矩形</param>
  2764. /// <returns>左中点</returns>
  2765. private Point GetRectLeft(Rectangle rect)
  2766. {
  2767. return new Point(rect.Left, rect.Top + rect.Height / 2);
  2768. }
  2769. /// <summary>
  2770. /// 获得矩形的右中点
  2771. /// </summary>
  2772. /// <param name="rect">矩形</param>
  2773. /// <returns>右中点</returns>
  2774. private Point GetRectRight(Rectangle rect)
  2775. {
  2776. return new Point(rect.Right, rect.Top + rect.Height / 2);
  2777. }
  2778. /// <summary>
  2779. /// 获得线段的中点
  2780. /// </summary>
  2781. /// <param name="pointBegin">线段开始点</param>
  2782. /// <param name="pointEnd">线段结束点</param>
  2783. /// <returns>中点</returns>
  2784. private Point GetLineMidpoint(Point pointBegin, Point pointEnd)
  2785. {
  2786. return new Point((pointBegin.X + pointEnd.X) / 2, (pointBegin.Y + pointEnd.Y) / 2);
  2787. }
  2788. #endregion 工具
  2789. #endregion 私有方法
  2790. }
  2791. }