C#画直线的代码!(和windows画图工具一样的效果*)我的代码不对,不是射线状,就是没有画的过程.怎么样实现在屏幕上一点,拖出一个可变的直线,像橡皮条,松开后确定的那种!private void Drawing_Mous
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/28 21:06:25
C#画直线的代码!(和windows画图工具一样的效果*)我的代码不对,不是射线状,就是没有画的过程.怎么样实现在屏幕上一点,拖出一个可变的直线,像橡皮条,松开后确定的那种!private void Drawing_Mous
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算法是解决我这个问题的么?
C#画直线的代码!(和windows画图工具一样的效果*)我的代码不对,不是射线状,就是没有画的过程.怎么样实现在屏幕上一点,拖出一个可变的直线,像橡皮条,松开后确定的那种!private void Drawing_Mous
我这里是画直线和画曲线的两种都有,楼主看看,错不了的,代码如下:
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();
}
}
}