C#中画一条直线完整方法
要什么graphics和pen的Penpen=newPen(Color.Pink,2);PictureBoxp1=newPictureBox();p1.Width=20;...
要什么graphics和pen的
Pen pen = new Pen(Color.Pink, 2);
PictureBox p1 = new PictureBox();
p1.Width = 20;
p1.Height = 40;
p1.Top = 100;
p1.Left = 100;
Graphics p = p1.CreateGraphics();
p.DrawLine(pen, 100, 100, 120, 100);
this.Controls.Add(p1);
为什么在form上不显示出来? 展开
Pen pen = new Pen(Color.Pink, 2);
PictureBox p1 = new PictureBox();
p1.Width = 20;
p1.Height = 40;
p1.Top = 100;
p1.Left = 100;
Graphics p = p1.CreateGraphics();
p.DrawLine(pen, 100, 100, 120, 100);
this.Controls.Add(p1);
为什么在form上不显示出来? 展开
3个回答
展开全部
写在这个事件里就可以了。
那是因为重画的原因。
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Blue, new Point(0, 0), new Point(500, 500));
}
那是因为重画的原因。
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Blue, new Point(0, 0), new Point(500, 500));
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在Form上画直线
bool downflag = false;
Point start = new Point(0, 0);
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
downflag = true;
start = e.Location;
}
}
Point old = new Point(0, 0);
private void DrawLine(Brush b, Point p1, Point p2)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(b);
g.DrawLine(p, p1, p2);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (downflag)
{
DrawLine(new SolidBrush(this.BackColor), start, old);
DrawLine(new SolidBrush(Color.Blue), start, e.Location);
old = e.Location;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DrawLine(new SolidBrush(Color.Blue), start, e.Location);
downflag = false;
old = new Point(0, 0);
start = new Point(0, 0);
}
}
Pen pen = new Pen(Color.Pink, 2);
PictureBox p1 = new PictureBox();
p1.Width = 20;
p1.Height = 40;
p1.Top = 100;
p1.Left = 100;
Graphics p = p1.CreateGraphics();
p.DrawLine(pen, 100, 100, 120, 100);
this.Controls.Add(p1);
为什么在form上不显示出来?
被显示pictureBox的行为刷新掉了吧,你延迟一点来画这条直线就可以了
bool downflag = false;
Point start = new Point(0, 0);
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
downflag = true;
start = e.Location;
}
}
Point old = new Point(0, 0);
private void DrawLine(Brush b, Point p1, Point p2)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(b);
g.DrawLine(p, p1, p2);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (downflag)
{
DrawLine(new SolidBrush(this.BackColor), start, old);
DrawLine(new SolidBrush(Color.Blue), start, e.Location);
old = e.Location;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DrawLine(new SolidBrush(Color.Blue), start, e.Location);
downflag = false;
old = new Point(0, 0);
start = new Point(0, 0);
}
}
Pen pen = new Pen(Color.Pink, 2);
PictureBox p1 = new PictureBox();
p1.Width = 20;
p1.Height = 40;
p1.Top = 100;
p1.Left = 100;
Graphics p = p1.CreateGraphics();
p.DrawLine(pen, 100, 100, 120, 100);
this.Controls.Add(p1);
为什么在form上不显示出来?
被显示pictureBox的行为刷新掉了吧,你延迟一点来画这条直线就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是GDI+,也就是.NET下的托管GDI。
using(Graphics canvas = ......) {
//...
using(Pen linePen = ...) {
canvas.DrawLine(linePen, ...);
}
}
DrawLine的定义,你自己多看看MSDN。
DrawLine(Pen pen, Point point1, Point point2)
DrawLine(Pen pen, PointF point1, PointF point2)
DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)
DrawLine(Pen pen, Single x1, Single y1, Single x2, Single y2)
using(Graphics canvas = ......) {
//...
using(Pen linePen = ...) {
canvas.DrawLine(linePen, ...);
}
}
DrawLine的定义,你自己多看看MSDN。
DrawLine(Pen pen, Point point1, Point point2)
DrawLine(Pen pen, PointF point1, PointF point2)
DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)
DrawLine(Pen pen, Single x1, Single y1, Single x2, Single y2)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询