123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /*************************************************************************************************
- **************************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
- {
- /// <summary>
- /// MainView is used to manage the panel.It inherit from panel.
- /// </summary>
- 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 组件设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器
- /// 修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- components = new System.ComponentModel.Container();
- }
- #endregion
-
- /// <summary>
- /// clear all the using resource
- /// </summary>
- 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; }
- }
- /// <summary>
- /// Panel OnPaint event
- /// </summary>
- /// <param name="e"></param>
- protected override void OnPaint(PaintEventArgs e)
- {
- this.Draw(e.Graphics);
- }
- /// <summary>
- /// refresh the MainView
- /// </summary>
- 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
- }
- }
- }
|