ItemManager.cs 100 KB

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