using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public partial class MyDrawBoxCtrl : PictureBox
{
public MyDrawBoxCtrl()
{
InitializeComponent();
}
public bool IsDraw
{
get { return isdraw; }
set { isdraw = value; }
}
bool isdraw = false;
bool init = false;
protected override void OnCreateControl()
{
base.OnCreateControl();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (!init)
{
img = new Bitmap(this.BackgroundImage, this.Width, this.Height);
grap = Graphics.FromImage(img);
this.Image = img;
init = true;
}
}
public Color DrawColor
{
get { return _drawcolor; }
set { _drawcolor = value; }
}
public int LineWidth
{
get { return _linewidth; }
set { _linewidth = value; }
}
private int _linewidth = 2;
private Graphics grap;
private Image img = null;
private List<Image> list = new List<Image>();
private Color _drawcolor = Color.Blue;
private Point p1, p2;//定义两个点(启点,终点)
private static bool drawing = false;//设置一个启动标志
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
p1 = new Point(e.X, e.Y);
p2 = new Point(e.X, e.Y);
drawing = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.Enabled)
{
if (e.Button == MouseButtons.Left)
{
if (drawing)
{
Point currentPoint = new Point(e.X, e.Y);
Graphics g = this.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawLine(new Pen(_drawcolor, _linewidth), p2, currentPoint);
grap.DrawLine(new Pen(_drawcolor, _linewidth), p2, currentPoint);
p2.X = currentPoint.X;
p2.Y = currentPoint.Y;
}
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
drawing = false;
base.OnMouseUp(e);
if (this.Enabled)
{
try
{
isdraw = true;
this.Image = img;
if (list.Count < 30) //最大保存30次画图操作,这里可适当调大
{
list.Add((Image)img.Clone());
}
else
{
list.RemoveAt(1);
list.Add((Image)img.Clone());
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Image为空");
}
}
}
/// <summary>
/// 绘图前加载背景图
/// </summary>
/// <param name="dataImage"></param>
public void Load(Image dataImage)
{
img = new Bitmap(dataImage, this.Width, this.Height);
grap = Graphics.FromImage(img);
this.Image = img;
list.Add((Image)dataImage.Clone());
isdraw = true;
}
/// <summary>
/// 撤销上一次的绘图
/// </summary>
public void Undo()
{
if (!this.Enabled)
return;
if (list.Count > 1)
{
list.RemoveAt(list.Count - 1);
img = new Bitmap(list[list.Count - 1], this.Width, this.Height);
}
else
{
list.Clear();
img = new Bitmap(this.BackgroundImage, this.Width, this.Height);
}
grap = Graphics.FromImage(img);
this.Image = img;
}
/// <summary>
/// 清除所有绘图
/// </summary>
public void Clear()
{
if (!this.Enabled)
return;
this.Refresh();
list.Clear();
img = new Bitmap(this.BackgroundImage, this.Width, this.Height);
grap = Graphics.FromImage(img);
this.Image = img;
}
}