using System.Data;
using System.Runtime.InteropServices;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ESss.EGraphics
{
//[Serializable()]
public partial class DrawingCanvas : UserControl
{
static private Size ImageSize = new Size(12, 12);
#region Enumerations(枚举)
public enum GraphicToolType
{
Pointer,
Checkpoint, // 测点
Line,
Polyline,
Rectangle,
Ellipse,
Polygon,
Picture, // 图片
//Oscillation, // 振动
//Temperature, // 温度
//Analog, // 模拟量
};
#endregion
#region Properties
public Form Owner { get; set; }
public Rectangle MultiSelRectangle { get; set; }
public bool DrawMultiSelRectangle{get; set;}
//
// 图形工具数组
//
public IToolGraphic[] GraphicTools { get; protected set; }
public bool Modified
{
get
{
if (!this.Workspace.Visible) return false;
if (this.Workspace.Modified) return true;
return this.GraphicList.Modified;
}
}
private GraphicToolType toolType = GraphicToolType.Pointer;
public GraphicToolType ActiveTool
{
get{ return toolType; }
set
{
if (value != GraphicToolType.Pointer && !this.AllowAdd)
{
value = GraphicToolType.Pointer;
}
toolType = value;
}
}
//
// 图形对象列表
//
public GraphicList GraphicList{ get; set;}
public Size WorkspaceSize
{
get { return this.Workspace.Size; }
set { this.Workspace.Size = value; }
}
//
//
//
public DrawingCanvasPanel Workspace
{
get;
private set;
}
public bool AllowAdd{ get; set; }
private bool bAllowEdit = false;
public bool AllowEdit
{
get { return bAllowEdit; }
set
{
if (this.GraphicList != null)
{
this.GraphicList.AllowEdit(value);
}
bAllowEdit = value;
}
}
#endregion
public DrawingCanvas()
{
this.InitializeComponent();
this.GraphicList = new GraphicList();
this.Workspace = new DrawingCanvasPanel(this);
this.WorkspaceSize = new Size(800, 600);
this.Controls.Add(Workspace);
this.AutoScroll = true;
}
#region User Defined events(用户定义的事件)
public sealed class MouseGraphicEventArgs : MouseEventArgs
{
public GraphicObject Graphic { get; set; }
public MouseGraphicEventArgs(GraphicObject o, EventArgs args)
: base(MouseButtons.None, 0, 0, 0, 0)
{
this.Graphic = o;
}
public MouseGraphicEventArgs(GraphicObject o, MouseEventArgs args)
: base(args.Button, args.Clicks, args.X, args.Y, args.Delta)
{
this.Graphic = o;
}
public MouseGraphicEventArgs(GraphicObject o, MouseButtons button, int clicks, int x, int y, int delta)
: base(button, clicks, x, y, delta)
{
this.Graphic = o;
}
}
public event MouseEventHandler WorkspaceClick;
public event EventHandler<MouseGraphicEventArgs> GraphicClick;
//public event EventHandler<MouseGraphicEventArgs> MouseEnterGraphic;
public event EventHandler<MouseGraphicEventArgs> MouseLeaveGraphic;
public event EventHandler<MouseGraphicEventArgs> MouseHoverGraphic;
public sealed class GraphicEventArgs : EventArgs
{
public GraphicObject o{ get; private set; }
public GraphicEventArgs(GraphicObject obj)
{
this.o = obj;
}
}
public event EventHandler<GraphicEventArgs> BeginDrawGraphic;
public event EventHandler<GraphicEventArgs> EndDrawGraphic;
public event EventHandler<GraphicEventArgs> GraphicUpdated;
public event EventHandler<GraphicEventArgs> GraphicRemoved;
#endregion
protected override void OnInvalidated(InvalidateEventArgs e)
{
base.OnInvalidated(e);
this.Workspace.Invalidate(e.InvalidRect);
}
#region Other Functions
// 初始化
public void Initialize(Form owner)
{
// base.DoubleBuffered = true;
// SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
this.Owner = owner;
this.ActiveTool = GraphicToolType.Pointer;
//this.GraphicList = new GraphicList();
this.AllowAdd = true;
this.AllowEdit = true;
this.Parent.Resize += new EventHandler(Parent_SizeChanged);
this.Parent.Padding = new Padding(3);
this.ParentForm.Resize += new EventHandler(Parent_SizeChanged);
this.Resize += new EventHandler(Parent_SizeChanged);
// Initialize the GraphicTools
this.GraphicTools = new IToolGraphic[Enum.GetValues(typeof(GraphicToolType)).Length];
this.GraphicTools[(int)GraphicToolType.Checkpoint] = new ToolChechpoint();
this.GraphicTools[(int)GraphicToolType.Pointer] = new ToolPointer();
this.GraphicTools[(int)GraphicToolType.Rectangle] = new ToolRectangle();
this.GraphicTools[(int)GraphicToolType.Line] = new ToolLine();
this.GraphicTools[(int)GraphicToolType.Picture] = new ToolPicture();
}
void Parent_SizeChanged(object sender, EventArgs e)
{
//this.Size = this.Parent.Size;
this.ResizeWS();
}
public void DrawMultiSelection(System.Drawing.Graphics g)
{
if ( ! DrawMultiSelRectangle )
return;
ControlPaint.DrawFocusRectangle(g, MultiSelRectangle, Color.Black, Color.Transparent);
}
#endregion
#region Serialize methods(同步方法)
public void Serialize(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this.GraphicList);
formatter.Serialize(stream, this.WorkspaceSize);
formatter.Serialize(stream, this.Workspace.BackColor);
formatter.Serialize(stream, this.Workspace.BackgroundImage != null);
if (this.Workspace.BackgroundImage != null)
{
formatter.Serialize(stream, this.Workspace.BackgroundImage);
}
formatter.Serialize(stream, this.Workspace.BackgroundImageLayout);
this.Workspace.Modified = false;
this.Workspace.EnablePlayAnimate = true;
this.GraphicList.Modified = false;
foreach (Object o in this.GraphicList.Graphics) (o as GraphicObject).Modified = false;
}
public void Deserialize(Stream stream)
{
try
{
if ((this.Workspace.BackgroundImage != null) && ImageAnimator.CanAnimate(this.Workspace.BackgroundImage))
{
//this.Workspace.
ImageAnimator.StopAnimate(this.Workspace.BackgroundImage, this.Workspace.gif_FrameChanged);
}
BinaryFormatter formatter = new BinaryFormatter();
this.GraphicList = (GraphicList)formatter.Deserialize(stream);
this.WorkspaceSize = (Size)formatter.Deserialize(stream);
this.Workspace.BackColor = (Color)formatter.Deserialize(stream);
bool bHasBackgroundImage = (bool)formatter.Deserialize(stream);
if (bHasBackgroundImage)
this.Workspace.BackgroundImage = (Image)formatter.Deserialize(stream);
else
this.Workspace.BackgroundImage = null;
this.Workspace.EnablePlayAnimate = true;
this.Workspace.BackgroundImageLayout = (ImageLayout)formatter.Deserialize(stream);
this.ResizeWS();
this.Workspace.Modified = false;
this.GraphicList.Modified = false;
foreach (Object o in this.GraphicList.Graphics) (o as GraphicObject).Modified = false;
}
catch (Exception)
{
DialogResult result = MessageBox.Show("读取设备图形失败,是否重新建立设备图形?", "错误" , MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
Serialize(new MemoryStream());
this.Workspace.Modified = true;
}
}
}
#endregion
#region Graphic checkpoint query(图形检查点查询)
public List<GraphicCheckpoint> GetAllCheckpoints()
{
var result = from GraphicObject graphic in this.GraphicList.Graphics
let cp = graphic as GraphicCheckpoint
where (cp != null)
select cp;
List<GraphicCheckpoint> checkpoints = new List<GraphicCheckpoint>(result);
return checkpoints;
}
//public List<GraphicCheckpoint> GetCheckpoints(int type)
//{
// Type SelectType;
// if (type == InspectItemType.Analog) SelectType = typeof(GraphicCheckpointA);
// else if (type == InspectItemType.Oscillation) SelectType = typeof(GraphicCheckpointO);
// else if (type == InspectItemType.Templature) SelectType = typeof(GraphicCheckpointT);
// else throw new InvalidEnumArgumentException();
// var result = from GraphicObject graphic in this.GraphicList.Graphics
// let cp = graphic as GraphicCheckpoint
// where (cp != null) && (cp.GetType() == SelectType)
// select cp;
// List<GraphicCheckpoint> checkpoints = new List<GraphicCheckpoint>(result);
// return checkpoints;
//}
public GraphicCheckpoint GetCheckpoint(int checkpointID)
{
var result = from GraphicObject graphic in this.GraphicList.Graphics
let cp = graphic as GraphicCheckpoint
where (cp != null) && (cp.CheckpointID == checkpointID)
select cp;
return result.SingleOrDefault<GraphicCheckpoint>();
}
#endregion
public void FireEvent_GraphicRemoved(GraphicObject obj)
{
if (this.GraphicRemoved != null) this.GraphicRemoved(this, new GraphicEventArgs(obj));
}
internal void FireEvent_BeginDrawGraphic(GraphicObject obj)
{
if (this.BeginDrawGraphic != null) this.BeginDrawGraphic(this, new GraphicEventArgs(obj));
}
internal void FireEvent_EndDrawGraphic(GraphicObject obj)
{
if (this.EndDrawGraphic != null) this.EndDrawGraphic(this, new GraphicEventArgs(obj));
}
internal void FireEvent_GraphicUpated(GraphicObject obj)
{
if (this.GraphicUpdated != null) this.GraphicUpdated(this, new GraphicEventArgs(obj));
}
internal void FireEvent_GraphicClick(GraphicObject o, MouseEventArgs e)
{
if (this.GraphicClick != null) this.GraphicClick(this, new MouseGraphicEventArgs(o, e));
}
internal void FireEvent_MouseHoverGraphic(GraphicObject o, MouseEventArgs e)
{
// Fire the selected object item event.
if (this.MouseHoverGraphic != null) this.MouseHoverGraphic(this, new MouseGraphicEventArgs(o, e));
}
internal void FireEvent_MouseLeaveGraphic(Object o, EventArgs e)
{
// Fire the selected object item event.
if (this.MouseLeaveGraphic != null) this.MouseLeaveGraphic(this, new MouseGraphicEventArgs(null, e));
}
internal void FireEvent_WorkspaceClick(MouseEventArgs e)
{
// Fire the selected workspace event.
if (this.WorkspaceClick != null) this.WorkspaceClick(this.Workspace, e);
}
public bool SelectGraphicObject(GraphicCheckpoint checkP)
{
bool b = this.GraphicList.SelectObject(checkP);
if (b) this.Refresh();
return b;
}
protected override void OnPaintBackground(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(this.BackColor) )
{
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
if ((this.BackgroundImage != null)&&(this.Tag == null) )
{
Bitmap bmp = new Bitmap(this.BackgroundImage);
Int32 left = (this.Width - bmp.Width) / 2;
Int32 top = (this.Height - bmp.Height) / 2;
Rectangle rect = new Rectangle(left, top, bmp.Width, bmp.Height);
bmp.MakeTransparent();
e.Graphics.DrawImage(bmp, rect);
}
}
internal void ResizeWS()
{
this.AutoScrollMinSize = new Size(this.Workspace.Width + this.Workspace.Margin.Horizontal, this.Workspace.Height + this.Workspace.Margin.Vertical);
if (this.Width < (this.Workspace.Margin.Horizontal + this.Workspace.Width) )
{
this.Workspace.Left = this.Workspace.Margin.Left - this.HorizontalScroll.Value;
}
else
{
this.Workspace.Left = this.Workspace.Margin.Left + (this.Width - this.Workspace.Width - this.Workspace.Margin.Horizontal) / 2;
}
if (this.Height < (this.Workspace.Margin.Vertical + this.Workspace.Height))
{
this.Workspace.Top = this.Workspace.Margin.Top - this.VerticalScroll.Value;
}
else
{
this.Workspace.Top = this.Workspace.Margin.Top + (this.Height - this.Workspace.Height - this.Workspace.Margin.Vertical) / 2;
}
}
}
}