ItemManager.cs 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174
  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. // 不显示无效节点 2024-11-27 by chenxy
  1560. if (!item.IsShow)
  1561. {
  1562. continue;
  1563. }
  1564. this.DrawItem(graphics, item);
  1565. }
  1566. foreach (FlowItem item in this._items)
  1567. {
  1568. // 不显示无效节点 2024-11-27 by chenxy
  1569. if (!item.IsShow)
  1570. {
  1571. continue;
  1572. }
  1573. this.DrawSelectedItem(graphics, item);
  1574. }
  1575. }
  1576. ///// <summary>
  1577. ///// 绘制未激活的Item
  1578. ///// </summary>
  1579. ///// <param name="graphics">Graphics</param>
  1580. //private void DrawStaticItems(Graphics graphics)
  1581. //{
  1582. // foreach (FlowItem item in this._items)
  1583. // {
  1584. // // 不绘制激活的item
  1585. // if (item.MouseOperating)
  1586. // {
  1587. // continue;
  1588. // }
  1589. // this.DrawItem(graphics, item);
  1590. // }
  1591. //}
  1592. ///// <summary>
  1593. ///// 绘制激活的Item
  1594. ///// </summary>
  1595. ///// <param name="graphics">Graphics</param>
  1596. //private void DrawActiveItems(Graphics graphics)
  1597. //{
  1598. // foreach (FlowItem item in this._items)
  1599. // {
  1600. // // 绘制激活的item
  1601. // if (!item.MouseOperating)
  1602. // {
  1603. // continue;
  1604. // }
  1605. // this.DrawItem(graphics, item);
  1606. // }
  1607. //}
  1608. /// <summary>
  1609. /// 绘制图片
  1610. /// </summary>
  1611. /// <param name="graphics"></param>
  1612. /// <param name="image"></param>
  1613. /// <param name="rect"></param>
  1614. private void DrawImage(Graphics graphics, Image image, Rectangle rect)
  1615. {
  1616. graphics.DrawImage(image, rect);
  1617. }
  1618. /// <summary>
  1619. /// 绘制Item
  1620. /// </summary>
  1621. /// <param name="graphics">Graphics</param>
  1622. /// <param name="item">item</param>
  1623. private void DrawItem(Graphics graphics, FlowItem item)
  1624. {
  1625. // 不显示无效节点 2024-11-27 by chenxy
  1626. if (!item.IsShow)
  1627. {
  1628. return;
  1629. }
  1630. if (item is FlowNode)
  1631. {
  1632. FlowNode node = item as FlowNode;
  1633. this.DrawNode(graphics, node);
  1634. return;
  1635. }
  1636. if (item is FlowLine)
  1637. {
  1638. FlowLine line = item as FlowLine;
  1639. this.DrawLine(graphics, line);
  1640. return;
  1641. }
  1642. }
  1643. /// <summary>
  1644. /// 绘制选中状态Item的附属
  1645. /// </summary>
  1646. /// <param name="graphics">Graphics</param>
  1647. /// <param name="item">item</param>
  1648. private void DrawSelectedItem(Graphics graphics, FlowItem item)
  1649. {
  1650. // 不显示无效节点 2024-11-27 by chenxy
  1651. if (!item.IsShow)
  1652. {
  1653. return;
  1654. }
  1655. if (item is FlowNode)
  1656. {
  1657. FlowNode node = item as FlowNode;
  1658. this.DrawSelectedNode(graphics, node);
  1659. return;
  1660. }
  1661. if (item is FlowLine)
  1662. {
  1663. FlowLine line = item as FlowLine;
  1664. this.DrawSelectedLine(graphics, line);
  1665. return;
  1666. }
  1667. }
  1668. /// <summary>
  1669. /// 绘制节点
  1670. /// </summary>
  1671. /// <param name="graphics">Graphics</param>
  1672. /// <param name="node">节点</param>
  1673. private void DrawNode(Graphics graphics, FlowNode node)
  1674. {
  1675. // 不显示无效节点 2024-11-27 by chenxy
  1676. if (!node.IsShow)
  1677. {
  1678. return;
  1679. }
  1680. // 填充颜色
  1681. if (node.FillColor.HasValue && node.FillColor != Color.Transparent)
  1682. {
  1683. this._penDrawNode.Color = node.FillColor.Value;
  1684. graphics.FillRectangle(this._penDrawNode.Brush, node.Bounds);
  1685. }
  1686. // 画图
  1687. if (node.NodeImage != null)
  1688. {
  1689. this.DrawImage(graphics, node.NodeImage, node.Bounds);
  1690. }
  1691. // 画边框
  1692. if (node.BorderWidth > 0 && node.BorderColor != Color.Transparent)
  1693. {
  1694. this._penDrawNode.Color = node.BorderColor;
  1695. this._penDrawNode.Width = node.BorderWidth;
  1696. graphics.DrawRectangle(this._penDrawNode, node.Bounds);
  1697. }
  1698. // 显示名称
  1699. if (node.ShowName)
  1700. {
  1701. //Point p5 = this.GetRectBottom(node.Bounds);
  1702. //SizeF size = graphics.MeasureString(node.Name, node.Font);
  1703. int mm = 40;
  1704. int mh = 20; // mm / 2;
  1705. int ww = node.Bounds.Width + mm;
  1706. int xx = node.Bounds.X - mh;
  1707. SizeF size = graphics.MeasureString(node.Name, node.Font, ww, _stringFormat);
  1708. this._penDrawString.Color = node.FontColor;
  1709. //graphics.DrawString(node.Name, node.Font, this._penDrawString.Brush, p5.X - size.Width / 2, p5.Y + Consts.NODE_STRING_MARGIN);
  1710. RectangleF ff = new RectangleF(xx, (float)(node.Bounds.Bottom + Consts.NODE_STRING_MARGIN), ww, size.Height);
  1711. string nn = node.ShortName;
  1712. if (string.IsNullOrEmpty(nn))
  1713. {
  1714. nn = node.Name;
  1715. }
  1716. /*
  1717. int ida = nn.IndexOf('-');
  1718. if (ida > 0)
  1719. {
  1720. nn = nn.Substring(ida + 1);
  1721. ida = nn.IndexOf('-');
  1722. if (ida > 0)
  1723. {
  1724. nn = nn.Substring(ida + 1);
  1725. }
  1726. }
  1727. */
  1728. graphics.DrawString(nn, node.Font, this._penDrawString.Brush, ff, _stringFormat);
  1729. }
  1730. // this.DrawSelectedNode(graphics, node);
  1731. }
  1732. /// <summary>
  1733. /// 绘制选中节点的附属
  1734. /// </summary>
  1735. /// <param name="graphics">Graphics</param>
  1736. /// <param name="node">节点</param>
  1737. private void DrawSelectedNode(Graphics graphics, FlowNode node)
  1738. {
  1739. // 不显示无效节点 2024-11-27 by chenxy
  1740. if (!node.IsShow)
  1741. {
  1742. return;
  1743. }
  1744. Rectangle[] rects = node.GetSelectedStyle();
  1745. // 画激活状态
  1746. if (node.MouseOperating)
  1747. {
  1748. for (int i = 0; i < rects.Length; i++)
  1749. {
  1750. bool isAnchor = false;
  1751. switch ((NodePointKind)i)
  1752. {
  1753. case NodePointKind.Top:
  1754. if (node.TopLines != null && node.TopLines.Count > 0)
  1755. {
  1756. isAnchor = true;
  1757. }
  1758. break;
  1759. case NodePointKind.Bottom:
  1760. if (node.BottomLines != null && node.BottomLines.Count > 0)
  1761. {
  1762. isAnchor = true;
  1763. }
  1764. break;
  1765. case NodePointKind.Left:
  1766. if (node.LeftLines != null && node.LeftLines.Count > 0)
  1767. {
  1768. isAnchor = true;
  1769. }
  1770. break;
  1771. case NodePointKind.Right:
  1772. if (node.RightLines != null && node.RightLines.Count > 0)
  1773. {
  1774. isAnchor = true;
  1775. }
  1776. break;
  1777. default:
  1778. break;
  1779. }
  1780. this.DrawSelected(graphics, rects[i], isAnchor, true);
  1781. }
  1782. }
  1783. // 画选择状态
  1784. else if (node.Selected)
  1785. {
  1786. for (int i = 0; i < rects.Length; i++)
  1787. {
  1788. bool isAnchor = false;
  1789. switch ((NodePointKind)i)
  1790. {
  1791. case NodePointKind.Top:
  1792. if (node.TopLines != null && node.TopLines.Count > 0)
  1793. {
  1794. isAnchor = true;
  1795. }
  1796. break;
  1797. case NodePointKind.Bottom:
  1798. if (node.BottomLines != null && node.BottomLines.Count > 0)
  1799. {
  1800. isAnchor = true;
  1801. }
  1802. break;
  1803. case NodePointKind.Left:
  1804. if (node.LeftLines != null && node.LeftLines.Count > 0)
  1805. {
  1806. isAnchor = true;
  1807. }
  1808. break;
  1809. case NodePointKind.Right:
  1810. if (node.RightLines != null && node.RightLines.Count > 0)
  1811. {
  1812. isAnchor = true;
  1813. }
  1814. break;
  1815. default:
  1816. break;
  1817. }
  1818. this.DrawSelected(graphics, rects[i], isAnchor, false);
  1819. }
  1820. }
  1821. if (node.ShowAnchor)
  1822. {
  1823. Rectangle[] anchorRects = node.GetAnchorRects();
  1824. foreach (Rectangle rect in anchorRects)
  1825. {
  1826. this.DrawSelected(graphics, rect, true, true);
  1827. }
  1828. }
  1829. }
  1830. /// <summary>
  1831. /// 绘制线段
  1832. /// </summary>
  1833. /// <param name="graphics">Graphics</param>
  1834. /// <param name="line">线段</param>
  1835. private void DrawLine(Graphics graphics, FlowLine line)
  1836. {
  1837. // 不显示无效节点 2024-11-27 by chenxy
  1838. if (!line.IsShow)
  1839. {
  1840. return;
  1841. }
  1842. this._penDrawLine.Color = line.LineColor;
  1843. this._penDrawLine.Width = line.LineWidth;
  1844. // 画线段
  1845. graphics.DrawLine(this._penDrawLine, line.PointBegin, line.PointEnd);
  1846. // 显示名称
  1847. if (line.ShowName)
  1848. {
  1849. Point mp = this.GetLineMidpoint(line.PointBegin, line.PointEnd);
  1850. SizeF size = graphics.MeasureString(line.Name, line.Font);
  1851. this._penDrawString.Color = line.FontColor;
  1852. graphics.DrawString(line.Name, line.Font, this._penDrawString.Brush, mp.X - size.Width / 2, mp.Y - size.Height / 2);
  1853. }
  1854. // this.DrawSelectedLine(graphics, node);
  1855. }
  1856. /// <summary>
  1857. /// 绘制选中线段的附属
  1858. /// </summary>
  1859. /// <param name="graphics">Graphics</param>
  1860. /// <param name="line">线段</param>
  1861. private void DrawSelectedLine(Graphics graphics, FlowLine line)
  1862. {
  1863. // 不显示无效节点 2024-11-27 by chenxy
  1864. if (!line.IsShow)
  1865. {
  1866. return;
  1867. }
  1868. Rectangle[] rects = line.GetSelectedStyle();
  1869. // 画激活状态
  1870. if (line.MouseOperating)
  1871. {
  1872. if (line.NodeBegin != null)
  1873. {
  1874. if (!line.NodeBegin.Selected)
  1875. {
  1876. this.DrawSelected(graphics, rects[0], true, true);
  1877. }
  1878. }
  1879. else
  1880. {
  1881. this.DrawSelected(graphics, rects[0], false, true);
  1882. }
  1883. if (line.NodeEnd != null)
  1884. {
  1885. if (!line.NodeEnd.Selected)
  1886. {
  1887. this.DrawSelected(graphics, rects[1], true, true);
  1888. }
  1889. }
  1890. else
  1891. {
  1892. this.DrawSelected(graphics, rects[1], false, true);
  1893. }
  1894. }
  1895. // 画选择状态
  1896. else if (line.Selected)
  1897. {
  1898. if (line.NodeBegin != null)
  1899. {
  1900. if (!line.NodeBegin.Selected)
  1901. {
  1902. this.DrawSelected(graphics, rects[0], true, false);
  1903. }
  1904. }
  1905. else
  1906. {
  1907. this.DrawSelected(graphics, rects[0], false, false);
  1908. }
  1909. if (line.NodeEnd != null)
  1910. {
  1911. if (!line.NodeEnd.Selected)
  1912. {
  1913. this.DrawSelected(graphics, rects[1], true, false);
  1914. }
  1915. }
  1916. else
  1917. {
  1918. this.DrawSelected(graphics, rects[1], false, false);
  1919. }
  1920. }
  1921. }
  1922. /// <summary>
  1923. /// 绘制折线
  1924. /// </summary>
  1925. /// <param name="graphics"></param>
  1926. /// <param name="line"></param>
  1927. private void DrawPolyline(Graphics graphics, FlowPolyline line)
  1928. {
  1929. }
  1930. /// <summary>
  1931. /// 绘制选择Item的附属
  1932. /// </summary>
  1933. /// <param name="graphics"></param>
  1934. /// <param name="rect"></param>
  1935. /// <param name="isAnchor"></param>
  1936. /// <param name="operating"></param>
  1937. private void DrawSelected(Graphics graphics, Rectangle rect, bool isAnchor, bool operating)
  1938. {
  1939. if (operating)
  1940. {
  1941. if (isAnchor)
  1942. {
  1943. graphics.FillEllipse(Consts.SELECT_POINT_BRUSH_LINE_TO_NODE, rect);
  1944. graphics.DrawEllipse(Consts.SELECT_POINT_PEN, rect);
  1945. }
  1946. else
  1947. {
  1948. graphics.FillEllipse(Consts.SELECT_POINT_BRUSH, rect);
  1949. graphics.DrawEllipse(Consts.SELECT_POINT_PEN, rect);
  1950. }
  1951. }
  1952. else
  1953. {
  1954. if (isAnchor)
  1955. {
  1956. graphics.FillRectangle(Consts.SELECT_POINT_BRUSH_LINE_TO_NODE, rect);
  1957. graphics.DrawRectangle(Consts.SELECT_POINT_PEN, rect);
  1958. }
  1959. else
  1960. {
  1961. graphics.FillRectangle(Consts.SELECT_POINT_BRUSH, rect);
  1962. graphics.DrawRectangle(Consts.SELECT_POINT_PEN, rect);
  1963. }
  1964. }
  1965. }
  1966. #endregion 绘制项目
  1967. #region 鼠标样式
  1968. /// <summary>
  1969. /// 设置鼠标样式
  1970. /// </summary>
  1971. /// <param name="type"></param>
  1972. private void SetCursor(MouseOperatingType type)
  1973. {
  1974. this._mouseOperatingType = type;
  1975. switch (type)
  1976. {
  1977. case MouseOperatingType.None:
  1978. this.CanvasCursor = Cursors.Default;
  1979. break;
  1980. case MouseOperatingType.RectSelect:
  1981. this.CanvasCursor = Cursors.Cross;
  1982. break;
  1983. case MouseOperatingType.Move:
  1984. this.CanvasCursor = Cursors.SizeAll;
  1985. break;
  1986. case MouseOperatingType.NodeTopAndLeft:
  1987. case MouseOperatingType.NodeBottomAndRight:
  1988. this.CanvasCursor = Cursors.SizeNWSE;
  1989. break;
  1990. case MouseOperatingType.NodeTopAndRight:
  1991. case MouseOperatingType.NodeBottomAndLeft:
  1992. this.CanvasCursor = Cursors.SizeNESW;
  1993. break;
  1994. case MouseOperatingType.NodeTop:
  1995. case MouseOperatingType.NodeBottom:
  1996. this.CanvasCursor = Cursors.SizeNS;
  1997. break;
  1998. case MouseOperatingType.NodeLeft:
  1999. case MouseOperatingType.NodeRight:
  2000. this.CanvasCursor = Cursors.SizeWE;
  2001. break;
  2002. case MouseOperatingType.LineBegin:
  2003. case MouseOperatingType.LineEnd:
  2004. this.CanvasCursor = Cursors.Hand;
  2005. break;
  2006. case MouseOperatingType.DrawingNode:
  2007. this.CanvasCursor = ItemCursor.NodeCursor;
  2008. break;
  2009. case MouseOperatingType.DrawingLine:
  2010. this.CanvasCursor = ItemCursor.LineCursor;
  2011. break;
  2012. default:
  2013. this.CanvasCursor = Cursors.Default;
  2014. break;
  2015. }
  2016. }
  2017. /// <summary>
  2018. /// 设置手动画节点时鼠标样式
  2019. /// </summary>
  2020. private void SetDrawingNodeCursor()
  2021. {
  2022. this.SetCursor(MouseOperatingType.DrawingNode);
  2023. }
  2024. /// <summary>
  2025. /// 设置手动画节点时鼠标样式
  2026. /// </summary>
  2027. private void SetDrawingLineCursor()
  2028. {
  2029. this.SetCursor(MouseOperatingType.DrawingLine);
  2030. }
  2031. /// <summary>
  2032. /// 设置框选时鼠标样式
  2033. /// </summary>
  2034. private void SetRectSelectCursor()
  2035. {
  2036. this.SetCursor(MouseOperatingType.RectSelect);
  2037. }
  2038. /// <summary>
  2039. /// 设置默认鼠标样式
  2040. /// </summary>
  2041. private void SetDefaultCursor()
  2042. {
  2043. this.SetCursor(MouseOperatingType.None);
  2044. }
  2045. /// <summary>
  2046. /// 设置移动时鼠标样式
  2047. /// </summary>
  2048. private void SetMoveCursor()
  2049. {
  2050. this.SetCursor(MouseOperatingType.Move);
  2051. }
  2052. /// <summary>
  2053. /// 设置改变节点大小时鼠标样式
  2054. /// </summary>
  2055. /// <param name="kind">节点端点</param>
  2056. private void SetNodeResizeCursor(NodePointKind kind)
  2057. {
  2058. MouseOperatingType type = MouseOperatingType.None;
  2059. switch (kind)
  2060. {
  2061. case NodePointKind.TopAndLeft:
  2062. type = MouseOperatingType.NodeTopAndLeft;
  2063. break;
  2064. case NodePointKind.TopAndRight:
  2065. type = MouseOperatingType.NodeTopAndRight;
  2066. break;
  2067. case NodePointKind.BottomAndLeft:
  2068. type = MouseOperatingType.NodeBottomAndLeft;
  2069. break;
  2070. case NodePointKind.BottomAndRight:
  2071. type = MouseOperatingType.NodeBottomAndRight;
  2072. break;
  2073. case NodePointKind.Top:
  2074. type = MouseOperatingType.NodeTop;
  2075. break;
  2076. case NodePointKind.Bottom:
  2077. type = MouseOperatingType.NodeBottom;
  2078. break;
  2079. case NodePointKind.Left:
  2080. type = MouseOperatingType.NodeLeft;
  2081. break;
  2082. case NodePointKind.Right:
  2083. type = MouseOperatingType.NodeRight;
  2084. break;
  2085. default:
  2086. type = MouseOperatingType.None;
  2087. break;
  2088. }
  2089. this.SetCursor(type);
  2090. }
  2091. /// <summary>
  2092. /// 设置改变线段端点时鼠标样式
  2093. /// </summary>
  2094. /// <param name="kind">线段端点</param>
  2095. private void SetLineResizeCursor(LinePointKind kind)
  2096. {
  2097. MouseOperatingType type = MouseOperatingType.None;
  2098. switch (kind)
  2099. {
  2100. case LinePointKind.Begin:
  2101. type = MouseOperatingType.LineBegin;
  2102. break;
  2103. case LinePointKind.End:
  2104. type = MouseOperatingType.LineEnd;
  2105. break;
  2106. default:
  2107. type = MouseOperatingType.None;
  2108. break;
  2109. }
  2110. this.SetCursor(type);
  2111. }
  2112. /// <summary>
  2113. /// 鼠标移动时,根据鼠标位置设置鼠标样式
  2114. /// </summary>
  2115. /// <param name="point">鼠标位置</param>
  2116. private void SetMouseCursorByMove(Point point)
  2117. {
  2118. this._nodeSizeChanging = null;
  2119. this._linePointChanging = null;
  2120. // item附属判断
  2121. for (int i = this._items.Count - 1; i >= 0; i--)
  2122. {
  2123. FlowItem item = this._items[i];
  2124. if (!item.Selected)
  2125. {
  2126. continue;
  2127. }
  2128. Rectangle[] rects = item.GetSelectedStyle();
  2129. if (item is FlowNode)
  2130. {
  2131. FlowNode node = item as FlowNode;
  2132. for (int index = 0; index < rects.Length; index++)
  2133. {
  2134. if (rects[index].Contains(point))
  2135. {
  2136. NodePointKind kind = (NodePointKind)index;
  2137. // 优先操作连接节点的线段
  2138. switch (kind)
  2139. {
  2140. case NodePointKind.Top:
  2141. if (!this.CanOperateNode(node.TopLines))
  2142. {
  2143. continue;
  2144. }
  2145. break;
  2146. case NodePointKind.Bottom:
  2147. if (!this.CanOperateNode(node.BottomLines))
  2148. {
  2149. continue;
  2150. }
  2151. break;
  2152. case NodePointKind.Left:
  2153. if (!this.CanOperateNode(node.LeftLines))
  2154. {
  2155. continue;
  2156. }
  2157. break;
  2158. case NodePointKind.Right:
  2159. if (!this.CanOperateNode(node.RightLines))
  2160. {
  2161. continue;
  2162. }
  2163. break;
  2164. default:
  2165. break;
  2166. }
  2167. this.SetNodeResizeCursor(kind);
  2168. this._nodeSizeChanging = node;
  2169. return;
  2170. }
  2171. }
  2172. }
  2173. if (this._flowBoxMode != FlowBoxMode.Edit)
  2174. {
  2175. continue;
  2176. }
  2177. if (item is FlowLine)
  2178. {
  2179. for (int index = 0; index < rects.Length; index++)
  2180. {
  2181. if (rects[index].Contains(point))
  2182. {
  2183. LinePointKind kind = (LinePointKind)index;
  2184. this.SetLineResizeCursor(kind);
  2185. this._linePointChanging = item as FlowLine;
  2186. return;
  2187. }
  2188. }
  2189. }
  2190. }
  2191. // item内部判断
  2192. for (int i = this._items.Count - 1; i >= 0; i--)
  2193. {
  2194. FlowItem item = this._items[i];
  2195. if (!item.Selected)
  2196. {
  2197. continue;
  2198. }
  2199. if (item is FlowNode)
  2200. {
  2201. if (this.IsPointOnNode(point, item as FlowNode))
  2202. {
  2203. this.SetMoveCursor();
  2204. return;
  2205. }
  2206. }
  2207. if (this._flowBoxMode != FlowBoxMode.Edit)
  2208. {
  2209. continue;
  2210. }
  2211. if (item is FlowLine)
  2212. {
  2213. if (this.IsPointOnLine(point, item as FlowLine))
  2214. {
  2215. this.SetMoveCursor();
  2216. return;
  2217. }
  2218. }
  2219. }
  2220. this._nodeSizeChanging = null;
  2221. this._linePointChanging = null;
  2222. this.SetDefaultCursor();
  2223. }
  2224. #endregion 鼠标样式
  2225. #region 流程控制
  2226. /// <summary>
  2227. /// 连接PointBegin到节点
  2228. /// </summary>
  2229. /// <param name="line">线段</param>
  2230. /// <param name="node">节点</param>
  2231. /// <param name="anchorKind">锚点</param>
  2232. private void ConnectL2NPointBegin(FlowLine line, FlowNode node, AnchorKind anchorKind)
  2233. {
  2234. // 不显示无效节点 2024-11-27 by chenxy
  2235. if (!node.IsShow)
  2236. {
  2237. return;
  2238. }
  2239. line.AnchorBegin = anchorKind;
  2240. // 连接节点
  2241. line.PointBegin = node.GetAnchor(anchorKind);
  2242. line.NodeBegin = node;
  2243. node.OutLines.Add(line);
  2244. switch (anchorKind)
  2245. {
  2246. case AnchorKind.Top:
  2247. node.TopLines.Add(line);
  2248. break;
  2249. case AnchorKind.Bottom:
  2250. node.BottomLines.Add(line);
  2251. break;
  2252. case AnchorKind.Left:
  2253. node.LeftLines.Add(line);
  2254. break;
  2255. case AnchorKind.Right:
  2256. node.RightLines.Add(line);
  2257. break;
  2258. default:
  2259. break;
  2260. }
  2261. }
  2262. /// <summary>
  2263. /// 连接PointEnd到节点
  2264. /// </summary>
  2265. /// <param name="line">线段</param>
  2266. /// <param name="node">节点</param>
  2267. /// <param name="anchorKind">锚点</param>
  2268. private void ConnectL2NPointEnd(FlowLine line, FlowNode node, AnchorKind anchorKind)
  2269. {
  2270. // 不显示无效节点 2024-11-27 by chenxy
  2271. if (!node.IsShow)
  2272. {
  2273. return;
  2274. }
  2275. line.AnchorEnd = anchorKind;
  2276. // 连接节点
  2277. line.PointEnd = node.GetAnchor(anchorKind);
  2278. line.NodeEnd = node;
  2279. node.InLines.Add(line);
  2280. switch (anchorKind)
  2281. {
  2282. case AnchorKind.Top:
  2283. node.TopLines.Add(line);
  2284. break;
  2285. case AnchorKind.Bottom:
  2286. node.BottomLines.Add(line);
  2287. break;
  2288. case AnchorKind.Left:
  2289. node.LeftLines.Add(line);
  2290. break;
  2291. case AnchorKind.Right:
  2292. node.RightLines.Add(line);
  2293. break;
  2294. default:
  2295. break;
  2296. }
  2297. }
  2298. /// <summary>
  2299. /// 断开PointBegin的连接
  2300. /// </summary>
  2301. /// <param name="line">线段</param>
  2302. private void DisconnectL2NPointBegin(FlowLine line)
  2303. {
  2304. switch (line.AnchorBegin)
  2305. {
  2306. case AnchorKind.Top:
  2307. line.NodeBegin.TopLines.Remove(line);
  2308. break;
  2309. case AnchorKind.Bottom:
  2310. line.NodeBegin.BottomLines.Remove(line);
  2311. break;
  2312. case AnchorKind.Left:
  2313. line.NodeBegin.LeftLines.Remove(line);
  2314. break;
  2315. case AnchorKind.Right:
  2316. line.NodeBegin.RightLines.Remove(line);
  2317. break;
  2318. default:
  2319. break;
  2320. }
  2321. line.NodeBegin.OutLines.Remove(line);
  2322. line.NodeBegin = null;
  2323. line.AnchorBegin = AnchorKind.None;
  2324. }
  2325. /// <summary>
  2326. /// 断开PointEnd的连接
  2327. /// </summary>
  2328. /// <param name="line">线段</param>
  2329. private void DisconnectL2NPointEnd(FlowLine line)
  2330. {
  2331. switch (line.AnchorEnd)
  2332. {
  2333. case AnchorKind.Top:
  2334. line.NodeEnd.TopLines.Remove(line);
  2335. break;
  2336. case AnchorKind.Bottom:
  2337. line.NodeEnd.BottomLines.Remove(line);
  2338. break;
  2339. case AnchorKind.Left:
  2340. line.NodeEnd.LeftLines.Remove(line);
  2341. break;
  2342. case AnchorKind.Right:
  2343. line.NodeEnd.RightLines.Remove(line);
  2344. break;
  2345. default:
  2346. break;
  2347. }
  2348. line.NodeEnd.InLines.Remove(line);
  2349. line.NodeEnd = null;
  2350. line.AnchorEnd = AnchorKind.None;
  2351. }
  2352. /// <summary>
  2353. /// 获得可连接的节点
  2354. /// </summary>
  2355. /// <param name="point">鼠标位置</param>
  2356. /// <returns>可连接的节点</returns>
  2357. private FlowNode GetL2N(Point point)
  2358. {
  2359. FlowNode inNode = null;
  2360. FlowNode edgeNode = null;
  2361. for (int i = this._items.Count - 1; i >= 0; i--)
  2362. {
  2363. FlowNode node = this._items[i] as FlowNode;
  2364. // 不显示无效节点 2024-11-27 by chenxy
  2365. if (node == null)
  2366. {
  2367. continue;
  2368. }
  2369. if (!node.IsShow)
  2370. {
  2371. continue;
  2372. }
  2373. node.ShowAnchor = false;
  2374. // 在节点范围内
  2375. if (node.Bounds.Contains(point))
  2376. {
  2377. inNode = node;
  2378. }
  2379. // 在节点边缘
  2380. Rectangle rect = node.Bounds;
  2381. rect.Inflate(Consts.L2N_PRECISION_EDGE, Consts.L2N_PRECISION_EDGE);
  2382. if (rect.Contains(point))
  2383. {
  2384. edgeNode = node;
  2385. }
  2386. }
  2387. if (inNode != null)
  2388. {
  2389. inNode.ShowAnchor = true;
  2390. return inNode;
  2391. }
  2392. if (edgeNode != null)
  2393. {
  2394. edgeNode.ShowAnchor = true;
  2395. return edgeNode;
  2396. }
  2397. return null;
  2398. }
  2399. #endregion 流程控制
  2400. #region 鼠标操作
  2401. /// <summary>
  2402. /// 框选Item
  2403. /// </summary>
  2404. /// <param name="rect">框选范围</param>
  2405. private void SelectItemByRect(Rectangle rect)
  2406. {
  2407. this.ClearSelection();
  2408. foreach (FlowItem item in this._items)
  2409. {
  2410. // 不显示无效节点 2024-11-27 by chenxy
  2411. if (!item.IsShow)
  2412. {
  2413. continue;
  2414. }
  2415. if (item is FlowNode)
  2416. {
  2417. FlowNode node = item as FlowNode;
  2418. if (rect.IntersectsWith(node.Bounds))
  2419. {
  2420. node.Selected = true;
  2421. this._selectedItems.Add(node);
  2422. }
  2423. else
  2424. {
  2425. node.Selected = false;
  2426. }
  2427. continue;
  2428. }
  2429. if (this._flowBoxMode != FlowBoxMode.Edit)
  2430. {
  2431. continue;
  2432. }
  2433. if (item is FlowLine)
  2434. {
  2435. FlowLine line = item as FlowLine;
  2436. if (rect.Contains(line.PointBegin) || rect.Contains(line.PointEnd))
  2437. {
  2438. line.Selected = true;
  2439. this._selectedItems.Add(line);
  2440. }
  2441. else
  2442. {
  2443. line.Selected = false;
  2444. }
  2445. continue;
  2446. }
  2447. }
  2448. }
  2449. /// <summary>
  2450. /// 点选Item
  2451. /// </summary>
  2452. /// <param name="point">鼠标点</param>
  2453. /// <returns>选中的item</returns>
  2454. private FlowItem SelectItemByPoint(Point point)
  2455. {
  2456. for (int i = this._items.Count - 1; i >= 0; i--)
  2457. {
  2458. FlowItem item = this._items[i];
  2459. // 不显示无效节点 2024-11-27 by chenxy
  2460. if (!item.IsShow)
  2461. {
  2462. continue;
  2463. }
  2464. if (item is FlowNode)
  2465. {
  2466. if (this.IsPointOnNode(point, item as FlowNode))
  2467. {
  2468. return item;
  2469. }
  2470. }
  2471. if (this._flowBoxMode != FlowBoxMode.Edit)
  2472. {
  2473. continue;
  2474. }
  2475. if (item is FlowLine)
  2476. {
  2477. if (this.IsPointOnLine(point, item as FlowLine))
  2478. {
  2479. return item;
  2480. }
  2481. }
  2482. //if (item is FlowPolyline)
  2483. //{
  2484. // if (this.IsPointOnPolyLine(point, item as FlowPolyline))
  2485. // {
  2486. // return item;
  2487. // }
  2488. //}
  2489. }
  2490. return null;
  2491. }
  2492. /// <summary>
  2493. /// 点选节点
  2494. /// </summary>
  2495. /// <param name="point">鼠标点</param>
  2496. /// <returns>选中的节点</returns>
  2497. private FlowNode SelectNodeByPoint(Point point)
  2498. {
  2499. for (int i = this._items.Count - 1; i >= 0; i--)
  2500. {
  2501. FlowItem item = this._items[i];
  2502. if (item is FlowNode)
  2503. {
  2504. FlowNode node = item as FlowNode;
  2505. // 不显示无效节点 2024-11-27 by chenxy
  2506. if (!item.IsShow)
  2507. {
  2508. continue;
  2509. }
  2510. if (this.IsPointOnNode(point, node))
  2511. {
  2512. return node;
  2513. }
  2514. }
  2515. }
  2516. return null;
  2517. }
  2518. /// <summary>
  2519. /// 能否操作节点(鼠标在锚点)
  2520. /// </summary>
  2521. /// <param name="lines">连接的线段</param>
  2522. /// <returns>能否操作节点</returns>
  2523. private bool CanOperateNode(List<FlowLine> lines)
  2524. {
  2525. if (lines != null && lines.Count > 0)
  2526. {
  2527. foreach (FlowLine line in lines)
  2528. {
  2529. if (line.Selected)
  2530. {
  2531. return false;
  2532. }
  2533. }
  2534. }
  2535. return true;
  2536. }
  2537. #endregion 鼠标操作
  2538. #region 工具
  2539. /// <summary>
  2540. /// 判断线段是否相交
  2541. /// </summary>
  2542. /// <param name="p1">线段1的开始点</param>
  2543. /// <param name="p2">线段1的结束点</param>
  2544. /// <param name="p3">线段2的开始点</param>
  2545. /// <param name="p4">线段2的结束点</param>
  2546. /// <returns>是否相交</returns>
  2547. private bool IsSegmentIntersect(Point p1, Point p2, Point p3, Point p4)
  2548. {
  2549. double d1 = this.GetDirection(p3, p4, p1);
  2550. double d2 = this.GetDirection(p3, p4, p2);
  2551. double d3 = this.GetDirection(p1, p2, p3);
  2552. double d4 = this.GetDirection(p1, p2, p4);
  2553. if (d1 * d2 < 0 && d3 * d4 < 0)
  2554. {
  2555. return true;
  2556. }
  2557. else if (d1 == 0 && this.IsOnSegment(p3, p4, p1))
  2558. {
  2559. return true;
  2560. }
  2561. else if (d2 == 0 && this.IsOnSegment(p3, p4, p2))
  2562. {
  2563. return true;
  2564. }
  2565. else if (d3 == 0 && this.IsOnSegment(p1, p2, p3))
  2566. {
  2567. return true;
  2568. }
  2569. else if (d4 == 0 && this.IsOnSegment(p1, p2, p4))
  2570. {
  2571. return true;
  2572. }
  2573. else
  2574. {
  2575. return false;
  2576. }
  2577. }
  2578. /// <summary>
  2579. /// 获取点1到点2和点3的向量的叉乘
  2580. /// </summary>
  2581. /// <param name="p1">点1</param>
  2582. /// <param name="p2">点2</param>
  2583. /// <param name="p3">点3</param>
  2584. /// <returns></returns>
  2585. private int GetDirection(Point p1, Point p2, Point p3)
  2586. {
  2587. Point v1 = new Point(p3.X - p1.X, p3.Y - p1.Y);
  2588. Point v2 = new Point(p2.X - p1.X, p2.Y - p1.Y);
  2589. return v1.X * v2.Y - v1.Y * v2.X;
  2590. }
  2591. /// <summary>
  2592. /// 点3在线上时,判断点3在线段上,还是在线段的延长线上
  2593. /// </summary>
  2594. /// <param name="p1">线段的开始点</param>
  2595. /// <param name="p2">线段的结束点</param>
  2596. /// <param name="p3">点3</param>
  2597. /// <returns></returns>
  2598. private bool IsOnSegment(Point p1, Point p2, Point p3)
  2599. {
  2600. int x_min, x_max, y_min, y_max;
  2601. if (p1.X < p2.X)
  2602. {
  2603. x_min = p1.X;
  2604. x_max = p2.X;
  2605. }
  2606. else
  2607. {
  2608. x_min = p2.X;
  2609. x_max = p1.X;
  2610. }
  2611. if (p1.Y < p2.Y)
  2612. {
  2613. y_min = p1.Y;
  2614. y_max = p2.Y;
  2615. }
  2616. else
  2617. {
  2618. y_min = p2.Y;
  2619. y_max = p1.Y;
  2620. }
  2621. if (p3.X < x_min || p3.X > x_max || p3.Y < y_min || p3.Y > y_max)
  2622. {
  2623. return false;
  2624. }
  2625. else
  2626. {
  2627. return true;
  2628. }
  2629. }
  2630. /// <summary>
  2631. /// 点是否在节点上
  2632. /// </summary>
  2633. /// <param name="point">点</param>
  2634. /// <param name="node">节点</param>
  2635. /// <returns>是否在节点上</returns>
  2636. private bool IsPointOnNode(Point point, FlowNode node)
  2637. {
  2638. // 不显示无效节点 2024-11-27 by chenxy
  2639. if (!node.IsShow)
  2640. {
  2641. return false;
  2642. }
  2643. return node.Bounds.Contains(point);
  2644. }
  2645. /// <summary>
  2646. /// 点是否在线段上
  2647. /// </summary>
  2648. /// <param name="point">点</param>
  2649. /// <param name="line">线段</param>
  2650. /// <returns>否在线段上</returns>
  2651. private bool IsPointOnLine(Point point, FlowLine line)
  2652. {
  2653. // 不显示无效节点 2024-11-27 by chenxy
  2654. if (!line.IsShow)
  2655. {
  2656. return false;
  2657. }
  2658. double p2ls = this.GetP2LSDistance(point, line.PointBegin, line.PointEnd);
  2659. //if (p2ls <= (line.LineWidth - 1) / 2 + Consts.P2L_PRECISION)
  2660. if (p2ls <= (line.LineWidth) / 2 + Consts.P2L_PRECISION)
  2661. {
  2662. return true;
  2663. }
  2664. return false;
  2665. }
  2666. /// <summary>
  2667. /// 获取点到线段的距离
  2668. /// </summary>
  2669. /// <param name="p">点</param>
  2670. /// <param name="pBegin">线段的端点</param>
  2671. /// <param name="pEnd">线段的端点</param>
  2672. /// <returns>点到线段的距离</returns>
  2673. private double GetP2LSDistance(Point p, Point pBegin, Point pEnd)
  2674. {
  2675. // 点在线段端点
  2676. double pp1 = this.GetP2PDistance(p, pBegin);
  2677. if (pp1 < Consts.DOUBLE_PRECISION)
  2678. {
  2679. return pp1;
  2680. }
  2681. double pp2 = this.GetP2PDistance(p, pEnd);
  2682. if (pp2 < Consts.DOUBLE_PRECISION)
  2683. {
  2684. return pp2;
  2685. }
  2686. double p1p2 = this.GetP2PDistance(pBegin, pEnd);
  2687. // 线段端点相同
  2688. if (p1p2 < Consts.DOUBLE_PRECISION)
  2689. {
  2690. return pp1;
  2691. }
  2692. // 点在线段端点外
  2693. double pp12 = pp1 * pp1;
  2694. double pp22 = pp2 * pp2;
  2695. double p1p22 = p1p2 * p1p2;
  2696. if (pp12 >= pp22 + p1p22)
  2697. {
  2698. return pp22;
  2699. }
  2700. if (pp22 >= pp12 + p1p22)
  2701. {
  2702. return pp12;
  2703. }
  2704. // 点在线段端点内
  2705. // 周长的一半
  2706. double l = (pp1 + pp2 + p1p2) / 2;
  2707. // 海伦公式求面积,也可以用矢量求
  2708. double s = Math.Sqrt(l * (l - pp1) * (l - pp2) * (l - p1p2));
  2709. return 2 * s / p1p2;
  2710. }
  2711. /// <summary>
  2712. /// 获取两点间的距离
  2713. /// </summary>
  2714. /// <param name="point1"></param>
  2715. /// <param name="point2"></param>
  2716. /// <returns>两点间的距离</returns>
  2717. private double GetP2PDistance(Point point1, Point point2)
  2718. {
  2719. return Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y));
  2720. }
  2721. /// <summary>
  2722. /// 获取鼠标操作后的新范围
  2723. /// </summary>
  2724. /// <param name="rect">源范围</param>
  2725. /// <param name="fixedPoint">固定点</param>
  2726. /// <param name="newPoint">移动点</param>
  2727. /// <returns>新范围</returns>
  2728. private Rectangle GetNewRect(Rectangle rect, Point fixedPoint, Point newPoint)
  2729. {
  2730. int xRect = newPoint.X - fixedPoint.X;
  2731. int yRect = newPoint.Y - fixedPoint.Y;
  2732. if (xRect < 0)
  2733. {
  2734. rect.X = newPoint.X;
  2735. rect.Width = -xRect;
  2736. }
  2737. else
  2738. {
  2739. rect.X = fixedPoint.X;
  2740. rect.Width = xRect;
  2741. }
  2742. if (yRect < 0)
  2743. {
  2744. rect.Y = newPoint.Y;
  2745. rect.Height = -yRect;
  2746. }
  2747. else
  2748. {
  2749. rect.Y = fixedPoint.Y;
  2750. rect.Height = yRect;
  2751. }
  2752. return rect;
  2753. }
  2754. /// <summary>
  2755. /// 获取鼠标操作后的新范围
  2756. /// </summary>
  2757. /// <param name="rect">源范围</param>
  2758. /// <param name="fixedPoint">固定点</param>
  2759. /// <param name="xRect">x方向移动距离</param>
  2760. /// <param name="yRect">y方向移动距离</param>
  2761. /// <returns>新范围</returns>
  2762. private Rectangle GetNewRect(Rectangle rect, Point fixedPoint, int xRect, int yRect)
  2763. {
  2764. if (xRect < 0)
  2765. {
  2766. rect.X = fixedPoint.X + xRect;
  2767. rect.Width = -xRect;
  2768. }
  2769. else
  2770. {
  2771. rect.X = fixedPoint.X;
  2772. rect.Width = xRect;
  2773. }
  2774. if (yRect < 0)
  2775. {
  2776. rect.Y = fixedPoint.Y + yRect;
  2777. rect.Height = -yRect;
  2778. }
  2779. else
  2780. {
  2781. rect.Y = fixedPoint.Y;
  2782. rect.Height = yRect;
  2783. }
  2784. return rect;
  2785. }
  2786. /// <summary>
  2787. /// 获得矩形的左上角
  2788. /// </summary>
  2789. /// <param name="rect">矩形</param>
  2790. /// <returns>左上角</returns>
  2791. private Point GetRectTopAndLeft(Rectangle rect)
  2792. {
  2793. return rect.Location;
  2794. }
  2795. /// <summary>
  2796. /// 获得矩形的右上角
  2797. /// </summary>
  2798. /// <param name="rect">矩形</param>
  2799. /// <returns>右上角</returns>
  2800. private Point GetRectTopAndRight(Rectangle rect)
  2801. {
  2802. return new Point(rect.Right, rect.Top);
  2803. }
  2804. /// <summary>
  2805. /// 获得矩形的左下角
  2806. /// </summary>
  2807. /// <param name="rect">矩形</param>
  2808. /// <returns>左下角</returns>
  2809. private Point GetRectBottomAndLeft(Rectangle rect)
  2810. {
  2811. return new Point(rect.Left, rect.Bottom);
  2812. }
  2813. /// <summary>
  2814. /// 获得矩形的右下角
  2815. /// </summary>
  2816. /// <param name="rect">矩形</param>
  2817. /// <returns>右下角</returns>
  2818. private Point GetRectBottomAndRight(Rectangle rect)
  2819. {
  2820. return new Point(rect.Right, rect.Bottom);
  2821. }
  2822. /// <summary>
  2823. /// 获得矩形的上中点
  2824. /// </summary>
  2825. /// <param name="rect">矩形</param>
  2826. /// <returns>上中点</returns>
  2827. private Point GetRectTop(Rectangle rect)
  2828. {
  2829. return new Point(rect.Left + rect.Width / 2, rect.Top);
  2830. }
  2831. /// <summary>
  2832. /// 获得矩形的下中点
  2833. /// </summary>
  2834. /// <param name="rect">矩形</param>
  2835. /// <returns>下中点</returns>
  2836. private Point GetRectBottom(Rectangle rect)
  2837. {
  2838. return new Point(rect.Left + rect.Width / 2, rect.Bottom);
  2839. }
  2840. /// <summary>
  2841. /// 获得矩形的左中点
  2842. /// </summary>
  2843. /// <param name="rect">矩形</param>
  2844. /// <returns>左中点</returns>
  2845. private Point GetRectLeft(Rectangle rect)
  2846. {
  2847. return new Point(rect.Left, rect.Top + rect.Height / 2);
  2848. }
  2849. /// <summary>
  2850. /// 获得矩形的右中点
  2851. /// </summary>
  2852. /// <param name="rect">矩形</param>
  2853. /// <returns>右中点</returns>
  2854. private Point GetRectRight(Rectangle rect)
  2855. {
  2856. return new Point(rect.Right, rect.Top + rect.Height / 2);
  2857. }
  2858. /// <summary>
  2859. /// 获得线段的中点
  2860. /// </summary>
  2861. /// <param name="pointBegin">线段开始点</param>
  2862. /// <param name="pointEnd">线段结束点</param>
  2863. /// <returns>中点</returns>
  2864. private Point GetLineMidpoint(Point pointBegin, Point pointEnd)
  2865. {
  2866. return new Point((pointBegin.X + pointEnd.X) / 2, (pointBegin.Y + pointEnd.Y) / 2);
  2867. }
  2868. #endregion 工具
  2869. #endregion 私有方法
  2870. }
  2871. }