y1:
y2;
w=Math.Abs(x1-x2);h=Math.Abs(y1-y2);
ig.DrawEllipse(pen,x,y,w,h);
pictureBox1.Image=bt;
break;
//画直线
case4:
x2=e.X;y2=e.Y;
ig.Clear(bgcolor);
ig.DrawImage(sbt,0,0);
ig.DrawLine(pen,x1,y1,x2,y2);
pictureBox1.Image=bt;
break;
//画弧线
case5:
x2=e.X;y2=e.Y;
x3=x1*2-x2;y3=y2;
x4=x3;y4=y3*2-y2;
ig.Clear(bgcolor);
ig.DrawImage(sbt,0,0);
ig.DrawBezier(pen,x1,y1,x2,y2,x3,y3,x4,y4);
pictureBox1.Image=bt;
break;
将颜色设置为白色,起到橡皮的作用
//橡皮
case6:
x2=e.X;y2=e.Y;
eraser.Color=bgcolor;
ig.DrawLine(eraser,x1,y1,x2,y2);
x1=x2;y1=y2;
pictureBox1.Image=bt;
break;
}
}
}
在picturebox的mouseup事件中确定画板中有已做图像时,撤销和重做图像才可用
privatevoidpictureBox1_MouseUp(objectsender,MouseEventArgse)
{
f=false;
//当stack中包含的元素不为0时撤销和重做图标可用
if(now.Count!
=0)
{
撤销ToolStripMenuItem.Enabled=true;
撤销toolStripButton.Enabled=true;
}
if(history.Count!
=0)
{
重做ToolStripMenuItem.Enabled=true;
重做toolStripButton.Enabled=true;
}
调用mousemove事件中编写的绘画功能,并当选中图标时,图标的背景颜色会改变
privatevoidtoolStripButton1_Click(objectsender,EventArgse)
{
type=1;
pictureBox1.Cursor=curpen;//鼠标变为铅笔的图案
toolStripButton1.BackColor=Color.LawnGreen;
toolStripButton2.BackColor=Color.Transparent;
toolStripButton3.BackColor=Color.Transparent;
toolStripButton4.BackColor=Color.Transparent;
toolStripButton5.BackColor=Color.Transparent;
toolStripButton11.BackColor=Color.Transparent;
toolStripButton9.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="三¨y角?
形?
";
}
privatevoidtoolStripButton2_Click(objectsender,EventArgse)
{
type=2;
pictureBox1.Cursor=curpen;
toolStripButton2.BackColor=Color.LawnGreen;
toolStripButton1.BackColor=Color.Transparent;
toolStripButton3.BackColor=Color.Transparent;
toolStripButton4.BackColor=Color.Transparent;
toolStripButton5.BackColor=Color.Transparent;
toolStripButton11.BackColor=Color.Transparent;
toolStripButton9.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="矩形";
}
privatevoidtoolStripButton3_Click(objectsender,EventArgse)
{
type=3;
pictureBox1.Cursor=curpen;
toolStripButton3.BackColor=Color.LawnGreen;
toolStripButton2.BackColor=Color.Transparent;
toolStripButton1.BackColor=Color.Transparent;
toolStripButton4.BackColor=Color.Transparent;
toolStripButton5.BackColor=Color.Transparent;
toolStripButton11.BackColor=Color.Transparent;
toolStripButton9.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="圆";
}
privatevoidtoolStripButton4_Click(objectsender,EventArgse)
{
type=4;
pictureBox1.Cursor=curpen;
toolStripButton4.BackColor=Color.LawnGreen;
toolStripButton2.BackColor=Color.Transparent;
toolStripButton3.BackColor=Color.Transparent;
toolStripButton1.BackColor=Color.Transparent;
toolStripButton5.BackColor=Color.Transparent;
toolStripButton11.BackColor=Color.Transparent;
toolStripButton9.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="直线";
}
privatevoidtoolStripButton5_Click(objectsender,EventArgse)
{
toolStripButton5.BackColor=Color.LawnGreen;
toolStripButton2.BackColor=Color.Transparent;
toolStripButton3.BackColor=Color.Transparent;
toolStripButton4.BackColor=Color.Transparent;
toolStripButton1.BackColor=Color.Transparent;
toolStripButton11.BackColor=Color.Transparent;
toolStripButton9.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="曲线";
type=5;
pictureBox1.Cursor=curpen;
}
privatevoidtoolStripButton11_Click(objectsender,EventArgse)
{
type=0;
pictureBox1.Cursor=curpen;
toolStripButton11.BackColor=Color.LawnGreen;
toolStripButton2.BackColor=Color.Transparent;
toolStripButton3.BackColor=Color.Transparent;
toolStripButton4.BackColor=Color.Transparent;
toolStripButton1.BackColor=Color.Transparent;
toolStripButton5.BackColor=Color.Transparent;
toolStripButton9.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="铅笔;
}
privatevoidtoolStripButton9_Click(objectsender,EventArgse)
{
type=6;
pictureBox1.Cursor=curearser;//鼠标变为橡皮的图案
toolStripButton9.BackColor=Color.LawnGreen;
toolStripButton2.BackColor=Color.Transparent;
toolStripButton3.BackColor=Color.Transparent;
toolStripButton4.BackColor=Color.Transparent;
toolStripButton1.BackColor=Color.Transparent;
toolStripButton11.BackColor=Color.Transparent;
toolStripButton5.BackColor=Color.Transparent;
toolStripStatusLabel2.Text="橡皮";
}
}
用堆栈方法实现撤销的功能,机上一步
privatevoid撤销ToolStripMenuItem_Click(objectsender,EventArgse)
{
Bitmapmjl=newBitmap(bt);
now.Push(mjl);//将对象移动到stack的顶部
bt=history.Pop();//移除并返回位于Stack顶部的对象
pictureBox1.Image=bt;
ig=Graphics.FromImage(bt);
pictureBox1.Refresh();
//如果stack中的元素为0则撤销图标不可用
if(history.Count==0)
{
撤销ToolStripMenuItem.Enabled=false;
}
}
用同样的方法实现重做功能,即下一步
privatevoid重做toolStripButton_Click(objectsender,EventArgse)
{
Bitmapmjl=newBitmap(bt);
history.Push(mjl);
bt=now.Pop();
pictureBox1.Image=bt;
ig=Graphics.FromImage(bt);
pictureBox1.Refresh();
if(now.Count==0)
{
重做toolStripButton.Enabled=false;
}
}
调用colordialog,用于选择画笔的颜色,并且当选中某个颜色时,颜色选择按钮的背景颜色会变成对应的颜色
privatevoidtoolStripButton10_Click(objectsender,EventArgse)
{
if(colorDialog1.ShowDialog()==DialogResult.OK)
{
pen.Color=colorDialog1.Color;
if(colorDialog1.Color!
=Color.Black)
toolStripButton10.BackColor=colorDialog1.Color;
else
toolStripButton10.BackColor=Color.Silver;
}
}
对于几个常用颜色按钮的设置
privatevoidtoolStripButton6_Click(objectsender,EventArgse)
{
pen.Color=Color.Red;
}
privatevoidtoolStripButton7_Click(objectsender,EventArgse)
{
pen.Color=Color.Green;
}
privatevoidtoolStripButton8_Click(objectsender,EventArgse)
{
pen.Color=Color.Yellow;
}
设置pen的width属性从而改变画笔的宽度
privatevoid磅ToolStripMenuItem_Click(objectsender,EventA