C#实现动态画线条并能移动!
Pen pen = new Pen(Color.Red,2);
g.Clear(this.BackColor);
g.DrawLine(pen,10,10,10,20);
这段代码我写在C#的FROM_LOAD里怎么线条画不出来啊?C#有没有VB一样有个线条空间啊!我要实现一个画线包含斜线,最后是能鼠标放在上面能实现移动的!
补充,这段代码一定要放到Paint(object sender, PaintEventArgs e)事件才有用,怎么可以不放到Paint(object sender, PaintEventArgs e)这个事件里面画出线来,因为我点一下button就画一条线,再点一下再画一条线,请不要定义一个Line类集成Label或picturebox之类的这个方法,这个方法无法画斜线,因为我的线大部分是斜的! 展开
//以下是完整代码,可以直接编译运行
//-------------------------------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
namespace q2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
/// <summary>
/// 线条对象
/// </summary>
class Line
{
/// <summary>
/// 建立线条对象,并设置起点
/// </summary>
/// <param name="startPoint">此线条的起点</param>
public Line(Point startPoint)
{
StartPoint = startPoint;
EndPoint = startPoint;
}
public Point StartPoint = Point.Empty;
public Point EndPoint = Point.Empty;
}
public class DrawPanel : Control
{
public DrawPanel()
{
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
}
}
/// <summary>
/// 窗口定义
/// </summary>
public class Form1 : Form
{
public Form1()
{
drawPanel.BackColor = Color.White;
drawPanel.Cursor = Cursors.Cross;
drawPanel.Dock = DockStyle.Fill;
drawPanel.MouseDown += new MouseEventHandler(drawPanel_MouseDown);
drawPanel.MouseUp += new MouseEventHandler(drawPanel_MouseUp);
drawPanel.MouseMove += new MouseEventHandler(drawPanel_MouseMove);
drawPanel.Paint += new PaintEventHandler(drawPanel_Paint);
Controls.Add(drawPanel);
}
/// <summary>
/// 用于保存绘出线条的集合
/// </summary>
private List<Line> lines = new List<Line>();
/// <summary>
/// 用于保存当前正在绘制的线条
/// </summary>
private Line drawingLine = null;
/// <summary>
/// 用于显示绘图的面板组件
/// </summary>
private DrawPanel drawPanel = new DrawPanel();
/// <summary>
/// 在绘图区释放鼠标,结束当前线条绘制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_MouseUp(object sender, MouseEventArgs e)
{
if (drawingLine == null) return;
drawingLine.EndPoint = e.Location;
drawingLine = null;
}
/// <summary>
/// 在绘图区按下鼠标,开始绘制新线条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_MouseDown(object sender, MouseEventArgs e)
{
drawingLine = new Line(e.Location);
lines.Add(drawingLine);
}
/// <summary>
/// 在绘图区移动鼠标时,如果正在绘制新线条,就更新绘制面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if(drawingLine != null)
{
drawingLine.EndPoint = e.Location;
drawPanel.Invalidate();
}
}
/// <summary>
/// 绘制效果到面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_Paint(object sender, PaintEventArgs e)
{
Bitmap bp = new Bitmap(drawPanel.Width, drawPanel.Height); // 用于缓冲输出的位图对象
Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 消锯齿(可选项)
Pen p = new Pen(Color.Black);
foreach (Line line in lines)
{
if (line == drawingLine)
{
// 当前绘制的线条是正在鼠标定位的线条
p.Color = Color.Blue;
}
else
{
p.Color = Color.Black;
}
g.DrawLine(p, line.StartPoint, line.EndPoint);
}
// 将缓冲位图绘制到输出
e.Graphics.DrawImage(bp, Point.Empty);
}
}
}
每画一条线,就生成一个Line控件,并添加事件,添加到form上
可以画斜线,任何线条,图形都行
//简单写了段代码,能移动,不过事件没怎么详细的定义,希望能对你有帮助
//1.Line类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Line
{
public class Line : PictureBox
{
public int startY;
public int endX;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="startY">起始点y坐标(x=0)</param>
/// <param name="endX">终点x坐标(y=0)</param>
public Line(int startY,int endX)
{
this.startY = startY;
this.endX = endX;
this.Width = endX;
this.Height = startY;
}
}
}
//2.Form1类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Line
{
public partial class Form1 : Form
{
private Line l = null;
private int cx,cy;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
l = new Line(50, 100);
l.Paint += new PaintEventHandler(this.Line_Paint);
this.Controls.Add(l);
}
private void Line_Paint(object sender, PaintEventArgs e)
{
Pen pen1 = new Pen(Color.Black, 2);
e.Graphics.DrawLine(pen1, 0, l.startY, l.endX, 0);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (l!=null)
{
l.SetBounds(l.Location.X + e.X - cx, l.Location.Y + e.Y - cy, l.Width, l.Height);
cx = e.X;
cy = e.Y;
}
}
}
}
注意给form添加对应的事件
如果是划完线实现鼠标拖拽整体线时 可以让线的A,B坐标为鼠标点击时线的A,B点坐标 然后随鼠标坐标的移动而加减A,B的值 其值为鼠标的横纵坐标的运算。。。
说的不是很明白 只是提供了一个不是很高明的算法。。。其实本人也是学习中。。。。
//int ax,ay,bx,by;
//移动的具体算法 就要看你想的到的效果了
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Red,2);
g.Clear(this.BackColor);
g.DrawLine(pen,10,10,10,20);
//g.DrawLine(pen,ax,ay,bx,by);