GraDrawTool.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. /******************************************************************************************
  2. *****************File Name:GraDrawTool.cs, Version 1.0, 8 November 2004********************
  3. ******************************Author: Kanti Surapaneni*************************************
  4. **This class is defined to represent drawing method for different drawing type. **
  5. ** The following classes are inherit from GraDrawTool-- DrawEnvMgr, GraDrawTool, **
  6. ** GraDrawToolMgr, SelectDrawTool, RectDrawTool, FrameRectDrawTool, FillRectDrawTool, **
  7. ** Line DrawTool, FramRectDrawtool, FillRectDrawTool. DrawEnvMgr class stores the **
  8. ** global pen, brush and font objects that are used to draw the graphical elements. **
  9. ** GraDrawToolMgr is the class that manages all these drawing tools classes. **
  10. ** Its functions are mainly to add new draw tool, getting the toolbar that is selected **
  11. ** so as to perform its function, setting the toolbar active. GraDrawTool class **
  12. ** function is to make sure that the drawing operation is fulfilled. It sets the toolbar **
  13. ** clicked to active and sends it to GraDrawToolMgr class. All the other DrawTools that **
  14. ** are inherited from this class will perform their specific operation like selecting, **
  15. ** drawing rectangle, line etc when that particular toolbar is selected. **
  16. ** **
  17. ******************************************************************************************/
  18. ///
  19. using System;
  20. using System.Collections;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.Windows.Forms;
  24. using System.Reflection;
  25. using System.IO;
  26. using GraEditor.Elements;//Elements include Line,Lines,Rectangle,Ellipse,Text,Image
  27. // Namespace DrawTool, include GraDrawTool,DrawEnvMgr,GraDrawToolMgr,LineDrawTool,LinesDrawTool,
  28. //FrameRectDrawTool, FillRectDrawTool, FrameEllipseDrawTool,FillEllipseDrawTool,ImageDrawTool,TextDrawTool,SelectDrawTool,GraDrawType
  29. namespace GraEditor.DrawTools
  30. {
  31. //Save Global pen, brush and font. We use these object when we draw graph element on canvas
  32. public class DrawEnvMgr
  33. {
  34. protected DrawEnvMgr()
  35. {
  36. this.pen = new Pen(Color.DarkGreen,1); //Set default pen
  37. this.brush = new SolidBrush(Color.DarkGreen);//Set default brush
  38. this.font = new Font("Arial",30); //Set default font
  39. }
  40. private static DrawEnvMgr instance;// instance of DrawEnvMgr
  41. //Get instance of DrawEnvMgr
  42. public static DrawEnvMgr Instance
  43. {
  44. get
  45. {
  46. if(instance == null)
  47. instance = new DrawEnvMgr();
  48. return instance;
  49. }
  50. }
  51. protected Pen pen; //protected pen variable to save pen object
  52. //Pen property of DrawEnvMgr
  53. public Pen Pen
  54. {
  55. get { return pen; }
  56. }
  57. protected SolidBrush brush;//protected solidbrush variable to save brush object
  58. //Brush property of DrawEnvMgr
  59. public Brush Brush
  60. {
  61. get { return brush; }
  62. }
  63. //Set pen and brush color
  64. public Color Color
  65. {
  66. get { return pen.Color; }
  67. set
  68. {
  69. pen.Color = value;
  70. brush.Color = value;
  71. }
  72. }
  73. //Pen width property of DrawEnvMgr
  74. public int PenWidth
  75. {
  76. get { return (int)pen.Width; }
  77. set { pen.Width = value; }
  78. }
  79. protected Font font; //private font variable to save font
  80. //Font property of DrawEnvMgr
  81. public Font Font
  82. {
  83. get { return font ; }
  84. set { font = value; }
  85. }
  86. }
  87. //Specify how many draw type is included
  88. public enum GraDrawType
  89. {
  90. Select,Line,Lines,Freehand,Erase,FrameRect,FillRect,FrameEllipse,FillEllipse,Text,Image
  91. }
  92. //Abstract class for drawing operation.
  93. //SelectDrawTool(), LineDrawTool(), LinesDrawTool(), ImageDrawTool(), ImageDrawTool(), RectDrawTool(),
  94. //EllipseDrawTool() and TextDrawTool() are inherit from it.
  95. public abstract class GraDrawTool
  96. {
  97. /// <summary>
  98. ///
  99. /// </summary>
  100. ///
  101. public GraDrawTool()
  102. {
  103. }
  104. //Abstract variable.
  105. public abstract GraDrawType Type
  106. {
  107. get;
  108. }
  109. private GraDrawToolMgr drawToolMgr;//Private drawToolMgr variable of GraDrawTool
  110. //DrawTool Mgr property of GraDrawTool
  111. public GraDrawToolMgr DrawToolMgr
  112. {
  113. get { return drawToolMgr; }
  114. set { drawToolMgr = value;}
  115. }
  116. protected bool isDrawing; //When you are drawing,isDrawing is true. Else isDrawing is false
  117. private ToolBarButton tblBtn;//Save the ToolBar button
  118. //Toolbar button property of GraDrawTool
  119. public ToolBarButton TblBtn
  120. {
  121. get { return tblBtn; }
  122. set { tblBtn = value ;}
  123. }
  124. private Cursor oldCursor;
  125. protected Cursor toolCursor;
  126. public static event EventHandler ActiveStateChanged;
  127. public void OnActiveStateChanged()
  128. {
  129. if(ActiveStateChanged != null)
  130. ActiveStateChanged(this,EventArgs.Empty);
  131. }
  132. private bool isActive;//When the ToolBar button is pushdown, isActive is true. Else isActive is false
  133. //Active property of GraDrawTool
  134. public virtual bool IsActive
  135. {
  136. get { return isActive; }
  137. set
  138. {
  139. this.isActive = value;
  140. tblBtn.Pushed = this.isActive;
  141. OnActiveStateChanged();
  142. if(this.toolCursor != null)
  143. {
  144. if(this.isActive)
  145. {
  146. this.oldCursor = Canvas.Instance.MainView.Cursor;
  147. Canvas.Instance.MainView.Cursor = this.toolCursor;
  148. }
  149. else
  150. Canvas.Instance.MainView.Cursor = this.oldCursor;
  151. }
  152. }
  153. }
  154. public virtual string Tip
  155. {
  156. get { return "";}
  157. }
  158. 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
  159. public Cursor CreateCursor(string cursorName)
  160. {
  161. Assembly a = Assembly.GetExecutingAssembly();
  162. Stream s = a.GetManifestResourceStream(cursorName);
  163. return new Cursor(s);
  164. }
  165. //Round calculation of mouse movement.Mouse must move at least 6 pixel every time.
  166. public static Point GetRoundPoint(Point pt)
  167. {
  168. int x = (pt.X+gridSize/2)/gridSize*gridSize;
  169. int y = (pt.Y+gridSize/2)/gridSize*gridSize;
  170. return new Point(x,y);
  171. }
  172. public static Point RegularEndPt(Point ptStart, Point ptEnd)
  173. {
  174. int sx = Math.Abs(ptEnd.X - ptStart.X);
  175. int sy = Math.Abs(ptEnd.Y - ptStart.Y);
  176. int s = Math.Max(sx,sy);
  177. int x =0;
  178. int y =0;
  179. if(ptEnd.X > ptStart.X)
  180. x = ptStart.X + s;
  181. else
  182. x = ptStart.X - s;
  183. if(ptEnd.Y > ptStart.Y)
  184. y = ptStart.Y + s;
  185. else
  186. y = ptStart.Y - s;
  187. return new Point(x,y);
  188. }
  189. public static Rectangle CreateValidRect(Point pt1, Point pt2)
  190. {
  191. int x = Math.Min(pt1.X,pt2.X);//Judge whether the current x position is at the right of the start point or not
  192. int y = Math.Min(pt1.Y,pt2.Y);//Judge whether the current y position is under the start point or no
  193. int w = Math.Abs(pt2.X - pt1.X);//calculate the width of rectangle
  194. int h = Math.Abs(pt2.Y - pt1.Y);//calculate the height of rectangle
  195. return new Rectangle(new Point(x,y),new Size(w,h));
  196. }
  197. public abstract void OnMouseDown(MouseEventArgs e);//abstract OnMouseDown event
  198. public abstract void OnMouseMove(MouseEventArgs e);//abstract OnMouseMove event
  199. public abstract void OnMouseUp(MouseEventArgs e);//abstract OnMouseUp event
  200. public virtual void OnDoubleClick(EventArgs e)//abstract OnDoubleClick event
  201. {
  202. }
  203. }
  204. //Manage draw tools, add new draw tool type
  205. public class GraDrawToolMgr
  206. {
  207. protected GraDrawToolMgr()
  208. {
  209. toolMap = new Hashtable(); //Use hash table to reach the draw tool, it is fast than using vector.toolMap(draw tool type)=draw tool.
  210. }
  211. private static GraDrawToolMgr instance;//instance of GraDrawToolMgr
  212. //Return the instance of GraDrawToolMgr
  213. public static GraDrawToolMgr Instance
  214. {
  215. get
  216. {
  217. if(instance == null)
  218. instance = new GraDrawToolMgr();
  219. return instance;
  220. }
  221. }
  222. private Hashtable toolMap; // private hash table to save draw tool types
  223. //Return draw tool
  224. public GraDrawTool this[GraDrawType t]
  225. {
  226. get { return toolMap[t] as GraDrawTool; }
  227. set
  228. {
  229. toolMap[t] = value;
  230. }
  231. }
  232. //Add draw tool to toolMap
  233. public void AddDrawTool(GraDrawTool drawTool)
  234. {
  235. if(drawTool == null)
  236. return;
  237. drawTool.DrawToolMgr = this;
  238. toolMap[drawTool.Type] = drawTool;
  239. }
  240. private GraDrawTool activeDrawTool;//Save the current draw tool
  241. //Active draw tool property of GraDrawToolMgr
  242. public GraDrawTool ActiveDrawTool
  243. {
  244. get { return activeDrawTool; }
  245. }
  246. //Set the current draw tool
  247. public void SetActiveTool(GraDrawType t)
  248. {
  249. GraDrawTool clickDrawTool = this[t];//Get which Toolbar button is clicked
  250. if(this.activeDrawTool == clickDrawTool)//Reclick the same Toolbar button.Disable the last cliked Toolbar button
  251. {
  252. this.activeDrawTool.IsActive = false;
  253. this.activeDrawTool = null;
  254. return;
  255. }
  256. if(this.activeDrawTool != null)//Click a new Toolbar button
  257. this.activeDrawTool.IsActive = false;
  258. this.activeDrawTool = clickDrawTool;//Set the new clicked Toolbar button as current one
  259. this.activeDrawTool.IsActive = true;
  260. }
  261. }
  262. //It is inherited from GraDrawTool(). It is used for selecting actions.
  263. //When we use mouse to select an area, we use this class display some temporary rectangles before we decide the end point of select
  264. public class SelectDrawTool : GraDrawTool
  265. {
  266. protected SelectDrawTool()
  267. :base()
  268. {
  269. this.isSelectRectVisible = false;//Do not display the rectangle frame
  270. }
  271. //Reset the select area
  272. public void ReSet()
  273. {
  274. this.selectRectGra = null;
  275. this.isSelectRectVisible = false;
  276. }
  277. //Specify select type
  278. public override GraDrawType Type
  279. {
  280. get
  281. {
  282. return GraDrawType.Select;
  283. }
  284. }
  285. private static SelectDrawTool instance;//instance of SelectDrawTool
  286. //Return instance of SelectDrawTool
  287. public static SelectDrawTool Instance
  288. {
  289. get
  290. {
  291. if(instance == null)
  292. instance = new SelectDrawTool();
  293. return instance;
  294. }
  295. }
  296. //Select area.Size and position of select
  297. public Rectangle SelectRect
  298. {
  299. get
  300. {
  301. if(selectRectGra == null)
  302. return Rectangle.Empty;
  303. else
  304. return selectRectGra.Rect;
  305. }
  306. }
  307. public event EventHandler StateChanged;
  308. public void OnStateChanged()
  309. {
  310. if(StateChanged != null)
  311. StateChanged(this,EventArgs.Empty);
  312. }
  313. private RectGra selectRectGra;//private RectGra variable so save select area
  314. public RectGra SelectRectGra
  315. {
  316. get { return this.selectRectGra; }
  317. }
  318. private Point ptStart;
  319. private Point ptPre;//Temporary point use for round
  320. private bool isSelectRectVisible;//Frame of select is visible or not
  321. //Cancel the selected operation
  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);//Start point of select
  333. ptStart = GetRoundPoint(ptStart);//Round the start point
  334. ptPre = ptStart;//Temporary point use for round
  335. //Convert the postion to the original coordinate when you zoom in or zoom out
  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;//select frame is dash line
  347. selectRectGra = new RectGra(p,null,
  348. GraDrawMode.Frame,new Rectangle(ptStart,new Size(0,0)));//Use selectRectGra to save the positon and size of Select area
  349. }
  350. Canvas.Instance.GraList.Add(selectRectGra);//Add select to GraList
  351. this.isSelectRectVisible = true;//Set select frame visible
  352. Canvas.Instance.Update();//Update the canvas
  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. //Set the position and size of select area
  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. private Bitmap copyImage;//Save the select area as a picture
  388. public Bitmap CopyImage
  389. {
  390. get { return this.copyImage; }
  391. }
  392. // private ToolBarButton cutBtn;
  393. // public ToolBarButton CutBtn
  394. // {
  395. // get { return cutBtn;}
  396. // set
  397. // {
  398. //
  399. // cutBtn = value;
  400. // if(this.selectRectGra == null)
  401. // cutBtn.Enabled = false;
  402. // }
  403. // }
  404. // private ToolBarButton copyBtn;
  405. // public ToolBarButton CopyBtn
  406. // {
  407. // get { return copyBtn;}
  408. // set
  409. // {
  410. // copyBtn = value;
  411. // if(this.selectRectGra == null)
  412. // copyBtn.Enabled = false;
  413. //
  414. // }
  415. // }
  416. //Cut operation
  417. public void Cut()
  418. {
  419. if(!this.CreateCopyImage())
  420. return;
  421. //
  422. Color c = Canvas.Instance.BackColor;//Background color
  423. Brush b = new SolidBrush(c);
  424. Pen p = new Pen(c);
  425. EraseRectGra rGra = new EraseRectGra(p,b,GraDrawMode.Frame | GraDrawMode.Fill,
  426. this.selectRectGra.Rect);//Set the select area with background color
  427. b.Dispose();//Dispose the brush object
  428. p.Dispose();//Dispose the pen object
  429. Canvas.Instance.AddGra(rGra);
  430. this.selectRectGra = null;
  431. OnStateChanged();
  432. }
  433. private bool CreateCopyImage()
  434. {
  435. if(this.selectRectGra == null)
  436. return false;;
  437. this.ClerSelectedRect();//Cancel the select area
  438. Canvas canvas = Canvas.Instance;//Get instance of canvas
  439. Rectangle destRect = new Rectangle(new Point(0,0),this.selectRectGra.Rect.Size);//Paste the cut area to this rectangle area
  440. Rectangle souRect = this.selectRectGra.Rect;//Save position and size of select area in souRect
  441. souRect = canvas.MainView.ConvertCanvasToViewBitmapRect(souRect);//Convert to orginal coordinate when you zoom in or zoom out
  442. this.copyImage = new Bitmap(destRect.Width,destRect.Height);//construct a image
  443. //construct a graphic object from copyImage
  444. using(Graphics g = Graphics.FromImage(this.copyImage))
  445. {
  446. //Draw the graph on destination area
  447. g.DrawImage(MainView.ViewBitmap,destRect,
  448. souRect.X,souRect.Y,souRect.Width,souRect.Height,GraphicsUnit.Pixel);
  449. }
  450. this.copyImage.MakeTransparent(Canvas.Instance.BackColor);// Make the copyImage transparent
  451. return true;
  452. }
  453. //Copy operation
  454. public void Copy()
  455. {
  456. if(!this.CreateCopyImage())
  457. return ;
  458. this.selectRectGra = null;
  459. OnStateChanged();
  460. // Color c = Canvas.Instance.BackColor;//Background color
  461. // Brush b = new SolidBrush(c);
  462. // Pen p = new Pen(c);
  463. // RectGra rGra = new RectGra(p,b,GraDrawMode.Frame | GraDrawMode.Fill,
  464. // this.selectRectGra.Rect);//Set the select area with background color
  465. //
  466. // b.Dispose();//Dispose the brush object
  467. // p.Dispose();//Dispose the pen object
  468. //
  469. // canvas.GraList.Add(rGra);//Add to GraList
  470. // canvas.Update();//Update canvas
  471. }
  472. private Point contextMenuMousePos; //position of pop-up menu
  473. public Point ContextMenuMousePos
  474. {
  475. get { return contextMenuMousePos; }
  476. set { contextMenuMousePos = value; }
  477. }
  478. //Paste operation
  479. public void Paste( )
  480. {
  481. this.ClerSelectedRect();//Cancel the select area
  482. if(this.copyImage == null)//Not did cut or copy operation
  483. return;
  484. //Convert the paste position.Convert to orginal coordinate when you zoom in or zoom out
  485. Point pastePos = Canvas.Instance.MainView.ConvertViewToCanvasPt(this.contextMenuMousePos);
  486. //Save select area as a image into imGra
  487. ImageGra imGra = new ImageGra(this.copyImage.Clone() as Image,
  488. new Rectangle(pastePos,new Size(this.copyImage.Width,this.copyImage.Height)));
  489. //Canvas.Instance.GraList.Add(imGra);//Add to GraList
  490. //Canvas.Instance.Update();//Update canvas
  491. Canvas.Instance.AddGra(imGra);
  492. }
  493. //Active property of SelectDrawTool
  494. public override bool IsActive
  495. {
  496. get
  497. {
  498. return base.IsActive;//Return the IsActive of GraDrawTool
  499. }
  500. set
  501. {
  502. base.IsActive = value;
  503. //Cancel select operation without doing cut,move or paste
  504. if(!base.IsActive && this.selectRectGra != null)
  505. {
  506. this.ClerSelectedRect();//Cancel the select area
  507. }
  508. }
  509. }
  510. }
  511. //It inherit from GraDrawTool(). It is used for drawing rectangle actions.
  512. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  513. public abstract class RectDrawTool : GraDrawTool
  514. {
  515. public RectDrawTool(GraDrawMode drawMode)//Get the draw mode:frame or solid
  516. :base()
  517. {
  518. this.drawMode = drawMode;
  519. this.toolCursor = Cursors.Cross;
  520. }
  521. protected GraDrawMode drawMode;//protected drawMode variable to save the draw mode
  522. public override string Tip
  523. {
  524. get
  525. {
  526. return "Pressed 'Shift' key to draw a quare";
  527. }
  528. }
  529. private RectGra rectGra;//Save the rectangle
  530. private Point ptStart;//Start postion for drawing
  531. private Point ptPre; ////Temporary point use for round
  532. public override void OnMouseDown(MouseEventArgs e)
  533. {
  534. this.isDrawing = true;//Drawing status
  535. ptStart = new Point(e.X,e.Y);//Get new start point
  536. ptStart = GetRoundPoint(ptStart);//Round the start point
  537. ptPre = ptStart;//Temporary point use for round
  538. //Convert the start position to original coordinate when you zoom in or zoom out
  539. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  540. //Construct a temporary rectangle to display
  541. rectGra = new RectGra(DrawEnvMgr.Instance.Pen,DrawEnvMgr.Instance.Brush,
  542. drawMode,new Rectangle(ptStart,new Size(0,0)));
  543. //Canvas.Instance.GraList.Add(rectGra);//Add to GraList
  544. //Canvas.Instance.Update();//Update canvas
  545. Canvas.Instance.AddGra(rectGra);
  546. }
  547. public override void OnMouseMove(MouseEventArgs e)
  548. {
  549. if(!this.isDrawing)//Not drawing
  550. return;
  551. Point ptEnd = new Point(e.X,e.Y);//Get end point of temporary rectangle
  552. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  553. if(ptEnd == ptPre)//Movement distance < 6
  554. return;
  555. ptPre = ptEnd;//Save end point as a temporary point
  556. //Convert the end position to ordinal coordinate when you zoom in or zoom out
  557. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  558. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  559. {
  560. ptEnd = RegularEndPt(ptStart,ptEnd);
  561. }
  562. rectGra.Rect = CreateValidRect(ptStart,ptEnd);
  563. Canvas.Instance.Update();//Update canvas
  564. }
  565. public override void OnMouseUp(MouseEventArgs e)
  566. {
  567. this.isDrawing = false;//Not Drawing
  568. }
  569. }
  570. //It inherits from RecaDrawTool(). It is used for drawing frame rectangle.
  571. //When we use mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  572. public class FrameRectDrawTool : RectDrawTool
  573. {
  574. /// <summary>
  575. ///
  576. /// </summary>
  577. public FrameRectDrawTool()
  578. :base(GraDrawMode.Frame)
  579. {
  580. }
  581. //Specify the draw type: frame rectangle
  582. public override GraDrawType Type
  583. {
  584. get
  585. {
  586. return GraDrawType.FrameRect;
  587. }
  588. }
  589. }
  590. //It inherit from RecaDrawTool(). It is used for drawing solid 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 FillRectDrawTool : RectDrawTool
  593. {
  594. public FillRectDrawTool()
  595. :base(GraDrawMode.Fill)
  596. {
  597. }
  598. //Specify the draw type: solid rectangle
  599. public override GraDrawType Type
  600. {
  601. get
  602. {
  603. return GraDrawType.FillRect;
  604. }
  605. }
  606. }
  607. //It inherit from GraDrawTool(). It is used for drawing rectangle actions.
  608. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  609. public abstract class EllipseDrawTool : GraDrawTool
  610. {
  611. public EllipseDrawTool(GraDrawMode drawMode)//Get the draw mode:frame or solid
  612. :base()
  613. {
  614. this.drawMode = drawMode;
  615. }
  616. protected GraDrawMode drawMode;//protected drawMode variable to save the draw mode
  617. private EllipseGra ellipseGra;//Save the rectangle
  618. private Point ptStart;//Start postion for drawing
  619. private Point ptPre; ////Temporary point use for round
  620. public override string Tip
  621. {
  622. get
  623. {
  624. return "Pressed 'Shift' key to draw a circle";
  625. }
  626. }
  627. public override void OnMouseDown(MouseEventArgs e)
  628. {
  629. this.isDrawing = true;//Drawing status
  630. ptStart = new Point(e.X,e.Y);//Get new start point
  631. ptStart = GetRoundPoint(ptStart);//Round the start point
  632. ptPre = ptStart;//Temporary point use for round
  633. //Convert the start position to original coordinate when you zoom in or zoom out
  634. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  635. //Construct a temporary rectangle to display
  636. ellipseGra = new EllipseGra(DrawEnvMgr.Instance.Pen,DrawEnvMgr.Instance.Brush,
  637. drawMode,new Rectangle(ptStart,new Size(0,0)));
  638. //Canvas.Instance.GraList.Add(ellipseGra);//Add to GraList
  639. //Canvas.Instance.Update();//Update canvas
  640. Canvas.Instance.AddGra(ellipseGra);
  641. }
  642. public override void OnMouseMove(MouseEventArgs e)
  643. {
  644. if(!this.isDrawing)//Not drawing
  645. return;
  646. Point ptEnd = new Point(e.X,e.Y);//Get end point of temporary rectangle
  647. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  648. if(ptEnd == ptPre)//Movement distance < 6
  649. return;
  650. ptPre = ptEnd;//Save end point as a temporary point
  651. //Convert the end position to orginal coordinate when you zoom in or zoom out
  652. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  653. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  654. {
  655. ptEnd = RegularEndPt(ptStart,ptEnd);
  656. }
  657. ellipseGra.Rect = CreateValidRect(ptStart,ptEnd);
  658. Canvas.Instance.Update();//Update canvas
  659. }
  660. public override void OnMouseUp(MouseEventArgs e)
  661. {
  662. this.isDrawing = false;//Not Drawing
  663. }
  664. }
  665. //It inherit from RecaDrawTool(). It is used for drawing frame rectangle.
  666. //When we use mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  667. public class FrameEllipseDrawTool : EllipseDrawTool
  668. {
  669. /// <summary>
  670. ///
  671. /// </summary>
  672. public FrameEllipseDrawTool()
  673. :base(GraDrawMode.Frame)
  674. {
  675. }
  676. //Specify the draw type: frame rectangle
  677. public override GraDrawType Type
  678. {
  679. get
  680. {
  681. return GraDrawType.FrameEllipse;
  682. }
  683. }
  684. }
  685. //It inherit from RecaDrawTool(). It is used for drawing solid 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 FillEllipseDrawTool : EllipseDrawTool
  688. {
  689. public FillEllipseDrawTool()
  690. :base(GraDrawMode.Fill)
  691. {
  692. }
  693. //Specify the draw type: solid rectangle
  694. public override GraDrawType Type
  695. {
  696. get
  697. {
  698. return GraDrawType.FillEllipse;
  699. }
  700. }
  701. }
  702. //It inherit from GraDrawTool(). It is used for insert image actions.
  703. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  704. public class ImageDrawTool : GraDrawTool
  705. {
  706. public ImageDrawTool()//Get the draw mode:frame or solid
  707. :base()
  708. {
  709. this.toolCursor = Cursors.Cross;
  710. }
  711. public override GraDrawType Type
  712. {
  713. get
  714. {
  715. return GraDrawType.Image;
  716. }
  717. }
  718. private RectGra rectGra;//Save the rectangle
  719. private Point ptStart;//Start postion for drawing
  720. private Point ptPre; ////Temporary point use for round
  721. public override void OnMouseDown(MouseEventArgs e)
  722. {
  723. this.isDrawing = true;//Drawing status
  724. ptStart = new Point(e.X,e.Y);//Get new start point
  725. ptStart = GetRoundPoint(ptStart);//Round the start point
  726. ptPre = ptStart;//Temporary point use for round
  727. //Convert the start position to original coordinate when you zoom in or zoom out
  728. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  729. //Construct a temporary rectangle to display
  730. rectGra = new RectGra(Pens.DarkBlue,null,
  731. GraDrawMode.Frame,new Rectangle(ptStart,new Size(0,0)));
  732. Canvas.Instance.GraList.Add(rectGra);//Add to GraList
  733. Canvas.Instance.Update();//Update canvas
  734. }
  735. public override void OnMouseMove(MouseEventArgs e)
  736. {
  737. if(!this.isDrawing)//Not drawing
  738. return;
  739. Point ptEnd = new Point(e.X,e.Y);//Get end point of temporary rectangle
  740. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  741. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  742. {
  743. ptEnd = RegularEndPt(ptStart,ptEnd);
  744. }
  745. if(ptEnd == ptPre)//Movement distance < 6
  746. return;
  747. ptPre = ptEnd;//Save end point as a temporary point
  748. //Convert the end position to original coordinate when you zoom in or zoom out
  749. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  750. rectGra.Rect = CreateValidRect(ptStart,ptEnd);
  751. Canvas.Instance.Update();//Update canvas
  752. }
  753. public override void OnMouseUp(MouseEventArgs e)
  754. {
  755. if(!this.isDrawing)
  756. return;
  757. this.isDrawing = false;//Not Drawing
  758. if(rectGra.Rect.Width <5 && rectGra.Rect.Height <5)
  759. return;
  760. Canvas.Instance.GraList.Remove(rectGra);
  761. OpenFileDialog dlg = new OpenFileDialog();
  762. dlg.Filter = "Bitmap files (*.bmp)|*.bmp|Jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
  763. dlg.FilterIndex = 1 ;
  764. dlg.RestoreDirectory = true ;
  765. if(dlg.ShowDialog() == DialogResult.OK)
  766. {
  767. Bitmap bmp = new Bitmap(dlg.FileName);
  768. if(bmp != null)
  769. {
  770. ImageGra imageGra = new ImageGra(bmp,rectGra.Rect);
  771. //Canvas.Instance.GraList.Add(imageGra);//Add to GraList
  772. //Canvas.Instance.Update();//Update canvas
  773. Canvas.Instance.AddGra(imageGra);
  774. }
  775. }
  776. this.rectGra = null;
  777. }
  778. }
  779. //It inherit from GraDrawTool(). It is used for insert text actions.
  780. //When we mouse to draw a rectangle, we need display some temporary rectangle before we decide the end point of the rectangle.
  781. public class TextDrawTool : GraDrawTool
  782. {
  783. protected TextDrawTool( )//Get the draw mode:frame or solid
  784. :base()
  785. {
  786. this.toolCursor = Cursors.Cross;
  787. }
  788. private static TextDrawTool instance;
  789. public static TextDrawTool Instance
  790. {
  791. get
  792. {
  793. if(instance == null)
  794. instance = new TextDrawTool();
  795. return instance;
  796. }
  797. }
  798. public override GraDrawType Type
  799. {
  800. get
  801. {
  802. return GraDrawType.Text;
  803. }
  804. }
  805. public override string Tip
  806. {
  807. get
  808. {
  809. return "Click 'Enter' key to stop,click 'Esc' Key to cancel";
  810. }
  811. }
  812. public override bool IsActive
  813. {
  814. get
  815. {
  816. return base.IsActive;
  817. }
  818. set
  819. {
  820. base.IsActive = value;
  821. if(!base.IsActive)
  822. {
  823. if(this.textBox != null)
  824. {
  825. this.EndTextEditing(true);
  826. }
  827. }
  828. }
  829. }
  830. private RectGra rectGra;//Save the rectangle
  831. private Point ptStart;//Start postion for drawing
  832. private Point ptEnd;
  833. private Point ptPre; ////Temporary point use for round
  834. private Point ptStartView;
  835. private Point ptEndView;
  836. public override void OnMouseDown(MouseEventArgs e)
  837. {
  838. if(this.isJustEditing)
  839. {
  840. Point pt = new Point(e.X,e.Y);
  841. if(!textBox.Bounds.Contains(pt))
  842. this.EndTextEditing(true);
  843. return ;
  844. }
  845. this.isDrawing = true;//Drawing status
  846. ptStartView = new Point(e.X,e.Y);//Get new start point
  847. ptStartView = GetRoundPoint(ptStartView);//Round the start point
  848. ptPre = ptStartView;//Temporary point use for round
  849. //Convert the start position to original coordinate when you zoom in or zoom out
  850. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStartView);
  851. //Construct a temporary rectangle to display
  852. rectGra = new RectGra(Pens.DarkBlue,null,
  853. GraDrawMode.Frame,new Rectangle(ptStart,new Size(0,0)));
  854. Canvas.Instance.GraList.Add(rectGra);//Add to GraList
  855. Canvas.Instance.Update();//Update canvas
  856. //
  857. }
  858. public override void OnMouseMove(MouseEventArgs e)
  859. {
  860. if(this.isJustEditing)
  861. return;
  862. if(!this.isDrawing)//Not drawing
  863. return;
  864. ptEndView = new Point(e.X,e.Y);//Get end point of temporary rectangle
  865. ptEndView = GetRoundPoint(ptEndView);//Round the end point. The min mouse movement distance is 6.
  866. if(ptEndView == ptPre)//Movement distance < 6
  867. return;
  868. ptPre = ptEndView;//Save end point as a temporary point
  869. //Convert the end position to original coordinate when you zoom in or zoom out
  870. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEndView);
  871. rectGra.Rect = CreateValidRect(ptStart,ptEnd);
  872. Canvas.Instance.Update();//Update canvas
  873. }
  874. private bool isJustEditing;
  875. private TextBox textBox;
  876. public override void OnMouseUp(MouseEventArgs e)
  877. {
  878. if(this.isJustEditing)
  879. return;
  880. if(!this.isDrawing)
  881. return;
  882. this.isDrawing = false;//Not Drawing
  883. if(rectGra.Rect.Width <5 && rectGra.Rect.Height <5)
  884. return;
  885. Canvas.Instance.GraList.Remove(rectGra);
  886. textBox = new TextBox();
  887. textBox.Multiline = true;
  888. textBox.BackColor = Canvas.Instance.BackColor;
  889. textBox.ForeColor = DrawEnvMgr.Instance.Color;
  890. Rectangle viewRect = CreateValidRect(ptStartView,ptEndView);
  891. float s = Canvas.Instance.MainView.ViewMatrix.Elements[0];
  892. textBox.Bounds = viewRect;
  893. Font f =DrawEnvMgr.Instance.Font;
  894. textBox.Font = new Font(f.Name,f.Size*s);
  895. Canvas.Instance.MainView.Controls.Add(textBox);
  896. textBox.Focus();
  897. textBox.KeyDown +=new KeyEventHandler(textBox_KeyDown);
  898. this.isJustEditing = true;
  899. //FontDialog dlg = new FontDialog(); //instantiating a new fontdialog box
  900. //dlg.Font = DrawEnvMgr.Instance.Font;
  901. //if(DialogResult.OK == dlg.ShowDialog())
  902. //{
  903. // DrawEnvMgr.Instance.Font = dlg.Font; // assigning the font selected by the user to DrawEnvMgr.Instance.Font
  904. //}
  905. }
  906. public void EndTextEditing(bool isValid)
  907. {
  908. if(this.textBox == null)
  909. return;
  910. this.isJustEditing = false;
  911. Canvas.Instance.MainView.Controls.Remove(textBox);
  912. if(isValid && !(textBox.Text == null || textBox.Text.Length == 0))
  913. {
  914. TextGra textGra = new TextGra(textBox.Text,DrawEnvMgr.Instance.Font,
  915. DrawEnvMgr.Instance.Brush,rectGra.Rect);
  916. //Canvas.Instance.GraList.Add(textGra);//Add to GraList
  917. //Canvas.Instance.Update();//Update canvas
  918. Canvas.Instance.AddGra(textGra);
  919. this.rectGra = null;
  920. }
  921. this.textBox = null;
  922. Canvas.Instance.Update();
  923. }
  924. private void textBox_KeyDown(object sender, KeyEventArgs e)
  925. {
  926. if(this.textBox == null)
  927. return;
  928. //Click 'Enter' when you complete input text
  929. if(e.KeyCode == Keys.Enter)
  930. {
  931. this.EndTextEditing(true);
  932. }
  933. else if(e.KeyCode == Keys.Escape)
  934. {
  935. this.EndTextEditing(false);
  936. }
  937. }
  938. }
  939. //It inherit from GraDrawTool(). It is used for drawing line actions.
  940. //When we mouse to draw a line, we need display some temporary lines before we decide the end point of the line
  941. public class LineDrawTool : GraDrawTool
  942. {
  943. public LineDrawTool()
  944. :base()
  945. {
  946. this.toolCursor = Cursors.Cross;
  947. }
  948. //Specify the draw type: line
  949. public override GraDrawType Type
  950. {
  951. get
  952. {
  953. return GraDrawType.Line;
  954. }
  955. }
  956. public override string Tip
  957. {
  958. get
  959. {
  960. return "Pressed 'Shift' key to draw horizontal or vertical line";
  961. }
  962. }
  963. private LineGra lineGra;//Save the start point,end point,color and width of line
  964. private Point ptStart;
  965. private Point ptPre;//Temporary point use for round
  966. public override void OnMouseDown(MouseEventArgs e)
  967. {
  968. this.isDrawing = true;//Drawing status
  969. ptStart = new Point(e.X,e.Y);//Start point
  970. ptStart = GetRoundPoint(ptStart);//Round point
  971. ptPre = ptStart;//Temporary point use for round
  972. //Convert the start position to original coordinate when you zoom in or zoom out
  973. Point pt = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  974. //Construct a temporary line to display
  975. lineGra = new LineGra(DrawEnvMgr.Instance.Pen,
  976. pt,pt);
  977. //Canvas.Instance.GraList.Add(lineGra);//Add to GraList
  978. //Canvas.Instance.Update();//Update canvas
  979. Canvas.Instance.AddGra(lineGra);
  980. }
  981. public override void OnMouseMove(MouseEventArgs e)
  982. {
  983. if(!this.isDrawing)//Drawing Status=false
  984. return;
  985. Point ptEnd = new Point(e.X,e.Y);//Temporary end point
  986. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  987. if((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
  988. {
  989. int sx = Math.Abs(ptEnd.X - ptStart.X);
  990. int sy = Math.Abs(ptEnd.Y - ptStart.Y);
  991. if(sy < sx)
  992. ptEnd.Y = ptStart.Y;
  993. else
  994. ptEnd.X = ptStart.X;
  995. }
  996. if( ptEnd == ptPre)//Movement distance < 6
  997. return;
  998. ptPre = ptEnd;//Save end point as a temporary point
  999. //Convert the end position to original coordinate when you zoom in or zoom out
  1000. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1001. lineGra.EndPt = ptEnd;//Set the end point to LineGra
  1002. Canvas.Instance.Update();//Update canvas
  1003. }
  1004. public override void OnMouseUp(MouseEventArgs e)
  1005. {
  1006. this.isDrawing = false;//Drawing status = false
  1007. }
  1008. }
  1009. //It inherit from GraDrawTool(). It is used for drawing lines actions.
  1010. //When we mouse to draw a line, we need display some temporary lines before we decide the end point of the line
  1011. public class LinesDrawTool : GraDrawTool
  1012. {
  1013. public LinesDrawTool()
  1014. :base()
  1015. {
  1016. this.ptList = new ArrayList();
  1017. this.toolCursor = Cursors.Cross;
  1018. }
  1019. //Specify the draw type: line
  1020. public override GraDrawType Type
  1021. {
  1022. get
  1023. {
  1024. return GraDrawType.Lines;
  1025. }
  1026. }
  1027. public override string Tip
  1028. {
  1029. get
  1030. {
  1031. return "Double click left mouse button to finish drawing lines";
  1032. }
  1033. }
  1034. private LinesGra linesGra;//Save the start point,end point,color and width of line
  1035. private ArrayList ptList;
  1036. public override void OnMouseDown(MouseEventArgs e)
  1037. {
  1038. Point ptStart = new Point(e.X,e.Y);//Start point
  1039. ptStart = GetRoundPoint(ptStart);//Round point
  1040. Point ptPre = ptStart;//Temporary point use for round
  1041. //Convert the start position to orginal coordinate when you zoom in or zoom out
  1042. Point pt = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  1043. if( !this.isDrawing)
  1044. {
  1045. //start creating lines graph element
  1046. this.isDrawing = true;//Drawing status
  1047. ptList.Clear();
  1048. ptList.Add(pt);
  1049. //Construct a temporary line to display
  1050. linesGra = new LinesGra(DrawEnvMgr.Instance.Pen,
  1051. new Point[]{pt});
  1052. Canvas.Instance.GraList.Add(linesGra);//Add to GraList
  1053. Canvas.Instance.IsModified = true;
  1054. }
  1055. else
  1056. {
  1057. ptList.Add(pt);
  1058. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1059. linesGra.Pts = pts;
  1060. }
  1061. Canvas.Instance.Update();//Update canvas
  1062. }
  1063. private Point ptPre;//Temproary point use for round
  1064. public override void OnMouseMove(MouseEventArgs e)
  1065. {
  1066. if(!this.isDrawing)//Drawing Status=false
  1067. return;
  1068. Point ptEnd = new Point(e.X,e.Y);//Temproary end point
  1069. ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  1070. if( ptEnd == ptPre)//Movement distance < 6
  1071. return;
  1072. ptPre = ptEnd;//Save end point as a temproary point
  1073. //Convert the end position to orginal coordinate when you zoom in or zoom out
  1074. ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1075. if(ptList.Count > 1)
  1076. ptList.RemoveAt(ptList.Count-1);
  1077. ptList.Add(ptEnd);
  1078. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1079. linesGra.Pts = pts;//Set the end point to LineGra
  1080. Canvas.Instance.Update();//Update canvas
  1081. }
  1082. public override void OnMouseUp(MouseEventArgs e)
  1083. {
  1084. //Do nothing
  1085. }
  1086. public override void OnDoubleClick(EventArgs e)
  1087. {
  1088. this.isDrawing = false;//Drawing status = false
  1089. }
  1090. }
  1091. //It inherit from GraDrawTool(). It is used for drawing lines actions.
  1092. //When we mouse to erase
  1093. public class EraseDrawTool : GraDrawTool
  1094. {
  1095. public EraseDrawTool()
  1096. :base()
  1097. {
  1098. this.ptList = new ArrayList();
  1099. this.toolCursor = CreateCursor("Icon.EraseCursor.cur");
  1100. //Cursor
  1101. }
  1102. //Specify the draw type: line
  1103. public override GraDrawType Type
  1104. {
  1105. get
  1106. {
  1107. return GraDrawType.Erase;
  1108. }
  1109. }
  1110. private ArrayList ptList;
  1111. private Point ptStart;
  1112. private Point ptStartView;
  1113. private Point ptPreView;//Temporary point use for round
  1114. public override void OnMouseDown(MouseEventArgs e)
  1115. {
  1116. ptStartView = new Point(e.X,e.Y);//Start point
  1117. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStartView);
  1118. if( !this.isDrawing)
  1119. {
  1120. //start creating lines graph element
  1121. Color eraseColor=Canvas.Instance.BackColor;
  1122. float sx = Canvas.Instance.MainView.ViewMatrix.Elements[0];
  1123. float w = sx*10;
  1124. this.erasePen = new Pen(eraseColor,w);
  1125. this.isDrawing = true;//Drawing status
  1126. ptList.Clear();
  1127. ptList.Add(ptStart);
  1128. ptPreView = ptStartView;
  1129. }
  1130. }
  1131. private Pen erasePen;
  1132. public override void OnMouseMove(MouseEventArgs e)
  1133. {
  1134. if(!this.isDrawing)//Drawing Status=false
  1135. return;
  1136. Point ptCurView = new Point(e.X,e.Y);//Temporary end point
  1137. //Convert the end position to original coordinate when you zoom in or zoom out
  1138. Point ptCur = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptCurView);
  1139. ptList.Add(ptCur);
  1140. using(Graphics g = Canvas.Instance.MainView.CreateGraphics())
  1141. {
  1142. Rectangle rc = new Rectangle(new Point(0,0),Canvas.Instance.Size);
  1143. rc = Canvas.Instance.MainView.ConvertCanvasToViewBitmapRect(rc);
  1144. Region r = new Region(rc);
  1145. Region old = g.Clip;
  1146. g.Clip = r;
  1147. g.DrawLine(this.erasePen,ptPreView,ptCurView);
  1148. g.Clip = old;
  1149. }
  1150. ptPreView = ptCurView;
  1151. }
  1152. public override void OnMouseUp(MouseEventArgs e)
  1153. {
  1154. if(this.isDrawing)
  1155. {
  1156. this.isDrawing = false;//Drawing status = false
  1157. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1158. EraseLinesGra linesGra = new EraseLinesGra(this.erasePen,pts);
  1159. Canvas.Instance.GraList.Add(linesGra);
  1160. Canvas.Instance.IsModified = true;
  1161. }
  1162. }
  1163. }
  1164. // public class EraseDrawTool : GraDrawTool
  1165. // {
  1166. //
  1167. // public EraseDrawTool()
  1168. // :base()
  1169. // {
  1170. // this.ptList = new ArrayList();
  1171. // toolCursor = CreateCursor("Icon.EraseCursor.cur");
  1172. //
  1173. // //Cursor
  1174. // }
  1175. //
  1176. // //Specify the draw type: line
  1177. // public override GraDrawType Type
  1178. // {
  1179. // get
  1180. // {
  1181. // return GraDrawType.Erase;
  1182. // }
  1183. // }
  1184. //
  1185. //
  1186. //
  1187. //
  1188. // private EraseLinesGra linesGra;//Save the start point,end point,color and width of line
  1189. //
  1190. // private ArrayList ptList;
  1191. //
  1192. //
  1193. //
  1194. // public override void OnMouseDown(MouseEventArgs e)
  1195. // {
  1196. // Point ptStart = new Point(e.X,e.Y);//Start point
  1197. // ptStart = GetRoundPoint(ptStart);//Round point
  1198. // Point ptPre = ptStart;//Temporary point use for round
  1199. // //Convert the start position to orginal coordinate when you zoom in or zoom out
  1200. // Point pt = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStart);
  1201. //
  1202. // if( !this.isDrawing)
  1203. // {
  1204. // //start creating lines graph element
  1205. //
  1206. // this.isDrawing = true;//Drawing status
  1207. // ptList.Clear();
  1208. // ptList.Add(pt);
  1209. //
  1210. //
  1211. // //Construct a temporary line to display
  1212. // Color color=Canvas.Instance.BackColor;
  1213. //
  1214. // linesGra = new EraseLinesGra(new Pen(color,10),
  1215. // new Point[]{pt});
  1216. //
  1217. // Canvas.Instance.GraList.Add(linesGra);//Add to GraList
  1218. // Canvas.Instance.IsModified = true;
  1219. // }
  1220. // else
  1221. // {
  1222. // ptList.Add(pt);
  1223. // Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1224. // linesGra.Pts = pts;
  1225. // }
  1226. //
  1227. // Canvas.Instance.Update();//Update canvas
  1228. //
  1229. // }
  1230. //
  1231. // private Point ptPre;//Temporary point use for round
  1232. //
  1233. //
  1234. // public override void OnMouseMove(MouseEventArgs e)
  1235. // {
  1236. // if(!this.isDrawing)//Drawing Status=false
  1237. // return;
  1238. // Point ptEnd = new Point(e.X,e.Y);//Temporary end point
  1239. // ptEnd = GetRoundPoint(ptEnd);//Round the end point. The min mouse movement distance is 6.
  1240. //
  1241. // if( ptEnd == ptPre)//Movement distance < 6
  1242. // return;
  1243. //
  1244. // ptPre = ptEnd;//Save end point as a temproary point
  1245. //
  1246. // //Convert the end position to orginal coordinate when you zoom in or zoom out
  1247. // ptEnd = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptEnd);
  1248. //
  1249. //
  1250. // ptList.Add(ptEnd);
  1251. //
  1252. // Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1253. //
  1254. // linesGra.Pts = pts;//Set the end point to LineGra
  1255. //
  1256. // Canvas.Instance.Update();//Update canvas
  1257. //
  1258. //
  1259. // }
  1260. //
  1261. //
  1262. // public override void OnMouseUp(MouseEventArgs e)
  1263. // {
  1264. // if(this.isDrawing)
  1265. // {
  1266. // this.isDrawing = false;//Drawing status = false
  1267. // // DrawEnvMgr.Instance.Pen.Color=color;
  1268. // // DrawEnvMgr.Instance.Pen.Width=width;
  1269. // }
  1270. // }
  1271. //
  1272. //
  1273. // }
  1274. //It inherit from GraDrawTool(). It is used for drawing lines actions.
  1275. //When we mouse to freehand drawing
  1276. public class FreehandDrawTool : GraDrawTool
  1277. {
  1278. public FreehandDrawTool()
  1279. :base()
  1280. {
  1281. this.ptList = new ArrayList();
  1282. this.toolCursor = Cursors.Cross;
  1283. //Cursor
  1284. }
  1285. //Specify the draw type: line
  1286. public override GraDrawType Type
  1287. {
  1288. get
  1289. {
  1290. return GraDrawType.Freehand;
  1291. }
  1292. }
  1293. private ArrayList ptList;
  1294. private Point ptStart;
  1295. private Point ptStartView;
  1296. private Point ptPreView;//Temporary point use for round
  1297. public override void OnMouseDown(MouseEventArgs e)
  1298. {
  1299. ptStartView = new Point(e.X,e.Y);//Start point
  1300. ptStart = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptStartView);
  1301. if( !this.isDrawing)
  1302. {
  1303. //start creating lines graph element
  1304. this.isDrawing = true;//Drawing status
  1305. ptList.Clear();
  1306. ptList.Add(ptStart);
  1307. ptPreView = ptStartView;
  1308. }
  1309. }
  1310. public override void OnMouseMove(MouseEventArgs e)
  1311. {
  1312. if(!this.isDrawing)//Drawing Status=false
  1313. return;
  1314. Point ptCurView = new Point(e.X,e.Y);//Temporary end point
  1315. //Convert the end position to orginal coordinate when you zoom in or zoom out
  1316. Point ptCur = Canvas.Instance.MainView.ConvertViewToCanvasPt(ptCurView);
  1317. ptList.Add(ptCur);
  1318. Pen p = DrawEnvMgr.Instance.Pen;
  1319. using(Graphics g = Canvas.Instance.MainView.CreateGraphics())
  1320. {
  1321. Rectangle rc = new Rectangle(new Point(0,0),Canvas.Instance.Size);
  1322. rc = Canvas.Instance.MainView.ConvertCanvasToViewBitmapRect(rc);
  1323. Region r = new Region(rc);
  1324. Region old = g.Clip;
  1325. g.Clip = r;
  1326. g.DrawLine(p,ptPreView,ptCurView);
  1327. g.Clip = old;
  1328. }
  1329. ptPreView = ptCurView;
  1330. }
  1331. public override void OnMouseUp(MouseEventArgs e)
  1332. {
  1333. if(this.isDrawing)
  1334. {
  1335. this.isDrawing = false;//Drawing status = false
  1336. Point[] pts = ptList.ToArray(typeof(Point)) as Point[];
  1337. FreehandLinesGra linesGra = new FreehandLinesGra(DrawEnvMgr.Instance.Pen,pts);
  1338. Canvas.Instance.GraList.Add(linesGra);
  1339. Canvas.Instance.IsModified = true;
  1340. }
  1341. }
  1342. }
  1343. }