C#的System.Drawing 命名空间提供了对 GDI+ 基本图形功能的访问
重点在于获取Graphics对象,例如:
Graphics g = panel1.CreateGraphics
事实上CreateGraphics继承自Control, 即基本每一种控件都有这个方法
Control.CreateGraphics
在pannel、form上画图都一样,这里以pictureBox为例。DrawRectangle函数为例画矩形,其他形状不在这里考虑,自己尝试很简单
画圆是画椭圆,只需g.DrawEllipse后两个int参数width,height要设置相等,同时前两个int参数并不是圆心而是左上角的坐标,没有自带的circle函数只能自己封装
回到正题:
网上给的都是MouseDown MouseMove MouseUp Paint事件相关的代码,非常的简单。
using System.Drawing;
bool bDrawStart = false;
Point pointStart = Point.Empty;
Point pointContinue = Point.Empty;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (bDrawStart)
{
bDrawStart = false;
}
else
{
bDrawStart = true;
pointStart = e.Location;
}
}
private void pictureBox1_Mouse