public partial class FormMove : Form
{
public FormMove()
{
InitializeComponent();
}
#region draw line
Point start; bool draw_down = false;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
draw_down = true;
start = e.Location;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
draw_down = false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (draw_down)
{
using (var g = this.CreateGraphics())
{
g.DrawLine(new Pen(Brushes.Red), start, e.Location);
this.Validate(true);
}
}
}
#endregion
}
Winform画图的简单方法
最新推荐文章于 2023-12-01 15:11:59 发布
该代码示例展示了一个C#窗体应用程序,用户在按下鼠标时从起点开始,在鼠标移动时绘制红色线条,松开鼠标时停止绘制。通过处理OnMouseDown,OnMouseUp和OnMouseMove事件实现这一功能。
摘要由CSDN通过智能技术生成