DrawArea.cs
上传用户:sxsgcs
上传日期:2013-10-21
资源大小:110k
文件大小:10k
源码类别:

CAD

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DocToolkit;
  9. namespace DrawTools
  10. {
  11.     partial class DrawArea : UserControl
  12.     {
  13.         #region Constructor
  14.         public DrawArea()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         #endregion Constructor
  19.         #region Enumerations
  20.         public enum DrawToolType
  21.         {
  22.             Pointer,
  23.             Rectangle,
  24.             Ellipse,
  25.             Line,
  26.             Polygon,
  27.             NumberOfDrawTools
  28.         };
  29.         #endregion
  30.         #region Members
  31.         private GraphicsList graphicsList;    // list of draw objects
  32.         // (instances of DrawObject-derived classes)
  33.         private DrawToolType activeTool;      // active drawing tool
  34.         private Tool[] tools;                 // array of tools
  35.         // Information about owner form
  36.         private MainForm owner;
  37.         private DocManager docManager;
  38.         private ContextMenuStrip m_ContextMenu;
  39.         private UndoManager undoManager;
  40.         #endregion
  41.         #region Properties
  42.         /// <summary>
  43.         /// Reference to the owner form
  44.         /// </summary>
  45.         public MainForm Owner
  46.         {
  47.             get
  48.             {
  49.                 return owner;
  50.             }
  51.             set
  52.             {
  53.                 owner = value;
  54.             }
  55.         }
  56.         /// <summary>
  57.         /// Reference to DocManager
  58.         /// </summary>
  59.         public DocManager DocManager
  60.         {
  61.             get
  62.             {
  63.                 return docManager;
  64.             }
  65.             set
  66.             {
  67.                 docManager = value;
  68.             }
  69.         }
  70.         /// <summary>
  71.         /// Active drawing tool.
  72.         /// </summary>
  73.         public DrawToolType ActiveTool
  74.         {
  75.             get
  76.             {
  77.                 return activeTool;
  78.             }
  79.             set
  80.             {
  81.                 activeTool = value;
  82.             }
  83.         }
  84.         /// <summary>
  85.         /// List of graphics objects.
  86.         /// </summary>
  87.         public GraphicsList GraphicsList
  88.         {
  89.             get
  90.             {
  91.                 return graphicsList;
  92.             }
  93.             set
  94.             {
  95.                 graphicsList = value;
  96.             }
  97.         }
  98.         /// <summary>
  99.         /// Return True if Undo operation is possible
  100.         /// </summary>
  101.         public bool CanUndo
  102.         {
  103.             get
  104.             {
  105.                 if ( undoManager != null )
  106.                 {
  107.                     return undoManager.CanUndo;
  108.                 }
  109.                 return false;
  110.             }
  111.         }
  112.         /// <summary>
  113.         /// Return True if Redo operation is possible
  114.         /// </summary>
  115.         public bool CanRedo
  116.         {
  117.             get
  118.             {
  119.                 if (undoManager != null)
  120.                 {
  121.                     return undoManager.CanRedo;
  122.                 }
  123.                 return false;
  124.             }
  125.         }
  126.         #endregion
  127.         #region Other Functions
  128.         /// <summary>
  129.         /// Initialization
  130.         /// </summary>
  131.         /// <param name="owner"></param>
  132.         /// <param name="docManager"></param>
  133.         public void Initialize(MainForm owner, DocManager docManager)
  134.         {
  135.             SetStyle(ControlStyles.AllPaintingInWmPaint |
  136.                 ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
  137.             // Keep reference to owner form
  138.             this.Owner = owner;
  139.             this.DocManager = docManager;
  140.             // set default tool
  141.             activeTool = DrawToolType.Pointer;
  142.             // create list of graphic objects
  143.             graphicsList = new GraphicsList();
  144.             // Create undo manager
  145.             undoManager = new UndoManager(graphicsList);
  146.             // create array of drawing tools
  147.             tools = new Tool[(int)DrawToolType.NumberOfDrawTools];
  148.             tools[(int)DrawToolType.Pointer] = new ToolPointer();
  149.             tools[(int)DrawToolType.Rectangle] = new ToolRectangle();
  150.             tools[(int)DrawToolType.Ellipse] = new ToolEllipse();
  151.             tools[(int)DrawToolType.Line] = new ToolLine();
  152.             tools[(int)DrawToolType.Polygon] = new ToolPolygon();
  153.         }
  154.         /// <summary>
  155.         /// Add command to history.
  156.         /// </summary>
  157.         public void AddCommandToHistory(Command command)
  158.         {
  159.             undoManager.AddCommandToHistory(command);
  160.         }
  161.         /// <summary>
  162.         /// Clear Undo history.
  163.         /// </summary>
  164.         public void ClearHistory()
  165.         {
  166.             undoManager.ClearHistory();
  167.         }
  168.         /// <summary>
  169.         /// Undo
  170.         /// </summary>
  171.         public void Undo()
  172.         {
  173.             undoManager.Undo();
  174.             Refresh();
  175.         }
  176.         /// <summary>
  177.         /// Redo
  178.         /// </summary>
  179.         public void Redo()
  180.         {
  181.             undoManager.Redo();
  182.             Refresh();
  183.         }
  184.         /// <summary>
  185.         /// Set dirty flag (file is changed after last save operation)
  186.         /// </summary>
  187.         public void SetDirty()
  188.         {
  189.             DocManager.Dirty = true;
  190.         }
  191.         /// <summary>
  192.         /// Right-click handler
  193.         /// </summary>
  194.         /// <param name="e"></param>
  195.         private void OnContextMenu(MouseEventArgs e)
  196.         {
  197.             // Change current selection if necessary
  198.             Point point = new Point(e.X, e.Y);
  199.             int n = GraphicsList.Count;
  200.             DrawObject o = null;
  201.             for (int i = 0; i < n; i++)
  202.             {
  203.                 if (GraphicsList[i].HitTest(point) == 0)
  204.                 {
  205.                     o = GraphicsList[i];
  206.                     break;
  207.                 }
  208.             }
  209.             if (o != null)
  210.             {
  211.                 if (!o.Selected)
  212.                     GraphicsList.UnselectAll();
  213.                 // Select clicked object
  214.                 o.Selected = true;
  215.             }
  216.             else
  217.             {
  218.                 GraphicsList.UnselectAll();
  219.             }
  220.             Refresh();      // in the case selection was changed
  221.             // Show context menu.
  222.             // Context menu items are filled from owner form Edit menu items.
  223.             m_ContextMenu = new ContextMenuStrip();
  224.             int nItems = owner.ContextParent.DropDownItems.Count;
  225.             // Read Edit items and move them to context menu.
  226.             // Since every move reduces number of items, read them in reverse order.
  227.             // To get items in direct order, insert each of them to beginning.
  228.             for (int i = nItems - 1; i >= 0; i--)
  229.             {
  230.                 m_ContextMenu.Items.Insert(0, owner.ContextParent.DropDownItems[i]);
  231.             }
  232.             // Show context menu for owner form, so that it handles items selection.
  233.             // Convert point from this window coordinates to owner's coordinates.
  234.             point.X += this.Left;
  235.             point.Y += this.Top;
  236.             m_ContextMenu.Show(owner, point);
  237.             Owner.SetStateOfControls();  // enable/disable menu items
  238.             // Context menu is shown, but owner's Edit menu is now empty.
  239.             // Subscribe to context menu Closed event and restore items there.
  240.             m_ContextMenu.Closed += delegate(object sender, ToolStripDropDownClosedEventArgs args)
  241.             {
  242.                 if (m_ContextMenu != null)      // precaution
  243.                 {
  244.                     nItems = m_ContextMenu.Items.Count;
  245.                     for (int k = nItems - 1; k >= 0; k--)
  246.                     {
  247.                         owner.ContextParent.DropDownItems.Insert(0, m_ContextMenu.Items[k]);
  248.                     }
  249.                 }
  250.             };
  251.         }
  252.         #endregion
  253.         #region Event Handlers
  254.         /// <summary>
  255.         /// Draw graphic objects and 
  256.         /// group selection rectangle (optionally)
  257.         /// </summary>
  258.         private void DrawArea_Paint(object sender, PaintEventArgs e)
  259.         {
  260.             SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
  261.             e.Graphics.FillRectangle(brush,
  262.                 this.ClientRectangle);
  263.             if (graphicsList != null)
  264.             {
  265.                 graphicsList.Draw(e.Graphics);
  266.             }
  267.             //DrawNetSelection(e.Graphics);
  268.             brush.Dispose();
  269.         }
  270.         /// <summary>
  271.         /// Mouse down.
  272.         /// Left button down event is passed to active tool.
  273.         /// Right button down event is handled in this class.
  274.         /// </summary>
  275.         private void DrawArea_MouseDown(object sender, MouseEventArgs e)
  276.         {
  277.             if (e.Button == MouseButtons.Left)
  278.                 tools[(int)activeTool].OnMouseDown(this, e);
  279.             else if (e.Button == MouseButtons.Right)
  280.                 OnContextMenu(e);
  281.         }
  282.         /// <summary>
  283.         /// Mouse move.
  284.         /// Moving without button pressed or with left button pressed
  285.         /// is passed to active tool.
  286.         /// </summary>
  287.         private void DrawArea_MouseMove(object sender, MouseEventArgs e)
  288.         {
  289.             if (e.Button == MouseButtons.Left || e.Button == MouseButtons.None)
  290.                 tools[(int)activeTool].OnMouseMove(this, e);
  291.             else
  292.                 this.Cursor = Cursors.Default;
  293.         }
  294.         /// <summary>
  295.         /// Mouse up event.
  296.         /// Left button up event is passed to active tool.
  297.         /// </summary>
  298.         private void DrawArea_MouseUp(object sender, MouseEventArgs e)
  299.         {
  300.             if (e.Button == MouseButtons.Left)
  301.                 tools[(int)activeTool].OnMouseUp(this, e);
  302.         }
  303.         #endregion Event Handlers
  304.     }
  305. }