C#画直线的代码!(和windows画图工具一样的效果*)
private void Drawing_MouseDown(object sender, MouseEventArgs e)
{
if ((isDrawing = !isDrawing) == true)//判断是否开始绘图
{
startPoint = new Point(e.X, e.Y);
}
}
private void Drawing_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = this.Drawing.CreateGraphics();
if (isDrawing)
{
switch (drawTool)
{
case drawTools.Line:
using (Pen p = new Pen(foreColor, 1))
{
graphics.DrawLine(p, startPoint, endPoint);
}
break;
}
}
private void Drawing_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;//标识变量
graphics = this.Drawing.CreateGraphics();
switch (drawTool)
{
case drawTools.Line:
using (Pen p = new Pen(foreColor, 1))
{
graphics.DrawLine(p, startPoint, new Point(e.X,e.Y));
}
break;
}
}
}
我没有单写类,需不需要单写个类啊?DDA算法是解决我这个问题的么? 展开
我这里是画直线和画曲线的两种都有,楼主看看,错不了的,代码如下:
namespace Case02_1
{
public partial class Form1 : Form
{
int startX; //获取鼠标起始点的X坐标
int startY; //获取鼠标起始点的Y坐标
Graphics g; //定义Graphics对象实例
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = Color.Snow;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
Pen p = new Pen(Color.Red , 4);
if (radioButton1.Checked == true)
{
g.DrawLine(p, startX, startY, e.X, e.Y);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startX = e.X;
startY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
Pen p = new Pen(Color.Blue , 2);
if (radioButton2.Checked == true)
{
g.DrawRectangle(p, e.X, e.Y, 1, 1);
}
}
private void button1_Click(object sender, EventArgs e)
{
g = this.CreateGraphics();
g.Clear(Color.Snow );
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
private bool drawBegin = false;
private Point starPoint = Point.Empty;
private void Form3_MouseDown(object sender, MouseEventArgs e)
{
this.drawBegin = true;
this.starPoint = e.Location;
g = this.CreateGraphics();
}
private void Form3_MouseMove(object sender, MouseEventArgs e)
{
if (drawBegin)
{
g.Clear(SystemColors.Control);
g.DrawLine(Pens.Black, starPoint, e.Location);
}
}
private void Form3_MouseUp(object sender, MouseEventArgs e)
{
this.drawBegin = false;
this.starPoint = Point.Empty;
}
2011-05-05
如果你要自己开发,gdi+必不可少.
某些功能网上有源码.
可以逐步添加功能.
另外,虚机团上产品团购,超级便宜