实现MSN的画板功能
MSN可以直接画画,然后传送给对方。我想自己实现一个类似的能直接画画的控件。
思路:
继承PictureBox
重写OnMouseDown事件处理程序:记录当前鼠标所在位置,保存为curPoint
重写OnMouseMove事件处理程序:记录当前鼠标位置,保存为newPoint,在curPoint和newPoint之间画线。
目前的效果是,当我把这个PictureBoxEx控件从ToolBox上拖动到Form中时,PictureBoxEx有个默认的大小。不管我怎么调整PictureBoxEx的大小,程序运行时,只有默认区域才能画图,其他区域则不能。
请问怎么解决呢?源代码如下:
public class PictureBoxEx : PictureBox
{
private int circleRadius = 1;
private Point curPoint;
private Pen pen;
public int CircleRadius
{
get { return circleRadius; }
set
{
pen.Width = value;
circleRadius = value;
}
}
public PictureBoxEx()
: base()
{
this.BackColor = Color.White;
pen = new Pen(Color.Blue, circleRadius);
this.Image = new Bitmap(this.Width, this.Height);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
Point newPoint = new Point(e.X, e.Y);
Graphics g = Graphics.FromImage(this.Image);
g.DrawLine(pen, this.curPoint, newPoint);
curPoint = newPoint;
this.Refresh();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
this.curPoint = new Point(e.X, e.Y);
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
或者提供另外的思路。
另外,我希望鼠标进入PictureBoxEx时候,鼠标样式被修改为一个小黑圆圈,这个怎么实现呢?