我想在picturebox上画一线段动作是这样的刚开始在picturebox上点一下,然后就有一条线连着picturebox与鼠标之间,接着我选择picturebox上另外一个位置,再点一下,就有一条线段在picturebox上被我确定的画下来我在网上找的几个代码,十分不好,要么太庞大不加注释,要么就先画什么白线,与底色一样,然后再画其他颜色线求这个功能的比较好的源码,谢谢
调试欢乐多
因为有的功能只能画一次线段同时
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace PictureDraw
{
public partial class Form1 : Form
{
int X;
int Y;
bool m_IsDraw = false;
Point m_StartPoint;
Point m_EndPoint;
List<Point[]> list = new List<Point[]>(); public Form1()
{
InitializeComponent();
MouseMove += new MouseEventHandler(Form1_MouseMove);
MouseDown += new MouseEventHandler(Form1_MouseDown);
} void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (!m_IsDraw)
{
m_StartPoint = new Point(e.X, e.Y);
m_IsDraw = true;
}
else
{
m_EndPoint = new Point(e.X, e.Y);
list.Add(new Point[] { m_StartPoint, m_EndPoint });
m_IsDraw = false;
Invalidate();
}
} void Form1_MouseMove(object sender, MouseEventArgs e)
{
X = e.X;
Y = e.Y;
if (m_IsDraw)
{
m_EndPoint = new Point(X, Y);
Invalidate();
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics; foreach (Point[] ps in list)
{
g.DrawLine(Pens.Blue, ps[0], ps[1]);
}
g.DrawLine(Pens.Blue, m_StartPoint, m_EndPoint);
}
}
}
我加到pictureBox里面,画图画不了
人家的代码是Form1里的。放到PictureBox里当然没用了。
你要把里面的Form1全部改成PictureBox1
做一个自定义的CustomPictureBox控件来用新建一个用户控件的项目,然后Form用这个控件就可以了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace CustomPictureBox
{
public partial class CustomPictureBox : PictureBox
{
int X;
int Y;
bool m_IsDraw = false;
Point m_StartPoint;
Point m_EndPoint;
List<Point[]> list = new List<Point[]>(); public CustomPictureBox()
{ this.MouseMove += new MouseEventHandler(p_MouseMove);
this.MouseDown += new MouseEventHandler(p_MouseDown); } void p_MouseDown(object sender, MouseEventArgs e)
{
if (!m_IsDraw)
{
m_StartPoint = new Point(e.X, e.Y);
m_IsDraw = true;
}
else
{
m_EndPoint = new Point(e.X, e.Y);
list.Add(new Point[] { m_StartPoint, m_EndPoint });
m_IsDraw = false;
this.Invalidate();
} } void p_MouseMove(object sender, MouseEventArgs e)
{
X = e.X;
Y = e.Y;
if (m_IsDraw)
{
m_EndPoint = new Point(X, Y);
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
foreach (Point[] ps in list)
{
g.DrawLine(Pens.Blue, ps[0], ps[1]);
}
g.DrawLine(Pens.Blue, m_StartPoint, m_EndPoint);
}
}
}
picturebox.MouseMove += new MouseEventHandler(p_MouseMove);
picturebox.MouseDown += new MouseEventHandler(p_MouseDown);
再把失效重绘改成
picturebox.Invalidate();
多的东西都不用
搞了半天都没搞定
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace GDI_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
}
int x1;
int y1;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
x1 = e.X;
y1 = e.Y;
} private void Form1_MouseUp(object sender, MouseEventArgs e)
{
int x2 = e.X;
int y2= e.Y;
Graphics g = this.CreateGraphics();
g.DrawLine(new Pen(Color.Red),x1,y1,x2,y2);
}//绘制直线,不用那么麻烦的