|
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using BizDraw.Tools;
using BizDraw.Core;
using BizDraw.Objects;
namespace BizDraw.Controls
{
[System.Runtime.InteropServices.ComVisible(true)]
/// <summary>
/// Working area.
/// Handles mouse input and draws graphics objects.
/// </summary>
public class DrawArea : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private float _zoom = 1;
#region Constructor, Dispose
public DrawArea()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
this.KeyDown += new KeyEventHandler(DrawArea_KeyDown);
}
void DrawArea_KeyDown(object sender, KeyEventArgs e)
{
if (this.Document == null)
return;
bool shouldRefresh = false;
switch (e.KeyData)
{
case Keys.Delete :
this.Document.DeleteSelection();
shouldRefresh = true;
break;
case Keys.Escape :
this.Document.Items.UnselectAll();
shouldRefresh = true;
break;
case (Keys)( Keys.A | Keys.Control ) :
if (e.Control )
{
this.Document.Items.SelectAll();
shouldRefresh = true;
}
break;
}
if (shouldRefresh)
this.Refresh();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// DrawArea
//
this.Name = "DrawArea";
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DrawArea_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawArea_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DrawArea_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawArea_MouseDown);
this.DoubleClick += new EventHandler(DrawArea_DoubleClick);
Initialize();
}
void DrawArea_DoubleClick(object sender, EventArgs e)
{
ActiveTool.OnDoubleClick(this, e);
}
#endregion
#region Members
private Document document;
private Tool activeTool ; // active drawing tool
// group selection rectangle
private Rectangle netRectangle;
private bool drawNetRectangle = false;
// Information about owner form
#endregion
#region Properties
[Browsable(false)]
[System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
/// <summary>
/// Group selection rectangle. Used for drawing.
/// </summary>
public Rectangle NetRectangle
{
get
{
return netRectangle;
}
set
{
netRectangle = value;
}
}
[System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
/// <summary>
/// Flas is set to true if group selection rectangle should be drawn.
/// </summary>
public bool DrawNetRectangle
{
get
{
return drawNetRectangle;
}
set
{
drawNetRectangle = value;
}
}
[System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
/// <summary>
/// Active drawing tool.
/// </summary>
public Tool ActiveTool
{
get
{
return activeTool;
}
set
{
activeTool = value;
}
}
[System.ComponentModel.ReadOnly(true)]
[Browsable(false)]
/// <summary>
/// List of graphics objects.
/// </summary>
public Document Document
{
get
{
return document ;
}
set
{
document = value;
}
}
public float Zoom
{
get
{
return _zoom;
}
set
{
_zoom = value;
}
}
#endregion
#region Event Handlers
/// <summary>
/// Draw graphic objects and
/// group selection rectangle (optionally)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrawArea_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.ScaleTransform(Zoom, Zoom);
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
e.Graphics.FillRectangle(brush,
this.ClientRectangle);
if ( this.Document != null )
{
this.document.Items.Draw(e.Graphics);
}
DrawNetSelection(e.Graphics);
brush.Dispose();
}
/// <summary>
/// Mouse down.
/// Left button down event is passed to active tool.
/// Right button down event is handled in this class.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrawArea_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ActiveTool.OnMouseDown(this, GetScaledMouseEvent(e));
}
else if (e.Button == MouseButtons.Right)
OnContextMenu(e);
}
MouseEventArgs GetScaledMouseEvent( MouseEventArgs e)
{
return new MouseEventArgs(e.Button, e.Clicks, Convert.ToInt32(e.X / Zoom), Convert.ToInt32(e.Y / Zoom), Convert.ToInt32(e.Delta / Zoom));
}
/// <summary>
/// Mouse move.
/// Moving without button pressed or with left button pressed
/// is passed to active tool.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrawArea_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left || e.Button == MouseButtons.None )
ActiveTool.OnMouseMove(this, GetScaledMouseEvent(e));
else
this.Cursor = Cursors.Default;
}
/// <summary>
/// Mouse up event.
/// Left button up event is passed to active tool.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrawArea_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left )
ActiveTool.OnMouseUp(this, GetScaledMouseEvent(e));
}
#endregion
#region Other Functions
/// <summary>
/// Initialization
/// </summary>
/// <param name="owner"></param>
/// <param name="docManager"></param>
public void Initialize()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
// set default tool
activeTool = new ToolPointer();
// create list of graphic objects
document = new Document();
// create array of drawing tools
}
/// <summary>
/// Draw group selection rectangle
/// </summary>
/// <param name="g"></param>
public void DrawNetSelection(Graphics g)
{
if ( ! DrawNetRectangle )
return;
ControlPaint.DrawFocusRectangle(g, NetRectangle, Color.Black, Color.Transparent);
}
/// <summary>
/// Right-click handler
/// </summary>
/// <param name="e"></param>
private void OnContextMenu(MouseEventArgs e)
{
// Change current selection if necessary
Point point = new Point(e.X, e.Y);
int n = this.Document.Items.Count;
DrawObject o = null;
for ( int i = 0; i < n; i++ )
{
if (this.Document.Items[i].HitTest(point) == 0)
{
o = this.Document.Items[i];
break;
}
}
if ( o != null )
{
if ( ! o.Selected )
this.Document.Items.UnselectAll();
// Select clicked object
o.SetSelectedState(true);
}
else
{
this.Document.Items.UnselectAll();
}
Refresh();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here