/************************************************************************************************* **************************File Name: MainView.cs, Version 1.0, 8 November 2004******************** *****************************Author: Karthik Janaswami******************************************** **This class mainly represents the functionalities of a panel. This panelis inherited from System **Panel.It defines functions which will be used to manage the bitmap and perform zoom in and zoom **out operations which will be done on the canvas. ** ** ** ** *************************************************************************************************/ using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Data; using System.Windows.Forms; using GraEditor.DrawTools; using GraEditor.Elements; namespace GraEditor { /// /// MainView is used to manage the panel.It inherit from panel. /// public class MainView : System.Windows.Forms.Panel { private System.ComponentModel.Container components = null; static MainView() { viewBitmap = new Bitmap(2000,2000); //Set the size of bitmap } public MainView() { InitializeComponent();//Initialize MainView this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); //Change Bitmap to Graphics this.gViewBitmap = Graphics.FromImage(viewBitmap); //gViewBitmap.SmoothingMode = SmoothingMode.HighQuality; this.viewMatrix = new Matrix();//Use it for zoom in and zoom out } #region 组件设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// private void InitializeComponent() { components = new System.ComponentModel.Container(); } #endregion /// /// clear all the using resource /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } private Graphics gViewBitmap; private Matrix viewMatrix;//Matrix view for display public Matrix ViewMatrix { get { return viewMatrix; } set { viewMatrix = value; } } private static Bitmap viewBitmap;//Bitmap view public static Bitmap ViewBitmap { get { return viewBitmap; } //set { this.viewBitmap = value; } } /// /// Panel OnPaint event /// /// protected override void OnPaint(PaintEventArgs e) { this.Draw(e.Graphics); } /// /// refresh the MainView /// public void UpdateView() { using(Graphics g = this.CreateGraphics()) { this.Draw(g); } } private void Draw(Graphics g) { Canvas c = Canvas.Instance;//Get the canvas instance this.gViewBitmap.Clear(this.BackColor);//refresh mainView by background color Point pos = this.AutoScrollPosition;//get scroll bar postion Matrix m = this.viewMatrix.Clone() as Matrix;//Get the matrix m.Translate(pos.X,pos.Y,MatrixOrder.Append);//zoom this.gViewBitmap.Transform = m;//Set new matrix to gViewBitmap Rectangle rect = new Rectangle(new Point(0,0),c.Size); Region oldClip = this.gViewBitmap.Clip; Region newClip = new Region(rect); this.gViewBitmap.Clip = newClip; //refresh mainView by background color using(SolidBrush b = new SolidBrush(c.BackColor)) { this.gViewBitmap.FillRectangle(b,rect); } c.Draw(gViewBitmap);//Draw on Canvas this.gViewBitmap.Clip = oldClip; newClip.Dispose(); g.DrawImage(viewBitmap,0,0);//draw the view bitmap buffer to mainview. } //Zoom view by s times public void ScaleView(float s) { this.viewMatrix.Scale(s,s,MatrixOrder.Append);//Zoom in x and y direction by s times this.UpdateViewMatrix();//Update MainView matrix } //Update MainView matrix public void UpdateViewMatrix() { Canvas c = Canvas.Instance;//Get the instance of canvas float scale = this.viewMatrix.Elements[0];//Get scale Size scaleCanasSize = new Size((int)(c.Size.Width*scale), (int)(c.Size.Height*scale));//Canvas size after zoom this.AutoScrollMinSize = scaleCanasSize;//Get the min position of Scroll this.UpdateView();//Refresh MainView } public void ResetScale()//Reset the scale of MainView { this.viewMatrix.Reset();//Reset the matrix to orginal this.UpdateViewMatrix();//Update Matrix } //Convert the postion of point ptCanvas to zoom coordiante when zoom in or zoom out public Point ConvertCanvasToViewBitmapPt(Point ptCanvas) { Point[] pts = new Point[]{ ptCanvas }; this.viewMatrix.TransformPoints(pts);//Convert to new position Point pt = pts[0]; //Point ptView = new Point(pt.X+this.AutoScrollPosition.X, // pt.Y+this.AutoScrollPosition.Y); return pt; } //Convert the postion and size of rectangle rCanvas to zoom coordiante when zoom in or zoom out public Rectangle ConvertCanvasToViewBitmapRect(Rectangle rCanvas) { //Convert the start point of rectangle to new position Point pt1 = this.ConvertCanvasToViewBitmapPt(rCanvas.Location); //Convert the end point of rectangle to new position Point pt2 = this.ConvertCanvasToViewBitmapPt(new Point(rCanvas.Right,rCanvas.Bottom)); Rectangle r = new Rectangle(pt1,new Size(pt2.X-pt1.X, pt2.Y-pt1.Y));//Convert the size to new size return r; } //Convert the postion of point ptView to original coordiante public Point ConvertViewToCanvasPt(Point ptView) { Point pt = new Point(ptView.X-this.AutoScrollPosition.X, ptView.Y-this.AutoScrollPosition.Y); Point[] pts = new Point[]{ pt }; //Invert transform from zoom to orginial using(Matrix m = this.viewMatrix.Clone()) { m.Invert(); m.TransformPoints(pts); } return pts[0]; } //Convert the postion and size of rectangle rView to original coordiante public Rectangle ConvertViewToCanvasRect(Rectangle rView) { //Convert the start point of rectangle to original coordinate Point pt1 = this.ConvertViewToCanvasPt(rView.Location); //Convert the end point of rectangle to original coordinate Point pt2 = this.ConvertViewToCanvasPt(new Point(rView.Right,rView.Bottom)); Rectangle r = new Rectangle(pt1,new Size(pt2.X-pt1.X, pt2.Y-pt1.Y));//Convert the size to original size return r; } //OnMouseDown event of Panel protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) { if((e.Button & MouseButtons.Left ) != MouseButtons.Left) return; GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool if(drawTool != null) drawTool.OnMouseDown(e);//Use the OnMouseDown event of active draw tool to deal with this event } //OnMouseMove event of Panel protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) { GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool if(drawTool != null) drawTool.OnMouseMove(e);//Use the OnMouseMove event of active draw tool to deal with this event base.OnMouseMove(e); } //OnMouseUp event of Panel protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) { if((e.Button & MouseButtons.Left ) != MouseButtons.Left) return; GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool if(drawTool != null) drawTool.OnMouseUp(e);//Use the OnMouseUp event of active draw tool to deal with this event } protected override void OnDoubleClick(EventArgs e) { GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;//Get the active draw tool if(drawTool != null) drawTool.OnDoubleClick(e);//Use the OnMouseUp event of active draw tool to deal with this event } //Pop-up menu of Panel public void contextMenu1_Popup(object sender, System.EventArgs e) { Point pt = Cursor.Position;//current position of cursor pt = this.PointToClient(pt);//convert screen position to panel coordinate SelectDrawTool.Instance.ContextMenuMousePos = pt;//Pop-up menu for SelectDrawTool } } }