110,536
社区成员
发帖
与我相关
我的任务
分享
//这里只有线,其它的自己添加
enum Action
{
None,
Line
}
Action currentAction = Action.None;//当前命令
GraphicsPath gp = new GraphicsPath();//存诸图形,可以一并显示(红色)
GraphicsPath tempGP = new GraphicsPath();//临时,在画时的显示(蓝色)
private void buttonLine_Click(object sender, EventArgs e)//点击了画线按钮
{
this.currentAction = Action.Line;
}
private void buttonClear_Click(object sender, EventArgs e)//点击了清除按钮
{
gp.Reset();
tempGP.Reset();
this.Invalidate();
}
Point mouseDownPoint = new Point();//鼠标按下时的点
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || this.currentAction == Action.None)
return;
switch (this.currentAction)
{
case Action.None:
break;
case Action.Line:
mouseDownPoint.X = e.X;
mouseDownPoint.Y = e.Y;
break;
default:
break;
}
}
//随着鼠标的位置改变,正在画的图形也会跟着改,把它写到tempGP
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || this.currentAction == Action.None)
return;
tempGP.Reset();
switch (this.currentAction)
{
case Action.None:
break;
case Action.Line:
tempGP.AddLine(mouseDownPoint.X, mouseDownPoint.Y, e.X, e.Y);
this.Invalidate();
break;
default:
break;
}
}
//图形确定,加入gp
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || this.currentAction == Action.None)
return;
switch (this.currentAction)
{
case Action.None:
break;
case Action.Line:
gp.AddLine(mouseDownPoint.X, mouseDownPoint.Y, e.X, e.Y);
gp.CloseFigure();
tempGP.Reset();
this.Invalidate();
break;
default:
break;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawPath(Pens.Red, gp);
e.Graphics.DrawPath(Pens.Blue, tempGP);
}
private void roundButton_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath buttonPath =
new System.Drawing.Drawing2D.GraphicsPath();
// Set a new rectangle to the same size as the button's
// ClientRectangle property.
System.Drawing.Rectangle newRectangle = roundButton.ClientRectangle;
// Decrease the size of the rectangle.
newRectangle.Inflate(-10, -10);
// Draw the button's border.
e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);
// Increase the size of the rectangle to include the border.
newRectangle.Inflate( 1, 1);
// Create a circle within the new rectangle.
buttonPath.AddEllipse(newRectangle);
// Set the button's Region property to the newly created
// circle region.
roundButton.Region = new System.Drawing.Region(buttonPath);
}