GraDrawTool.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. ///
  2. using System;
  3. using System.Collections;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Windows.Forms;
  7. using System.Reflection;
  8. using System.IO;
  9. using GraEditor.Elements;//Elements include Line,Lines,Rectangle,Ellipse,Text,Image
  10. // Namespace DrawTool, include GraDrawTool,DrawEnvMgr,GraDrawToolMgr,LineDrawTool,LinesDrawTool,
  11. //FrameRectDrawTool, FillRectDrawTool, FrameEllipseDrawTool,FillEllipseDrawTool,ImageDrawTool,TextDrawTool,SelectDrawTool,GraDrawType
  12. namespace GraEditor.DrawTools
  13. {
  14. //Save Global pen, brush and font. We use these object when we draw graph element on canvas
  15. public class DrawEnvMgr
  16. {
  17. protected DrawEnvMgr()
  18. {
  19. this.pen = new Pen(Color.Red, 1); //设置默认的画笔
  20. this.brush = new SolidBrush(Color.Red);//设置默认刷
  21. this.font = new Font("宋体", 30); //设置默认字体
  22. }
  23. private static DrawEnvMgr instance;// instance of DrawEnvMgr
  24. //Get instance of DrawEnvMgr
  25. public static DrawEnvMgr Instance
  26. {
  27. get
  28. {
  29. if(instance == null)
  30. instance = new DrawEnvMgr();
  31. return instance;
  32. }
  33. }
  34. protected Pen pen; //protected pen variable to save pen object
  35. //Pen property of DrawEnvMgr
  36. public Pen Pen
  37. {
  38. get { return pen; }
  39. }
  40. protected SolidBrush brush;//protected solidbrush variable to save brush object
  41. //Brush property of DrawEnvMgr
  42. public Brush Brush
  43. {
  44. get { return brush; }
  45. }
  46. //Set pen and brush color
  47. public Color Color
  48. {
  49. get { return pen.Color; }
  50. set
  51. {
  52. pen.Color = value;
  53. brush.Color = value;
  54. }
  55. }
  56. //Pen width property of DrawEnvMgr
  57. public int PenWidth
  58. {
  59. get { return (int)pen.Width; }
  60. set { pen.Width = value; }
  61. }
  62. protected Font font; //private font variable to save font
  63. //Font property of DrawEnvMgr
  64. public Font Font
  65. {
  66. get { return font ; }
  67. set { font = value; }
  68. }
  69. }
  70. //Specify how many draw type is included
  71. public enum GraDrawType
  72. {
  73. Select, Line, Arrow,MobileViews, Lines, Freehand, Erase, FrameRect, FillRect, FrameEllipse, FillEllipse, Text, Image
  74. }
  75. //Abstract class for drawing operation.
  76. //SelectDrawTool(), LineDrawTool(), LinesDrawTool(), ImageDrawTool(), ImageDrawTool(), RectDrawTool(),
  77. //EllipseDrawTool() and TextDrawTool() are inherit from it.
  78. public abstract class GraDrawTool
  79. {
  80. /// <summary>
  81. ///
  82. /// </summary>
  83. ///
  84. public GraDrawTool()
  85. {
  86. }
  87. //Abstract variable.
  88. public abstract GraDrawType Type
  89. {
  90. get;
  91. }
  92. private GraDrawToolMgr drawToolMgr;//Private drawToolMgr variable of GraDrawTool
  93. //DrawTool Mgr property of GraDrawTool
  94. public GraDrawToolMgr DrawToolMgr
  95. {
  96. get { return drawToolMgr; }
  97. set { drawToolMgr = value;}
  98. }
  99. protected bool isDrawing; //When you are drawing,isDrawing is true. Else isDrawing is false
  100. private ToolBarButton tblBtn;//Save the ToolBar button
  101. //Toolbar button property of GraDrawTool
  102. public ToolBarButton TblBtn
  103. {
  104. get { return tblBtn; }
  105. set { tblBtn = value ;}
  106. }
  107. private Cursor oldCursor;
  108. protected Cursor toolCursor;
  109. public static event EventHandler ActiveStateChanged;
  110. public void OnActiveStateChanged()
  111. {
  112. if(ActiveStateChanged != null)
  113. ActiveStateChanged(this,EventArgs.Empty);
  114. }
  115. private bool isActive;//When the ToolBar button is pushdown, isActive is true. Else isActive is false
  116. //Active property of GraDrawTool
  117. public virtual bool IsActive
  118. {
  119. get { return isActive; }
  120. set
  121. {
  122. this.isActive = value;
  123. tblBtn.Pushed = this.isActive;
  124. OnActiveStateChanged();
  125. if(this.toolCursor != null)
  126. {
  127. if(this.isActive)
  128. {
  129. this.oldCursor = Canvas.Instance.MainView.Cursor;
  130. Canvas.Instance.MainView.Cursor = this.toolCursor;
  131. }
  132. else
  133. Canvas.Instance.MainView.Cursor = this.oldCursor;
  134. }
  135. }
  136. }
  137. public virtual string Tip
  138. {
  139. get { return "";}
  140. }
  141. public static int gridSize = 5;//Set the small distance you can draw. If mouse moves smaller distance than gridSize, we think it do not move
  142. public Cursor CreateCursor(byte[] resoures)
  143. {
  144. System.IO.Stream stream = new MemoryStream(resoures);
  145. return new Cursor(stream);
  146. }
  147. //Round calculation of mouse movement.Mouse must move at least 6 pixel every time.
  148. public static Point GetRoundPoint(Point pt)
  149. {
  150. int x = (pt.X+gridSize/2)/gridSize*gridSize;
  151. int y = (pt.Y+gridSize/2)/gridSize*gridSize;
  152. return new Point(x,y);
  153. }
  154. public static Point RegularEndPt(Point ptStart, Point ptEnd)
  155. {
  156. int sx = Math.Abs(ptEnd.X - ptStart.X);
  157. int sy = Math.Abs(ptEnd.Y - ptStart.Y);
  158. int s = Math.Max(sx,sy);
  159. int x =0;
  160. int y =0;
  161. if(ptEnd.X > ptStart.X)
  162. x = ptStart.X + s;
  163. else
  164. x = ptStart.X - s;
  165. if(ptEnd.Y > ptStart.Y)
  166. y = ptStart.Y + s;
  167. else
  168. y = ptStart.Y - s;
  169. return new Point(x,y);
  170. }
  171. public static Rectangle CreateValidRect(Point pt1, Point pt2)
  172. {
  173. int x = Math.Min(pt1.X,pt2.X);//Judge whether the current x position is at the right of the start point or not
  174. int y = Math.Min(pt1.Y,pt2.Y);//Judge whether the current y position is under the start point or no
  175. int w = Math.Abs(pt2.X - pt1.X);//calculate the width of rectangle
  176. int h = Math.Abs(pt2.Y - pt1.Y);//calculate the height of rectangle
  177. return new Rectangle(new Point(x,y),new Size(w,h));
  178. }
  179. public abstract void OnMouseDown(MouseEventArgs e);//abstract OnMouseDown event
  180. public abstract void OnMouseMove(MouseEventArgs e);//abstract OnMouseMove event
  181. public abstract void OnMouseUp(MouseEventArgs e);//abstract OnMouseUp event
  182. public virtual void OnDoubleClick(EventArgs e)//abstract OnDoubleClick event
  183. {
  184. }
  185. public virtual void OnKeyDown(KeyEventArgs e)
  186. {
  187. }
  188. public virtual void OnKeyUp(KeyEventArgs e)
  189. {
  190. }
  191. }
  192. //Manage draw tools, add new draw tool type
  193. public class GraDrawToolMgr
  194. {
  195. protected GraDrawToolMgr()
  196. {
  197. toolMap = new Hashtable(); //Use hash table to reach the draw tool, it is fast than using vector.toolMap(draw tool type)=draw tool.
  198. }
  199. private static GraDrawToolMgr instance;//instance of GraDrawToolMgr
  200. //Return the instance of GraDrawToolMgr
  201. public static GraDrawToolMgr Instance
  202. {
  203. get
  204. {
  205. if(instance == null)
  206. instance = new GraDrawToolMgr();
  207. return instance;
  208. }
  209. }
  210. private Hashtable toolMap; // private hash table to save draw tool types
  211. //Return draw tool
  212. public GraDrawTool this[GraDrawType t]
  213. {
  214. get { return toolMap[t] as GraDrawTool; }
  215. set
  216. {
  217. toolMap[t] = value;
  218. }
  219. }
  220. //Add draw tool to toolMap
  221. public void AddDrawTool(GraDrawTool drawTool)
  222. {
  223. if(drawTool == null)
  224. return;
  225. drawTool.DrawToolMgr = this;
  226. toolMap[drawTool.Type] = drawTool;
  227. }
  228. private GraDrawTool activeDrawTool;//Save the current draw tool
  229. //Active draw tool property of GraDrawToolMgr
  230. public GraDrawTool ActiveDrawTool
  231. {
  232. get { return activeDrawTool; }
  233. }
  234. //Set the current draw tool
  235. public void SetActiveTool(GraDrawType t)
  236. {
  237. GraDrawTool clickDrawTool = this[t];//Get which Toolbar button is clicked
  238. if(this.activeDrawTool == clickDrawTool)//Reclick the same Toolbar button.Disable the last cliked Toolbar button
  239. {
  240. this.activeDrawTool.IsActive = false;
  241. this.activeDrawTool = null;
  242. return;
  243. }
  244. if(this.activeDrawTool != null)//Click a new Toolbar button
  245. this.activeDrawTool.IsActive = false;
  246. this.activeDrawTool = clickDrawTool;//Set the new clicked Toolbar button as current one
  247. this.activeDrawTool.IsActive = true;
  248. }
  249. }
  250. //It is inherited from GraDrawTool(). It is used for selecting actions.
  251. //When we use mouse to select an area, we use this class display some temporary rectangles before we decide the end point of select
  252. public class SelectDrawTool : GraDrawTool
  253. {
  254. protected SelectDrawTool()
  255. :base()
  256. {
  257. this.isSelectRectVisible = false;//Do not display the rectangle frame
  258. }
  259. //Reset the select area
  260. public void ReSet()
  261. {
  262. this.selectRectGra = null;
  263. this.isSelectRectVisible = false;
  264. }
  265. //Specify select type
  266. public override GraDrawType Type
  267. {
  268. get
  269. {
  270. return GraDrawType.Select;
  271. }
  272. }
  273. private static SelectDrawTool instance;//instance of SelectDrawTool
  274. //返回SelectDrawTool实例
  275. public static SelectDrawTool Instance
  276. {
  277. get
  278. {
  279. if(instance == null)
  280. instance = new SelectDrawTool();
  281. return instance;
  282. }
  283. }
  284. //选择area.Size和选择位置
  285. public Rectangle SelectRect
  286. {
  287. get
  288. {
  289. if(selectRectGra == null)
  290. return Rectangle.Empty;
  291. else
  292. return selectRectGra.Rect;
  293. }
  294. }
  295. public event EventHandler StateChanged;
  296. public void OnStateChanged()
  297. {
  298. if(StateChanged != null)
  299. StateChanged(this,EventArgs.Empty);
  300. }
  301. private RectGra selectRectGra;//私人RectGra变量,以便保存选择区域
  302. public RectGra SelectRectGra
  303. {
  304. get { return this.selectRectGra; }
  305. set { this.selectRectGra=value; }
  306. }
  307. private Point ptStart;
  308. private Point ptPre;//Temporary point use for round
  309. private bool isSelectRectVisible;//Frame of select is visible or not
  310. /// <summary>
  311. /// 取消选区
  312. /// </summary>
  313. public void ClerSelected()
  314. {
  315. ClerSelectedRect();
  316. this.selectRectGra = null;
  317. OnStateChanged();
  318. }
  319. /// <summary>
  320. /// 取消所选操作
  321. /// </summary>
  322. private void ClerSelectedRect()
  323. {
  324. if(!this.isSelectRectVisible)
  325. return;
  326. Canvas.Instance.GraList.Remove(this.selectRectGra); //Remove the select object from GraList
  327. this.isSelectRectVisible = false;
  328. Canvas.Instance.Update();//Update the canvas
  329. }
  330. public override void OnMouseDown(MouseEventArgs e)
  331. {
  332. ptStart = new Point(e.X, e.Y);//开始点的选择
  333. ptStart = GetRoundPoint(ptStart);//起点
  334. ptPre = ptStart;//临时使用点
  335. //在现在的位置转换成原始的,当你放大或坐标缩小
  336. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  337. if(e.Button == MouseButtons.Right)//right click mouse button
  338. {
  339. return;
  340. }
  341. this.ClerSelectedRect();//Cancel the selected operation
  342. this.isDrawing = true; //Drawing status
  343. //Construct a pen object to draw the select frame
  344. using(Pen p = new Pen(Color.Black,1))
  345. {
  346. p.DashStyle = DashStyle.Dot;//选择框是虚线
  347. selectRectGra = new RectGra(p,null,
  348. GraDrawMode.Frame, new Rectangle(ptStart, new Size(0, 0)));//使用selectRectGra保存选择区域的当前位置和大小
  349. }
  350. Canvas.Instance.GraList.Add(selectRectGra);//添加选择GraList
  351. this.isSelectRectVisible = true;//设置选择框可见
  352. Canvas.Instance.Update();//更新画布
  353. }
  354. public override void OnMouseMove(MouseEventArgs e)
  355. {
  356. if(!this.isDrawing)
  357. return;
  358. Point ptEnd = new Point(e.X,e.Y);//Save the current position of mouse
  359. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  360. if(ptEnd == ptPre)//Movement distance < 6
  361. return;
  362. ptPre = ptEnd;//Save end point as a temporary point
  363. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);//Get current posion.Convert to orginal coordiante when zoom in or zoom out
  364. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  365. {
  366. ptEnd = RegularEndPt(ptStart,ptEnd);
  367. }
  368. //设置选择区域的位置和大小
  369. selectRectGra.Rect = CreateValidRect(ptStart,ptEnd);
  370. Canvas.Instance.Update();//Update canvas
  371. }
  372. public override void OnMouseUp(MouseEventArgs e)
  373. {
  374. if(this.isDrawing)
  375. {
  376. this.isDrawing = false;//Disable the drawing status
  377. Rectangle r = this.selectRectGra.Rect;
  378. if(r.Width <5 || r.Height <5)
  379. {
  380. ClerSelectedRect();
  381. this.selectRectGra = null;
  382. }
  383. else
  384. OnStateChanged();
  385. }
  386. }
  387. /// <summary>
  388. /// 全选
  389. /// </summary>
  390. public void AllSelectedRect()
  391. {
  392. ptStart = new Point(0, 0);//开始点的选择
  393. ptStart = GetRoundPoint(ptStart);//起点
  394. ptPre = ptStart;//临时使用点
  395. //在现在的位置转换成原始的,当你放大或坐标缩小
  396. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  397. this.ClerSelectedRect();//Cancel the selected operation
  398. this.isDrawing = true; //Drawing status
  399. //Construct a pen object to draw the select frame
  400. using (Pen p = new Pen(Color.Black, 1))
  401. {
  402. p.DashStyle = DashStyle.Dot;//选择框是虚线
  403. selectRectGra = new RectGra(p, null,
  404. GraDrawMode.Frame, new Rectangle(ptStart, new Size(0, 0)));//使用selectRectGra保存选择区域的当前位置和大小
  405. }
  406. Canvas.Instance.GraList.Add(selectRectGra);//添加选择GraList
  407. this.isSelectRectVisible = true;//设置选择框可见
  408. Point ptEnd = new Point(Canvas.Instance.Size.Width-3, Canvas.Instance.Size.Height-3);//Save the current position of mouse
  409. ptEnd = GetRoundPoint(ptEnd);//回合结束点。最小鼠标移动距离为6。
  410. if (ptEnd == ptPre)//Movement distance < 6
  411. return;
  412. ptPre = ptEnd;//Save end point as a temporary point
  413. // ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);//获取当前posion.Convert到原单coordiante时,放大或缩小
  414. if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  415. {
  416. ptEnd = RegularEndPt(ptStart, ptEnd);
  417. }
  418. //设置选择区域的位置和大小
  419. selectRectGra.Rect = CreateValidRect(ptStart, ptEnd);
  420. Canvas.Instance.Update();//更新画布
  421. OnMouseUp(null);
  422. }
  423. private Bitmap copyImage;//Save the select area as a picture
  424. public Bitmap CopyImage
  425. {
  426. get { return this.copyImage; }
  427. }
  428. /// <summary>
  429. /// 删除选择区域的图像
  430. /// </summary>
  431. public void ClearRectGra()
  432. {
  433. if (this.selectRectGra == null) { return; }
  434. Color c = Canvas.Instance.BackColor;//Background color
  435. Brush b = new SolidBrush(c);
  436. Pen p = new Pen(c);
  437. EraseRectGra rGra = new EraseRectGra(p, b, GraDrawMode.Frame | GraDrawMode.Fill,
  438. this.selectRectGra.Rect);//Set the select area with background color
  439. b.Dispose();//Dispose the brush object
  440. p.Dispose();//Dispose the pen object
  441. Canvas.Instance.AddGra(rGra);
  442. this.selectRectGra = null;
  443. OnStateChanged();
  444. }
  445. public void Cut()
  446. {
  447. if(!this.CreateCopyImage())
  448. return;
  449. //
  450. Color c = Canvas.Instance.BackColor;//Background color
  451. Brush b = new SolidBrush(c);
  452. Pen p = new Pen(c);
  453. EraseRectGra rGra = new EraseRectGra(p,b,GraDrawMode.Frame | GraDrawMode.Fill,
  454. this.selectRectGra.Rect);//Set the select area with background color
  455. b.Dispose();//Dispose the brush object
  456. p.Dispose();//Dispose the pen object
  457. Canvas.Instance.AddGra(rGra);
  458. this.selectRectGra = null;
  459. OnStateChanged();
  460. }
  461. private bool CreateCopyImage()
  462. {
  463. if(this.selectRectGra == null)
  464. return false;;
  465. this.ClerSelectedRect();//Cancel the select area
  466. Canvas canvas = Canvas.Instance;//Get instance of canvas
  467. Rectangle destRect = new Rectangle(new Point(0,0),this.selectRectGra.Rect.Size);//Paste the cut area to this rectangle area
  468. Rectangle souRect = this.selectRectGra.Rect;//Save position and size of select area in souRect
  469. souRect = canvas.MainView.ConvertCanvasToViewBitmapRect(souRect);//Convert to orginal coordinate when you zoom in or zoom out
  470. this.copyImage = new Bitmap(destRect.Width,destRect.Height);//construct a image
  471. //construct a graphic object from copyImage
  472. using(Graphics g = Graphics.FromImage(this.copyImage))
  473. {
  474. //Draw the graph on destination area
  475. g.DrawImage(MainView.ViewBitmap,destRect,
  476. souRect.X,souRect.Y,souRect.Width,souRect.Height,GraphicsUnit.Pixel);
  477. }
  478. this.copyImage.MakeTransparent(Canvas.Instance.BackColor);// Make the copyImage transparent
  479. return true;
  480. }
  481. //Copy operation
  482. public void Copy()
  483. {
  484. if(!this.CreateCopyImage())
  485. return ;
  486. this.selectRectGra = null;
  487. OnStateChanged();
  488. }
  489. private Point contextMenuMousePos; //弹出菜单的位置
  490. /// <summary>
  491. /// 弹出菜单的位置
  492. /// </summary>
  493. public Point ContextMenuMousePos
  494. {
  495. get { return contextMenuMousePos; }
  496. set { contextMenuMousePos = value; }
  497. }
  498. //Paste operation
  499. public void Paste( )
  500. {
  501. this.ClerSelectedRect();//Cancel the select area
  502. if(this.copyImage == null)//Not did cut or copy operation
  503. return;
  504. //Convert the paste position.Convert to orginal coordinate when you zoom in or zoom out
  505. Point pastePos = Canvas.Instance.MainView.ConvertViewToCanvasPt(this.contextMenuMousePos);
  506. //Save select area as a image into imGra
  507. ImageGra imGra = new ImageGra(this.copyImage.Clone() as Image,
  508. new Rectangle(pastePos,new Size(this.copyImage.Width,this.copyImage.Height)));
  509. //Canvas.Instance.GraList.Add(imGra);//Add to GraList
  510. //Canvas.Instance.Update();//Update canvas
  511. Canvas.Instance.AddGra(imGra);
  512. }
  513. //Active property of SelectDrawTool
  514. public override bool IsActive
  515. {
  516. get
  517. {
  518. return base.IsActive;//Return the IsActive of GraDrawTool
  519. }
  520. set
  521. {
  522. base.IsActive = value;
  523. //Cancel select operation without doing cut,move or paste
  524. if(!base.IsActive && this.selectRectGra != null)
  525. {
  526. this.ClerSelectedRect();//Cancel the select area
  527. }
  528. }
  529. }
  530. }
  531. //It inherit from GraDrawTool(). It is used for drawing rectangle actions.
  532. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  533. public abstract class RectDrawTool : GraDrawTool
  534. {
  535. public RectDrawTool(GraDrawMode drawMode)//Get the draw mode:frame or solid
  536. :base()
  537. {
  538. this.drawMode = drawMode;
  539. this.toolCursor = Cursors.Cross;
  540. }
  541. protected GraDrawMode drawMode;//protected drawMode variable to save the draw mode
  542. public override string Tip
  543. {
  544. get
  545. {
  546. return "Pressed 'Shift' key to draw a quare";
  547. }
  548. }
  549. private RectGra rectGra;//Save the rectangle
  550. private Point ptStart;//Start postion for drawing
  551. private Point ptPre; ////Temporary point use for round
  552. public override void OnMouseDown(MouseEventArgs e)
  553. {
  554. this.isDrawing = true;//Drawing status
  555. ptStart = new Point(e.X,e.Y);//Get new start point
  556. ptStart = GetRoundPoint(ptStart);//Round the start point
  557. ptPre = ptStart;//Temporary point use for round
  558. //Convert the start position to original coordinate when you zoom in or zoom out
  559. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  560. //Construct a temporary rectangle to display
  561. rectGra = new RectGra(DrawEnvMgr.Instance.Pen,DrawEnvMgr.Instance.Brush,
  562. drawMode,new Rectangle(ptStart,new Size(0,0)));
  563. //Canvas.Instance.GraList.Add(rectGra);//Add to GraList
  564. //Canvas.Instance.Update();//Update canvas
  565. Canvas.Instance.AddGra(rectGra);
  566. }
  567. public override void OnMouseMove(MouseEventArgs e)
  568. {
  569. if(!this.isDrawing)//Not drawing
  570. return;
  571. Point ptEnd = new Point(e.X,e.Y);//Get end point of temporary rectangle
  572. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  573. if(ptEnd == ptPre)//Movement distance < 6
  574. return;
  575. ptPre = ptEnd;//Save end point as a temporary point
  576. //Convert the end position to ordinal coordinate when you zoom in or zoom out
  577. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  578. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  579. {
  580. ptEnd = RegularEndPt(ptStart,ptEnd);
  581. }
  582. rectGra.Rect = CreateValidRect(ptStart,ptEnd);
  583. Canvas.Instance.Update();//Update canvas
  584. }
  585. public override void OnMouseUp(MouseEventArgs e)
  586. {
  587. this.isDrawing = false;//Not Drawing
  588. }
  589. }
  590. //It inherits from RecaDrawTool(). It is used for drawing frame rectangle.
  591. //When we use mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  592. public class FrameRectDrawTool : RectDrawTool
  593. {
  594. /// <summary>
  595. ///
  596. /// </summary>
  597. public FrameRectDrawTool()
  598. :base(GraDrawMode.Frame)
  599. {
  600. }
  601. //Specify the draw type: frame rectangle
  602. public override GraDrawType Type
  603. {
  604. get
  605. {
  606. return GraDrawType.FrameRect;
  607. }
  608. }
  609. }
  610. //It inherit from RecaDrawTool(). It is used for drawing solid rectangle.
  611. //When we use mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  612. public class FillRectDrawTool : RectDrawTool
  613. {
  614. public FillRectDrawTool()
  615. :base(GraDrawMode.Fill)
  616. {
  617. }
  618. //Specify the draw type: solid rectangle
  619. public override GraDrawType Type
  620. {
  621. get
  622. {
  623. return GraDrawType.FillRect;
  624. }
  625. }
  626. }
  627. //It inherit from GraDrawTool(). It is used for drawing rectangle actions.
  628. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  629. public abstract class EllipseDrawTool : GraDrawTool
  630. {
  631. public EllipseDrawTool(GraDrawMode drawMode)//Get the draw mode:frame or solid
  632. :base()
  633. {
  634. this.drawMode = drawMode;
  635. }
  636. protected GraDrawMode drawMode;//protected drawMode variable to save the draw mode
  637. private EllipseGra ellipseGra;//Save the rectangle
  638. private Point ptStart;//Start postion for drawing
  639. private Point ptPre; ////Temporary point use for round
  640. public override string Tip
  641. {
  642. get
  643. {
  644. return "Pressed 'Shift' key to draw a circle";
  645. }
  646. }
  647. public override void OnMouseDown(MouseEventArgs e)
  648. {
  649. this.isDrawing = true;//Drawing status
  650. ptStart = new Point(e.X,e.Y);//Get new start point
  651. ptStart = GetRoundPoint(ptStart);//Round the start point
  652. ptPre = ptStart;//Temporary point use for round
  653. //Convert the start position to original coordinate when you zoom in or zoom out
  654. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  655. //Construct a temporary rectangle to display
  656. ellipseGra = new EllipseGra(DrawEnvMgr.Instance.Pen,DrawEnvMgr.Instance.Brush,
  657. drawMode,new Rectangle(ptStart,new Size(0,0)));
  658. //Canvas.Instance.GraList.Add(ellipseGra);//Add to GraList
  659. //Canvas.Instance.Update();//Update canvas
  660. Canvas.Instance.AddGra(ellipseGra);
  661. }
  662. public override void OnMouseMove(MouseEventArgs e)
  663. {
  664. if(!this.isDrawing)//Not drawing
  665. return;
  666. Point ptEnd = new Point(e.X,e.Y);//Get end point of temporary rectangle
  667. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  668. if(ptEnd == ptPre)//Movement distance < 6
  669. return;
  670. ptPre = ptEnd;//Save end point as a temporary point
  671. //Convert the end position to orginal coordinate when you zoom in or zoom out
  672. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  673. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  674. {
  675. ptEnd = RegularEndPt(ptStart,ptEnd);
  676. }
  677. ellipseGra.Rect = CreateValidRect(ptStart,ptEnd);
  678. Canvas.Instance.Update();//Update canvas
  679. }
  680. public override void OnMouseUp(MouseEventArgs e)
  681. {
  682. this.isDrawing = false;//Not Drawing
  683. }
  684. }
  685. //It inherit from RecaDrawTool(). It is used for drawing frame rectangle.
  686. //When we use mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  687. public class FrameEllipseDrawTool : EllipseDrawTool
  688. {
  689. /// <summary>
  690. ///
  691. /// </summary>
  692. public FrameEllipseDrawTool()
  693. :base(GraDrawMode.Frame)
  694. {
  695. this.toolCursor = Cursors.Cross;
  696. }
  697. //Specify the draw type: frame rectangle
  698. public override GraDrawType Type
  699. {
  700. get
  701. {
  702. return GraDrawType.FrameEllipse;
  703. }
  704. }
  705. }
  706. //It inherit from RecaDrawTool(). It is used for drawing solid rectangle.
  707. //When we use mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  708. public class FillEllipseDrawTool : EllipseDrawTool
  709. {
  710. public FillEllipseDrawTool()
  711. :base(GraDrawMode.Fill)
  712. {
  713. this.toolCursor = Cursors.Cross;
  714. }
  715. //Specify the draw type: solid rectangle
  716. public override GraDrawType Type
  717. {
  718. get
  719. {
  720. return GraDrawType.FillEllipse;
  721. }
  722. }
  723. }
  724. //It inherit from GraDrawTool(). It is used for insert image actions.
  725. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  726. public class ImageDrawTool : GraDrawTool
  727. {
  728. public ImageDrawTool()//Get the draw mode:frame or solid
  729. :base()
  730. {
  731. this.toolCursor = Cursors.Cross;
  732. }
  733. public override GraDrawType Type
  734. {
  735. get
  736. {
  737. return GraDrawType.Image;
  738. }
  739. }
  740. private RectGra rectGra;//Save the rectangle
  741. private Point ptStart;//Start postion for drawing
  742. private Point ptPre; ////Temporary point use for round
  743. public override void OnMouseDown(MouseEventArgs e)
  744. {
  745. this.isDrawing = true;//Drawing status
  746. ptStart = new Point(e.X,e.Y);//Get new start point
  747. ptStart = GetRoundPoint(ptStart);//Round the start point
  748. ptPre = ptStart;//Temporary point use for round
  749. //Convert the start position to original coordinate when you zoom in or zoom out
  750. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  751. //Construct a temporary rectangle to display
  752. rectGra = new RectGra(Pens.DarkBlue,null,
  753. GraDrawMode.Frame,new Rectangle(ptStart,new Size(0,0)));
  754. Canvas.Instance.GraList.Add(rectGra);//Add to GraList
  755. Canvas.Instance.Update();//Update canvas
  756. }
  757. public override void OnMouseMove(MouseEventArgs e)
  758. {
  759. if(!this.isDrawing)//Not drawing
  760. return;
  761. Point ptEnd = new Point(e.X,e.Y);//Get end point of temporary rectangle
  762. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  763. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  764. {
  765. ptEnd = RegularEndPt(ptStart,ptEnd);
  766. }
  767. if(ptEnd == ptPre)//Movement distance < 6
  768. return;
  769. ptPre = ptEnd;//Save end point as a temporary point
  770. //Convert the end position to original coordinate when you zoom in or zoom out
  771. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  772. rectGra.Rect = CreateValidRect(ptStart,ptEnd);
  773. Canvas.Instance.Update();//Update canvas
  774. }
  775. public override void OnMouseUp(MouseEventArgs e)
  776. {
  777. if(!this.isDrawing)
  778. return;
  779. this.isDrawing = false;//Not Drawing
  780. if(rectGra.Rect.Width <5 && rectGra.Rect.Height <5)
  781. return;
  782. Canvas.Instance.GraList.Remove(rectGra);
  783. OpenFileDialog dlg = new OpenFileDialog();
  784. dlg.Filter = "位图文件(*.bmp;*.jpg;*.gif;*.png)|*.bmp;*.jpg;*.gif;*.png|Bitmap files (*.bmp)|*.bmp|Jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
  785. dlg.FilterIndex = 1 ;
  786. dlg.RestoreDirectory = true ;
  787. if(dlg.ShowDialog() == DialogResult.OK)
  788. {
  789. Bitmap bmp = new Bitmap(dlg.FileName);
  790. if(bmp != null)
  791. {
  792. ImageGra imageGra = new ImageGra(bmp,rectGra.Rect);
  793. //Canvas.Instance.GraList.Add(imageGra);//Add to GraList
  794. //Canvas.Instance.Update();//Update canvas
  795. Canvas.Instance.AddGra(imageGra);
  796. }
  797. }
  798. this.rectGra = null;
  799. }
  800. }
  801. //It inherit from GraDrawTool(). It is used for insert text actions.
  802. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  803. public class TextDrawTool : GraDrawTool
  804. {
  805. protected TextDrawTool( )//Get the draw mode:frame or solid
  806. :base()
  807. {
  808. this.toolCursor = Cursors.Cross;
  809. }
  810. private static TextDrawTool instance;
  811. public static TextDrawTool Instance
  812. {
  813. get
  814. {
  815. if(instance == null)
  816. instance = new TextDrawTool();
  817. return instance;
  818. }
  819. }
  820. public override GraDrawType Type
  821. {
  822. get
  823. {
  824. return GraDrawType.Text;
  825. }
  826. }
  827. public override string Tip
  828. {
  829. get
  830. {
  831. return "按“Enter”键停止,点击“ESC”键取消";
  832. }
  833. }
  834. public override bool IsActive
  835. {
  836. get
  837. {
  838. return base.IsActive;
  839. }
  840. set
  841. {
  842. base.IsActive = value;
  843. if(!base.IsActive)
  844. {
  845. if(this.textBox != null)
  846. {
  847. this.EndTextEditing(true);
  848. }
  849. }
  850. }
  851. }
  852. private RectGra rectGra;//保存矩形
  853. private Point ptStart;//Start postion for drawing
  854. private Point ptEnd;
  855. private Point ptPre; ////Temporary point use for round
  856. private Point ptStartView;
  857. private Point ptEndView;
  858. public override void OnMouseDown(MouseEventArgs e)
  859. {
  860. if(this.isJustEditing)
  861. {
  862. Point pt = new Point(e.X,e.Y);
  863. if(!textBox.Bounds.Contains(pt))
  864. this.EndTextEditing(true);
  865. return ;
  866. }
  867. this.isDrawing = true;//Drawing status
  868. ptStartView = new Point(e.X,e.Y);//Get new start point
  869. ptStartView = GetRoundPoint(ptStartView);//Round the start point
  870. ptPre = ptStartView;//Temporary point use for round
  871. //Convert the start position to original coordinate when you zoom in or zoom out
  872. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStartView);
  873. //Construct a temporary rectangle to display
  874. rectGra = new RectGra(Pens.DarkBlue,null,
  875. GraDrawMode.Frame,new Rectangle(ptStart,new Size(0,0)));
  876. Canvas.Instance.GraList.Add(rectGra);//Add to GraList
  877. Canvas.Instance.Update();//Update canvas
  878. //
  879. }
  880. public override void OnMouseMove(MouseEventArgs e)
  881. {
  882. if (this.isJustEditing)//是否只需编辑
  883. return;
  884. if (!this.isDrawing)//没有图纸
  885. return;
  886. ptEndView = new Point(e.X,e.Y);//Get end point of temporary rectangle
  887. ptEndView = GetRoundPoint(ptEndView);//回合结束点。最小鼠标移动距离为6。
  888. if(ptEndView == ptPre)//Movement distance < 6
  889. return;
  890. ptPre = ptEndView;//Save end point as a temporary point
  891. //结束位置转换成原始的,当你放大或坐标缩小
  892. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEndView);
  893. rectGra.Rect = CreateValidRect(ptStart,ptEnd);
  894. Canvas.Instance.Update();//Update canvas
  895. }
  896. /// <summary>
  897. /// 是否只需编辑
  898. /// </summary>
  899. private bool isJustEditing;
  900. private TextBox textBox;
  901. public override void OnMouseUp(MouseEventArgs e)
  902. {
  903. if(this.isJustEditing)
  904. return;
  905. if(!this.isDrawing)
  906. return;
  907. this.isDrawing = false;//Not Drawing
  908. if(rectGra.Rect.Width <5 && rectGra.Rect.Height <5)
  909. return;
  910. Canvas.Instance.GraList.Remove(rectGra);
  911. textBox = new TextBox();
  912. textBox.Multiline = true;
  913. textBox.BackColor = Canvas.Instance.BackColor;
  914. textBox.ForeColor = DrawEnvMgr.Instance.Color;
  915. Rectangle viewRect = CreateValidRect(ptStartView,ptEndView);
  916. float s = Canvas.Instance.MainView.ViewMatrix.Elements[0];
  917. textBox.Bounds = viewRect;
  918. Font f =DrawEnvMgr.Instance.Font;
  919. textBox.Font = new Font(f.Name,f.Size*s);
  920. Canvas.Instance.MainView.Controls.Add(textBox);
  921. textBox.Focus();
  922. textBox.KeyDown +=new KeyEventHandler(textBox_KeyDown);
  923. this.isJustEditing = true;
  924. //FontDialog dlg = new FontDialog(); //instantiating a new fontdialog box
  925. //dlg.Font = DrawEnvMgr.Instance.Font;
  926. //if(DialogResult.OK == dlg.ShowDialog())
  927. //{
  928. // DrawEnvMgr.Instance.Font = dlg.Font; // assigning the font selected by the user to DrawEnvMgr.Instance.Font
  929. //}
  930. }
  931. /// <summary>
  932. /// 完成输入
  933. /// </summary>
  934. /// <param name="isValid"></param>
  935. public void EndTextEditing(bool isValid)
  936. {
  937. if(this.textBox == null)
  938. return;
  939. this.isJustEditing = false;
  940. Canvas.Instance.MainView.Controls.Remove(textBox);
  941. if(isValid && !(textBox.Text == null || textBox.Text.Length == 0))
  942. {
  943. TextGra textGra = new TextGra(textBox.Text,DrawEnvMgr.Instance.Font,
  944. DrawEnvMgr.Instance.Brush,rectGra.Rect);
  945. //Canvas.Instance.GraList.Add(textGra);//Add to GraList
  946. //Canvas.Instance.Update();//Update canvas
  947. Canvas.Instance.AddGra(textGra);
  948. this.rectGra = null;
  949. }
  950. this.textBox = null;
  951. Canvas.Instance.Update();
  952. }
  953. private void textBox_KeyDown(object sender, KeyEventArgs e)
  954. {
  955. if(this.textBox == null)
  956. return;
  957. //Click 'Enter' when you complete input text
  958. if(e.KeyCode == Keys.Enter)
  959. {
  960. //this.EndTextEditing(true);
  961. }
  962. else if(e.KeyCode == Keys.Escape)
  963. {
  964. this.EndTextEditing(false);
  965. }
  966. }
  967. }
  968. //It inherit from GraDrawTool(). It is used for drawing line actions.
  969. //When we mouse to draw a line, we need display some temporary lines before we decide the end point of the line
  970. public class LineDrawTool : GraDrawTool
  971. {
  972. public LineDrawTool()
  973. :base()
  974. {
  975. //this.toolCursor = Cursors.Cross;
  976. Bitmap cursorBmp = new Bitmap(GraEditor.Properties.Resources.pen, new Size(64, 64));
  977. this.toolCursor = new Cursor(cursorBmp.GetHicon());
  978. }
  979. //Specify the draw type: line
  980. public override GraDrawType Type
  981. {
  982. get
  983. {
  984. return GraDrawType.Line;
  985. }
  986. }
  987. public override string Tip
  988. {
  989. get
  990. {
  991. return "按下“shift'键绘制水平或垂直线";
  992. }
  993. }
  994. private LineGra lineGra;//Save the start point,end point,color and width of line
  995. private Point ptStart;
  996. private Point ptPre;//Temporary point use for round
  997. public override void OnMouseDown(MouseEventArgs e)
  998. {
  999. this.isDrawing = true;//Drawing status
  1000. ptStart = new Point(e.X,e.Y);//Start point
  1001. ptStart = GetRoundPoint(ptStart);//Round point
  1002. ptPre = ptStart;//Temporary point use for round
  1003. //Convert the start position to original coordinate when you zoom in or zoom out
  1004. Point pt = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  1005. //Construct a temporary line to display
  1006. lineGra = new LineGra(DrawEnvMgr.Instance.Pen,
  1007. pt,pt);
  1008. //Canvas.Instance.GraList.Add(lineGra);//Add to GraList
  1009. //Canvas.Instance.Update();//Update canvas
  1010. Canvas.Instance.AddGra(lineGra);
  1011. }
  1012. public override void OnMouseMove(MouseEventArgs e)
  1013. {
  1014. if(!this.isDrawing)//Drawing Status=false
  1015. return;
  1016. Point ptEnd = new Point(e.X,e.Y);//Temporary end point
  1017. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  1018. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  1019. {
  1020. int sx = Math.Abs(ptEnd.X - ptStart.X);
  1021. int sy = Math.Abs(ptEnd.Y - ptStart.Y);
  1022. if(sy < sx)
  1023. ptEnd.Y = ptStart.Y;
  1024. else
  1025. ptEnd.X = ptStart.X;
  1026. }
  1027. if( ptEnd == ptPre)//Movement distance < 6
  1028. return;
  1029. ptPre = ptEnd;//Save end point as a temporary point
  1030. //Convert the end position to original coordinate when you zoom in or zoom out
  1031. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1032. lineGra.EndPt = ptEnd;//Set the end point to LineGra
  1033. Canvas.Instance.Update();//Update canvas
  1034. }
  1035. public override void OnMouseUp(MouseEventArgs e)
  1036. {
  1037. this.isDrawing = false;//Drawing status = false
  1038. }
  1039. }
  1040. public class ArrowDrawTool : GraDrawTool
  1041. {
  1042. public ArrowDrawTool()
  1043. : base()
  1044. {
  1045. // this.toolCursor = Cursors.Cross;
  1046. Bitmap cursorBmp = new Bitmap(GraEditor.Properties.Resources.pen, new Size(64, 64));
  1047. this.toolCursor = new Cursor(cursorBmp.GetHicon());
  1048. }
  1049. //Specify the draw type: line
  1050. public override GraDrawType Type
  1051. {
  1052. get
  1053. {
  1054. return GraDrawType.Arrow;
  1055. }
  1056. }
  1057. public override string Tip
  1058. {
  1059. get
  1060. {
  1061. return "按下“shift'键绘制水平或垂箭头";
  1062. }
  1063. }
  1064. private ArrowGra arrowGra;//Save the start point,end point,color and width of line
  1065. private Point ptStart;
  1066. private Point ptPre;//Temporary point use for round
  1067. public override void OnMouseDown(MouseEventArgs e)
  1068. {
  1069. this.isDrawing = true;//Drawing status
  1070. ptStart = new Point(e.X, e.Y);//Start point
  1071. ptStart = GetRoundPoint(ptStart);//Round point
  1072. ptPre = ptStart;//Temporary point use for round
  1073. //Convert the start position to original coordinate when you zoom in or zoom out
  1074. Point pt = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  1075. //Construct a temporary line to display
  1076. AdjustableArrowCap lineArrow = new AdjustableArrowCap(4f, 4f, true);
  1077. //Pen pen = new Pen(DrawEnvMgr.Instance.Pen.Color, DrawEnvMgr.Instance.Pen.Width);
  1078. // pen.CustomEndCap = lineArrow;
  1079. arrowGra = new ArrowGra(DrawEnvMgr.Instance.Pen,
  1080. pt, pt);
  1081. arrowGra.PenExt.Pen.CustomEndCap = lineArrow;
  1082. //Canvas.Instance.GraList.Add(lineGra);//Add to GraList
  1083. //Canvas.Instance.Update();//Update canvas
  1084. Canvas.Instance.AddGra(arrowGra);
  1085. }
  1086. public override void OnMouseMove(MouseEventArgs e)
  1087. {
  1088. if (!this.isDrawing)//Drawing Status=false
  1089. return;
  1090. Point ptEnd = new Point(e.X, e.Y);//Temporary end point
  1091. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  1092. if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  1093. {
  1094. int sx = Math.Abs(ptEnd.X - ptStart.X);
  1095. int sy = Math.Abs(ptEnd.Y - ptStart.Y);
  1096. if (sy < sx)
  1097. ptEnd.Y = ptStart.Y;
  1098. else
  1099. ptEnd.X = ptStart.X;
  1100. }
  1101. if (ptEnd == ptPre)//Movement distance < 6
  1102. return;
  1103. ptPre = ptEnd;//Save end point as a temporary point
  1104. //Convert the end position to original coordinate when you zoom in or zoom out
  1105. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1106. arrowGra.EndPt = ptEnd;//Set the end point to LineGra
  1107. Canvas.Instance.Update();//Update canvas
  1108. }
  1109. public override void OnMouseUp(MouseEventArgs e)
  1110. {
  1111. this.isDrawing = false;//Drawing status = false
  1112. }
  1113. }
  1114. /// <summary>
  1115. /// 平移视图
  1116. /// </summary>
  1117. public class MobileViewsDrawTool : GraDrawTool
  1118. {
  1119. Bitmap cursorBmp64 = new Bitmap(GraEditor.Properties.Resources.hand_48, new Size(64, 64));
  1120. Bitmap cursorBmp56= new Bitmap(GraEditor.Properties.Resources.hand_48, new Size(60, 60));
  1121. public MobileViewsDrawTool()
  1122. : base()
  1123. {
  1124. // this.toolCursor = new Cursor(GraEditor.Properties.Resources.hand_48.GetHicon());// Cursors.SizeAll;
  1125. this.toolCursor = new Cursor(cursorBmp64.GetHicon());
  1126. }
  1127. //Specify the draw type: line
  1128. public override GraDrawType Type
  1129. {
  1130. get
  1131. {
  1132. return GraDrawType.MobileViews;
  1133. }
  1134. }
  1135. public override string Tip
  1136. {
  1137. get
  1138. {
  1139. return "按下左键移动视图";
  1140. }
  1141. }
  1142. // private ArrowGra arrowGra;//Save the start point,end point,color and width of line
  1143. private Point ptStart;
  1144. private Point ptPre;//临时使用的点
  1145. public override void OnMouseDown(MouseEventArgs e)
  1146. {
  1147. this.isDrawing = true;//Drawing status
  1148. ptStart = new Point(e.X, e.Y);//Start point
  1149. ptStart = GetRoundPoint(ptStart);//Round point
  1150. ptPre = ptStart;//Temporary point use for round
  1151. Canvas.Instance.MainView.Cursor = new Cursor(cursorBmp56.GetHicon());
  1152. }
  1153. public override void OnMouseMove(MouseEventArgs e)
  1154. {
  1155. if (!this.isDrawing)//Drawing Status=false
  1156. return;
  1157. Point ptEnd = new Point(e.X, e.Y);//Temporary end point
  1158. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  1159. int mSpeed = 30;
  1160. /// <summary>
  1161. /// 移动步长值
  1162. /// </summary>
  1163. int moveAmount = 10;
  1164. if (e.Button == MouseButtons.Left)
  1165. {
  1166. if (ptStart.Y > ptEnd.Y + mSpeed)
  1167. {//向右
  1168. if (Canvas.Instance.MainView.VerticalScroll.Value + moveAmount >= Canvas.Instance.MainView.VerticalScroll.Maximum)
  1169. {
  1170. Canvas.Instance.MainView.VerticalScroll.Value = Canvas.Instance.MainView.VerticalScroll.Maximum;
  1171. }
  1172. else
  1173. {
  1174. Canvas.Instance.MainView.VerticalScroll.Value += moveAmount;
  1175. }
  1176. }
  1177. else if (ptStart.Y < ptEnd.Y - mSpeed)
  1178. {
  1179. //向左
  1180. if (Canvas.Instance.MainView.VerticalScroll.Value - moveAmount <= 0)
  1181. {
  1182. Canvas.Instance.MainView.VerticalScroll.Value = 0;
  1183. }
  1184. else
  1185. {
  1186. Canvas.Instance.MainView.VerticalScroll.Value -= moveAmount;
  1187. }
  1188. }
  1189. if (ptStart.X > ptEnd.X + mSpeed)
  1190. //向下
  1191. {
  1192. if (Canvas.Instance.MainView.HorizontalScroll.Value + moveAmount >= Canvas.Instance.MainView.HorizontalScroll.Maximum)
  1193. {
  1194. Canvas.Instance.MainView.HorizontalScroll.Value = Canvas.Instance.MainView.HorizontalScroll.Maximum;
  1195. }
  1196. else
  1197. {
  1198. Canvas.Instance.MainView.HorizontalScroll.Value += moveAmount;
  1199. }
  1200. }
  1201. else if (ptStart.X < ptEnd.X - mSpeed)
  1202. {//向上
  1203. if (Canvas.Instance.MainView.HorizontalScroll.Value - moveAmount <= 0)
  1204. {
  1205. Canvas.Instance.MainView.HorizontalScroll.Value = 0;
  1206. }
  1207. else
  1208. {
  1209. Canvas.Instance.MainView.HorizontalScroll.Value -= moveAmount;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. public override void OnMouseUp(MouseEventArgs e)
  1215. {
  1216. this.isDrawing = false;//Drawing status = false
  1217. Canvas.Instance.MainView.Cursor = new Cursor(cursorBmp64.GetHicon());
  1218. }
  1219. }
  1220. //It inherit from GraDrawTool(). It is used for drawing lines actions.
  1221. //When we mouse to draw a line, we need display some temporary lines before we decide the end point of the line
  1222. public class LinesDrawTool : GraDrawTool
  1223. {
  1224. public LinesDrawTool()
  1225. :base()
  1226. {
  1227. this.ptList = new ArrayList();
  1228. //this.toolCursor = Cursors.Cross;
  1229. Bitmap cursorBmp = new Bitmap(GraEditor.Properties.Resources.pen, new Size(64, 64));
  1230. this.toolCursor = new Cursor(cursorBmp.GetHicon());
  1231. }
  1232. //Specify the draw type: line
  1233. public override GraDrawType Type
  1234. {
  1235. get
  1236. {
  1237. return GraDrawType.Lines;
  1238. }
  1239. }
  1240. public override string Tip
  1241. {
  1242. get
  1243. {
  1244. return "双击鼠标左键,完成画线";
  1245. }
  1246. }
  1247. private LinesGra linesGra;//Save the start point,end point,color and width of line
  1248. private ArrayList ptList;
  1249. private Point ptStart;
  1250. public override void OnMouseDown(MouseEventArgs e)
  1251. {
  1252. ptStart = new Point(e.X,e.Y);//Start point
  1253. ptStart = GetRoundPoint(ptStart);//Round point
  1254. //Point ptPre = ptStart;//Temporary point use for round
  1255. //Convert the start position to orginal coordinate when you zoom in or zoom out
  1256. Point pt = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  1257. if( !this.isDrawing)
  1258. {
  1259. //start creating lines graph element
  1260. this.isDrawing = true;//Drawing status
  1261. ptList.Clear();
  1262. ptList.Add(pt);
  1263. //Construct a temporary line to display
  1264. linesGra = new LinesGra(DrawEnvMgr.Instance.Pen,
  1265. new Point[]{pt});
  1266. Canvas.Instance.GraList.Add(linesGra);//Add to GraList
  1267. Canvas.Instance.IsModified = true;
  1268. }
  1269. else
  1270. {
  1271. ptList.Add(pt);
  1272. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1273. linesGra.Pts = pts;
  1274. }
  1275. Canvas.Instance.Update();//Update canvas
  1276. }
  1277. private Point ptPre;//Temproary point use for round
  1278. public override void OnMouseMove(MouseEventArgs e)
  1279. {
  1280. if(!this.isDrawing)//Drawing Status=false
  1281. return;
  1282. Point ptEnd = new Point(e.X,e.Y);//Temproary end point
  1283. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  1284. if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  1285. {
  1286. int sx = Math.Abs(ptEnd.X - ptStart.X);
  1287. int sy = Math.Abs(ptEnd.Y - ptStart.Y);
  1288. if (sy < sx)
  1289. ptEnd.Y = ptStart.Y;
  1290. else
  1291. ptEnd.X = ptStart.X;
  1292. }
  1293. if( ptEnd == ptPre)//Movement distance < 6
  1294. return;
  1295. ptPre = ptEnd;//Save end point as a temproary point
  1296. //Convert the end position to orginal coordinate when you zoom in or zoom out
  1297. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1298. if(ptList.Count > 1)
  1299. ptList.RemoveAt(ptList.Count-1);
  1300. ptList.Add(ptEnd);
  1301. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1302. linesGra.Pts = pts;//Set the end point to LineGra
  1303. Canvas.Instance.Update();//Update canvas
  1304. }
  1305. public override void OnMouseUp(MouseEventArgs e)
  1306. {
  1307. //Do nothing
  1308. }
  1309. public override void OnDoubleClick(EventArgs e)
  1310. {
  1311. this.isDrawing = false;//Drawing status = false
  1312. }
  1313. public override void OnKeyUp(KeyEventArgs e)
  1314. {
  1315. if (e.KeyCode == Keys.Escape||e.KeyCode== Keys.Enter)
  1316. {
  1317. this.isDrawing = false;
  1318. Point ptEnd = new Point(ptStart.X, ptStart.Y);//Temproary end point
  1319. ptEnd = GetRoundPoint(ptEnd);
  1320. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1321. if (ptList.Count > 1)
  1322. ptList.RemoveAt(ptList.Count - 1);
  1323. ptList.Add(ptEnd);
  1324. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1325. linesGra.Pts = pts;//Set the end point to LineGra
  1326. Canvas.Instance.Update();//Update canvas
  1327. }
  1328. //base.OnKeyUp(e);
  1329. }
  1330. }
  1331. //它从GraDrawTool继承()。它是用于绘制线条的操作。
  1332. //当我们的鼠标擦除
  1333. public class EraseDrawTool : GraDrawTool
  1334. {
  1335. public EraseDrawTool()
  1336. :base()
  1337. {
  1338. this.ptList = new ArrayList();
  1339. // this.toolCursor = CreateCursor(GraEditor.Properties.Resources.EraseCursor);
  1340. Bitmap cursorBmp = new Bitmap(GraEditor.Properties.Resources.draw_2562, new Size(64, 64));
  1341. this.toolCursor = new Cursor(cursorBmp.GetHicon());
  1342. //Cursor
  1343. }
  1344. //Specify the draw type: line
  1345. public override GraDrawType Type
  1346. {
  1347. get
  1348. {
  1349. return GraDrawType.Erase;
  1350. }
  1351. }
  1352. private ArrayList ptList;
  1353. private Point ptStart;
  1354. private Point ptStartView;
  1355. private Point ptPreView;//Temporary point use for round
  1356. public override void OnMouseDown(MouseEventArgs e)
  1357. {
  1358. ptStartView = new Point(e.X,e.Y);//Start point
  1359. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStartView);
  1360. if( !this.isDrawing)
  1361. {
  1362. //start creating lines graph element
  1363. Color eraseColor=Canvas.Instance.BackColor;
  1364. float sx = Canvas.Instance.MainView.ViewMatrix.Elements[0];
  1365. float w = sx * 10;
  1366. this.erasePen = new Pen(eraseColor,w);
  1367. this.isDrawing = true;//Drawing status
  1368. ptList.Clear();
  1369. ptList.Add(ptStart);
  1370. ptPreView = ptStartView;
  1371. }
  1372. }
  1373. private Pen erasePen;
  1374. public override void OnMouseMove(MouseEventArgs e)
  1375. {
  1376. if(!this.isDrawing)//Drawing Status=false
  1377. return;
  1378. Point ptCurView = new Point(e.X,e.Y);//Temporary end point
  1379. //Convert the end position to original coordinate when you zoom in or zoom out
  1380. Point ptCur = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptCurView);
  1381. ptList.Add(ptCur);
  1382. using(Graphics g = Canvas.Instance.MainView.CreateGraphics())
  1383. {
  1384. Rectangle rc = new Rectangle(new Point(0,0),Canvas.Instance.Size);
  1385. rc = Canvas.Instance.MainView.ConvertCanvasToViewBitmapRect(rc);
  1386. Region r = new Region(rc);
  1387. Region old = g.Clip;
  1388. g.Clip = r;
  1389. g.DrawLine(this.erasePen,ptPreView,ptCurView);
  1390. g.Clip = old;
  1391. }
  1392. ptPreView = ptCurView;
  1393. }
  1394. public override void OnMouseUp(MouseEventArgs e)
  1395. {
  1396. if(this.isDrawing)
  1397. {
  1398. this.isDrawing = false;//Drawing status = false
  1399. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1400. EraseLinesGra linesGra = new EraseLinesGra(this.erasePen,pts);
  1401. Canvas.Instance.GraList.Add(linesGra);
  1402. Canvas.Instance.IsModified = true;
  1403. }
  1404. }
  1405. }
  1406. public class FreehandDrawTool : GraDrawTool
  1407. {
  1408. public FreehandDrawTool()
  1409. :base()
  1410. {
  1411. this.ptList = new ArrayList();
  1412. //this.toolCursor = Cursors.Cross;
  1413. Bitmap cursorBmp = new Bitmap(GraEditor.Properties.Resources.Freehand, new Size(64, 64));
  1414. this.toolCursor = new Cursor(cursorBmp.GetHicon());
  1415. //Cursor
  1416. }
  1417. //Specify the draw type: line
  1418. public override GraDrawType Type
  1419. {
  1420. get
  1421. {
  1422. return GraDrawType.Freehand;
  1423. }
  1424. }
  1425. private ArrayList ptList;
  1426. private Point ptStart;
  1427. private Point ptStartView;
  1428. private Point ptPreView;//Temporary point use for round
  1429. public override void OnMouseDown(MouseEventArgs e)
  1430. {
  1431. ptStartView = new Point(e.X,e.Y);//Start point
  1432. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStartView);
  1433. if( !this.isDrawing)
  1434. {
  1435. //start creating lines graph element
  1436. this.isDrawing = true;//Drawing status
  1437. ptList.Clear();
  1438. ptList.Add(ptStart);
  1439. ptPreView = ptStartView;
  1440. }
  1441. }
  1442. public override void OnMouseMove(MouseEventArgs e)
  1443. {
  1444. if(!this.isDrawing)//Drawing Status=false
  1445. return;
  1446. Point ptCurView = new Point(e.X,e.Y);//Temporary end point
  1447. //Convert the end position to orginal coordinate when you zoom in or zoom out
  1448. Point ptCur = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptCurView);
  1449. ptList.Add(ptCur);
  1450. Pen p = DrawEnvMgr.Instance.Pen;
  1451. using(Graphics g = Canvas.Instance.MainView.CreateGraphics())
  1452. {
  1453. Rectangle rc = new Rectangle(new Point(0,0),Canvas.Instance.Size);
  1454. rc = Canvas.Instance.MainView.ConvertCanvasToViewBitmapRect(rc);
  1455. Region r = new Region(rc);
  1456. Region old = g.Clip;
  1457. g.Clip = r;
  1458. g.DrawLine(p,ptPreView,ptCurView);
  1459. g.Clip = old;
  1460. }
  1461. ptPreView = ptCurView;
  1462. }
  1463. public override void OnMouseUp(MouseEventArgs e)
  1464. {
  1465. if(this.isDrawing)
  1466. {
  1467. this.isDrawing = false;//Drawing status = false
  1468. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1469. FreehandLinesGra linesGra = new FreehandLinesGra(DrawEnvMgr.Instance.Pen,pts);
  1470. Canvas.Instance.GraList.Add(linesGra);
  1471. Canvas.Instance.IsModified = true;
  1472. }
  1473. }
  1474. }
  1475. }