实现MSN的画板功能

bineon 2006-04-02 11:36:55
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时候,鼠标样式被修改为一个小黑圆圈,这个怎么实现呢?
...全文
289 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
syeerzy 2006-04-03
  • 打赏
  • 举报
回复
API:
HCURSOR SetCursor(HCURSOR hCursor);

hCursor:光标的句柄,该光标由CreateCursor函数载入。如果该参数为NULL,则该光标从屏幕上移开。在Windows95中该光标的宽和高是GetSysfemMefirics 函数的返回值SM_CXCURSOR和观_CYCURSOR,并且光标的位深必须和显示器的位深相匹配,或者光标是单色的。

返回值:如果有前一个光标,则返回值是前光标的句柄;如果没有前光标,则返回值是NUL。


该函数确定光标的形状。可以把光标设置成一个十字就行了.如果你一定要自己绘制的而不是系统光标,请使用软鼠标吧....自己绘制
bineon 2006-04-03
  • 打赏
  • 举报
回复
现在的进展是重写如下方法:
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged (e);
img = (Image)this.Image.Clone();
this.Image = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(this.Image);
g.DrawImage(img, this.Left,this.Top);
}
这样能够解决问题。但是发现当将该控件弄到较大界面后,画图,然后缩小该控件,这时,其余的图画会消失。比如最大化窗口,画图,然后缩小窗口大小,这时图画将消失部分。应该怎么解决呢?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧