MainView.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*************************************************************************************************
  2. **************************File Name: MainView.cs, Version 1.0, 8 November 2004********************
  3. *****************************Author: Karthik Janaswami********************************************
  4. **This class mainly represents the functionalities of a panel. This panelis inherited from System
  5. **Panel.It defines functions which will be used to manage the bitmap and perform zoom in and zoom
  6. **out operations which will be done on the canvas.
  7. ** **
  8. ** **
  9. *************************************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.ComponentModel;
  13. using System.Drawing;
  14. using System.Drawing.Drawing2D;
  15. using System.Data;
  16. using System.Windows.Forms;
  17. using GraEditor.DrawTools;
  18. using GraEditor.Elements;
  19. namespace GraEditor
  20. {
  21. /// <summary>
  22. /// MainView is used to manage the panel.It inherit from panel.
  23. /// </summary>
  24. public class MainView : System.Windows.Forms.Panel
  25. {
  26. private System.ComponentModel.Container components = null;
  27. static MainView()
  28. {
  29. viewBitmap = new Bitmap(2000,2000); //Set the size of bitmap
  30. }
  31. public MainView()
  32. {
  33. InitializeComponent();//Initialize MainView
  34. this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer,
  35. true);
  36. //Change Bitmap to Graphics
  37. this.gViewBitmap = Graphics.FromImage(viewBitmap);
  38. //gViewBitmap.SmoothingMode = SmoothingMode.HighQuality;
  39. this.viewMatrix = new Matrix();//Use it for zoom in and zoom out
  40. }
  41. #region 组件设计器生成的代码
  42. /// <summary>
  43. /// 设计器支持所需的方法 - 不要使用代码编辑器
  44. /// 修改此方法的内容。
  45. /// </summary>
  46. private void InitializeComponent()
  47. {
  48. components = new System.ComponentModel.Container();
  49. }
  50. #endregion
  51. /// <summary>
  52. /// clear all the using resource
  53. /// </summary>
  54. protected override void Dispose( bool disposing )
  55. {
  56. if( disposing )
  57. {
  58. if(components != null)
  59. {
  60. components.Dispose();
  61. }
  62. }
  63. base.Dispose( disposing );
  64. }
  65. private Graphics gViewBitmap;
  66. private Matrix viewMatrix;//Matrix view for display
  67. public Matrix ViewMatrix
  68. {
  69. get { return viewMatrix; }
  70. set { viewMatrix = value; }
  71. }
  72. private static Bitmap viewBitmap;//Bitmap view
  73. public static Bitmap ViewBitmap
  74. {
  75. get { return viewBitmap; }
  76. //set { this.viewBitmap = value; }
  77. }
  78. /// <summary>
  79. /// Panel OnPaint event
  80. /// </summary>
  81. /// <param name="e"></param>
  82. protected override void OnPaint(PaintEventArgs e)
  83. {
  84. this.Draw(e.Graphics);
  85. }
  86. /// <summary>
  87. /// refresh the MainView
  88. /// </summary>
  89. public void UpdateView()
  90. {
  91. using(Graphics g = this.CreateGraphics())
  92. {
  93. this.Draw(g);
  94. }
  95. }
  96. private void Draw(Graphics g)
  97. {
  98. Canvas c = Canvas.Instance;//Get the canvas instance
  99. this.gViewBitmap.Clear(this.BackColor);//refresh mainView by background color
  100. Point pos = this.AutoScrollPosition;//get scroll bar postion
  101. Matrix m = this.viewMatrix.Clone() as Matrix;//Get the matrix
  102. m.Translate(pos.X,pos.Y,MatrixOrder.Append);//zoom
  103. this.gViewBitmap.Transform = m;//Set new matrix to gViewBitmap
  104. Rectangle rect = new Rectangle(new Point(0,0),c.Size);
  105. Region oldClip = this.gViewBitmap.Clip;
  106. Region newClip = new Region(rect);
  107. this.gViewBitmap.Clip = newClip;
  108. //refresh mainView by background color
  109. using(SolidBrush b = new SolidBrush(c.BackColor))
  110. {
  111. this.gViewBitmap.FillRectangle(b,rect);
  112. }
  113. c.Draw(gViewBitmap);//Draw on Canvas
  114. this.gViewBitmap.Clip = oldClip;
  115. newClip.Dispose();
  116. g.DrawImage(viewBitmap,0,0);//draw the view bitmap buffer to mainview.
  117. }
  118. //Zoom view by s times
  119. public void ScaleView(float s)
  120. {
  121. this.viewMatrix.Scale(s,s,MatrixOrder.Append);//Zoom in x and y direction by s times
  122. this.UpdateViewMatrix();//Update MainView matrix
  123. }
  124. //Update MainView matrix
  125. public void UpdateViewMatrix()
  126. {
  127. Canvas c = Canvas.Instance;//Get the instance of canvas
  128. float scale = this.viewMatrix.Elements[0];//Get scale
  129. Size scaleCanasSize = new Size((int)(c.Size.Width*scale),
  130. (int)(c.Size.Height*scale));//Canvas size after zoom
  131. this.AutoScrollMinSize = scaleCanasSize;//Get the min position of Scroll
  132. this.UpdateView();//Refresh MainView
  133. }
  134. public void ResetScale()//Reset the scale of MainView
  135. {
  136. this.viewMatrix.Reset();//Reset the matrix to orginal
  137. this.UpdateViewMatrix();//Update Matrix
  138. }
  139. //Convert the postion of point ptCanvas to zoom coordiante when zoom in or zoom out
  140. public Point ConvertCanvasToViewBitmapPt(Point ptCanvas)
  141. {
  142. Point[] pts = new Point[]{ ptCanvas };
  143. this.viewMatrix.TransformPoints(pts);//Convert to new position
  144. Point pt = pts[0];
  145. //Point ptView = new Point(pt.X+this.AutoScrollPosition.X,
  146. // pt.Y+this.AutoScrollPosition.Y);
  147. return pt;
  148. }
  149. //Convert the postion and size of rectangle rCanvas to zoom coordiante when zoom in or zoom out
  150. public Rectangle ConvertCanvasToViewBitmapRect(Rectangle rCanvas)
  151. {
  152. //Convert the start point of rectangle to new position
  153. Point pt1 = this.ConvertCanvasToViewBitmapPt(rCanvas.Location);
  154. //Convert the end point of rectangle to new position
  155. Point pt2 = this.ConvertCanvasToViewBitmapPt(new Point(rCanvas.Right,rCanvas.Bottom));
  156. Rectangle r = new Rectangle(pt1,new Size(pt2.X-pt1.X,
  157. pt2.Y-pt1.Y));//Convert the size to new size
  158. return r;
  159. }
  160. //Convert the postion of point ptView to original coordiante
  161. public Point ConvertViewToCanvasPt(Point ptView)
  162. {
  163. Point pt = new Point(ptView.X-this.AutoScrollPosition.X,
  164. ptView.Y-this.AutoScrollPosition.Y);
  165. Point[] pts = new Point[]{ pt };
  166. //Invert transform from zoom to orginial
  167. using(Matrix m = this.viewMatrix.Clone())
  168. {
  169. m.Invert();
  170. m.TransformPoints(pts);
  171. }
  172. return pts[0];
  173. }
  174. //Convert the postion and size of rectangle rView to original coordiante
  175. public Rectangle ConvertViewToCanvasRect(Rectangle rView)
  176. {
  177. //Convert the start point of rectangle to original coordinate
  178. Point pt1 = this.ConvertViewToCanvasPt(rView.Location);
  179. //Convert the end point of rectangle to original coordinate
  180. Point pt2 = this.ConvertViewToCanvasPt(new Point(rView.Right,rView.Bottom));
  181. Rectangle r = new Rectangle(pt1,new Size(pt2.X-pt1.X,
  182. pt2.Y-pt1.Y));//Convert the size to original size
  183. return r;
  184. }
  185. //OnMouseDown event of Panel
  186. protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
  187. {
  188. if((e.Button & MouseButtons.Left ) != MouseButtons.Left)
  189. return;
  190. GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool
  191. if(drawTool != null)
  192. drawTool.OnMouseDown(e);//Use the OnMouseDown event of active draw tool to deal with this event
  193. }
  194. //OnMouseMove event of Panel
  195. protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
  196. {
  197. GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool
  198. if(drawTool != null)
  199. drawTool.OnMouseMove(e);//Use the OnMouseMove event of active draw tool to deal with this event
  200. base.OnMouseMove(e);
  201. }
  202. //OnMouseUp event of Panel
  203. protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
  204. {
  205. if((e.Button & MouseButtons.Left ) != MouseButtons.Left)
  206. return;
  207. GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool
  208. if(drawTool != null)
  209. drawTool.OnMouseUp(e);//Use the OnMouseUp event of active draw tool to deal with this event
  210. }
  211. protected override void OnDoubleClick(EventArgs e)
  212. {
  213. GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool
  214. if(drawTool != null)
  215. drawTool.OnDoubleClick(e);//Use the OnMouseUp event of active draw tool to deal with this event
  216. }
  217. //Pop-up menu of Panel
  218. public void contextMenu1_Popup(object sender, System.EventArgs e)
  219. {
  220. Point pt = Cursor.Position;//current position of cursor
  221. pt = this.PointToClient(pt);//convert screen position to panel coordinate
  222. SelectDrawTool.Instance.ContextMenuMousePos = pt;//Pop-up menu for SelectDrawTool
  223. }
  224. }
  225. }