123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- 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
- {
-
-
-
- public class MainView : System.Windows.Forms.Panel
- {
-
- private System.ComponentModel.Container components = null;
- static MainView()
- {
- viewBitmap = new Bitmap(2000,2000);
- }
- public MainView()
- {
-
- InitializeComponent();
- this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer,
- true);
-
- this.gViewBitmap = Graphics.FromImage(viewBitmap);
-
- this.viewMatrix = new Matrix();
-
- }
- #region 组件设计器生成的代码
-
-
-
-
- private void InitializeComponent()
- {
- components = new System.ComponentModel.Container();
- }
- #endregion
-
-
-
-
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
-
- private Graphics gViewBitmap;
- private Matrix viewMatrix;
- public Matrix ViewMatrix
- {
- get { return viewMatrix; }
- set { viewMatrix = value; }
- }
- private static Bitmap viewBitmap;
- public static Bitmap ViewBitmap
- {
- get { return viewBitmap; }
-
- }
-
-
-
-
- protected override void OnPaint(PaintEventArgs e)
- {
- this.Draw(e.Graphics);
- }
-
-
-
- public void UpdateView()
- {
- using(Graphics g = this.CreateGraphics())
- {
- this.Draw(g);
- }
- }
- private void Draw(Graphics g)
- {
- Canvas c = Canvas.Instance;
-
- this.gViewBitmap.Clear(this.BackColor);
-
- Point pos = this.AutoScrollPosition;
- Matrix m = this.viewMatrix.Clone() as Matrix;
- m.Translate(pos.X,pos.Y,MatrixOrder.Append);
- this.gViewBitmap.Transform = m;
- Rectangle rect = new Rectangle(new Point(0,0),c.Size);
- Region oldClip = this.gViewBitmap.Clip;
- Region newClip = new Region(rect);
- this.gViewBitmap.Clip = newClip;
-
- using(SolidBrush b = new SolidBrush(c.BackColor))
- {
- this.gViewBitmap.FillRectangle(b,rect);
- }
- c.Draw(gViewBitmap);
- this.gViewBitmap.Clip = oldClip;
- newClip.Dispose();
-
- g.DrawImage(viewBitmap,0,0);
- }
-
- public void ScaleView(float s)
- {
- this.viewMatrix.Scale(s,s,MatrixOrder.Append);
- this.UpdateViewMatrix();
-
- }
-
- public void UpdateViewMatrix()
- {
- Canvas c = Canvas.Instance;
- float scale = this.viewMatrix.Elements[0];
- Size scaleCanasSize = new Size((int)(c.Size.Width*scale),
- (int)(c.Size.Height*scale));
-
- this.AutoScrollMinSize = scaleCanasSize;
-
- this.UpdateView();
- }
- public void ResetScale()
- {
- this.viewMatrix.Reset();
- this.UpdateViewMatrix();
- }
-
- public Point ConvertCanvasToViewBitmapPt(Point ptCanvas)
-
- {
- Point[] pts = new Point[]{ ptCanvas };
- this.viewMatrix.TransformPoints(pts);
- Point pt = pts[0];
-
-
- return pt;
- }
-
- public Rectangle ConvertCanvasToViewBitmapRect(Rectangle rCanvas)
-
- {
-
- Point pt1 = this.ConvertCanvasToViewBitmapPt(rCanvas.Location);
-
- 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));
- return r;
- }
-
- public Point ConvertViewToCanvasPt(Point ptView)
-
- {
- Point pt = new Point(ptView.X-this.AutoScrollPosition.X,
- ptView.Y-this.AutoScrollPosition.Y);
- Point[] pts = new Point[]{ pt };
-
-
- using(Matrix m = this.viewMatrix.Clone())
- {
- m.Invert();
- m.TransformPoints(pts);
-
- }
- return pts[0];
- }
-
- public Rectangle ConvertViewToCanvasRect(Rectangle rView)
-
- {
-
- Point pt1 = this.ConvertViewToCanvasPt(rView.Location);
-
- 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));
- return r;
- }
-
- protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
- {
- if((e.Button & MouseButtons.Left ) != MouseButtons.Left)
- return;
- GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;
- if(drawTool != null)
- drawTool.OnMouseDown(e);
- }
-
- protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
- {
- GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;
- if(drawTool != null)
- drawTool.OnMouseMove(e);
- base.OnMouseMove(e);
- }
-
- protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
- {
- if((e.Button & MouseButtons.Left ) != MouseButtons.Left)
- return;
- GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;
- if(drawTool != null)
- drawTool.OnMouseUp(e);
- }
- public void OnKeyDownTool(KeyEventArgs e)
- {
- GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;
- if (drawTool != null)
- drawTool.OnKeyDown(e);
- }
- public void OnKeyUpTool(KeyEventArgs e)
- {
- GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;
- if (drawTool != null)
- drawTool.OnKeyUp(e);
- }
-
- protected override void OnDoubleClick(EventArgs e)
- {
- GraDrawTool drawTool = GraDrawToolMgr.Instance.ActiveDrawTool;
- if(drawTool != null)
- drawTool.OnDoubleClick(e);
- }
-
- public void contextMenu1_Popup(object sender, System.EventArgs e)
- {
- Point pt = Cursor.Position;
- pt = this.PointToClient(pt);
- SelectDrawTool.Instance.ContextMenuMousePos = pt;
- }
- }
- }
|