回复 2楼 qw1161748579
我是用变量画的,画在了windows窗口里面。主要代码如下:
private void Frm_Main_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//如果按下鼠标左键
{
//如果选择文字输入,则打开strInput窗体
if (drawTool == drawTools.String)
{
Frm_Text inputBox = new Frm_Text();
inputBox.StartPosition = FormStartPosition.CenterParent;
if (inputBox.ShowDialog() == DialogResult.OK)
{
Graphics g = this.CreateGraphics();
Font theFont = this.Font;
g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
}
}
//如果开始绘制,则开始记录鼠标位置
else if ((isDrawing = !isDrawing) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}
private void Frm_Main_MouseMove(object sender, MouseEventArgs e)
{
Graphics g;
g = this.CreateGraphics();
if (isDrawing)//如果开始绘图
{
switch (drawTool)
{
case drawTools.None:
break;
case drawTools.Pen:
//从上一个点到当前点绘制线段
g.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.Line:
//首先恢复此次操作之前的图像,然后再添加Line
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.Ellipse:
//首先恢复此次操作之前的图像,然后再添加Ellipse
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.Rectangle:
//首先恢复此次操作之前的图像,然后再添加Rectangle
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.String:
break;
case drawTools.Rubber:
//用背景色绘制宽线段
g.DrawLine(new Pen(backColor, 100), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(backColor, 100), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.triangle:
//首先恢复此次操作之前的图像,然后再添加triangle
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
Pen mypen = new Pen(foreColor,1);
Point point1 = new Point(startPoint.X,e.Y);
Point point2 = new Point((startPoint.X+e.X)/2,startPoint.Y);
Point point3 = new Point(e.X,e.Y);
Point[] mypoints = {point1,point2,point3 };
g.DrawPolygon(mypen,mypoints);
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
}
}
}
private void Frm_Main_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
switch (drawTool)
{
case drawTools.Line://画线
ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case drawTools.Ellipse://画椭圆
ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case drawTools.Rectangle://画矩形
ig.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case drawTools.triangle://画三角形
Pen mypen = new Pen(foreColor,1);
Point point1 = new Point(startPoint.X,e.Y);
Point point2 = new Point((startPoint.X+e.X)/2,startPoint.Y);
Point point3 = new Point(e.X,e.Y);
Point[] mypoints = {point1,point2,point3 };
ig.DrawPolygon(mypen,mypoints);
break;
}
}
应该再写哪些代码,才能选中所画的图,并移动?