C# 从panel中画一条竖线
想从panel中画一条竖线怎么就是画不出来呢郁闷感觉没有写错啊。。。。。Graphicsgraphics=this.panel1.CreateGraphics();//g...
想从panel中画一条竖线怎么就是画不出来呢 郁闷 感觉没有写错啊。。。。。 Graphics graphics = this.panel1.CreateGraphics(); //graphics.DrawLine(new Pen(Color.Black,2), 0, this.panel1.Top, this.panel1.Left + 5, this.panel1.Bottom); Brush gridBrush = new SolidBrush(Color.Magenta), backColorBrush = new SolidBrush(Color.Magenta); Pen mypen1 = new Pen(gridBrush); graphics.DrawLine(mypen1, new PointF(this.panel1.Location.X - 4, this.panel1.Location.Y), new PointF(this.panel1.Location.X - 4, this.panel1.Location.Y+this.panel1.Bottom));
展开
3个回答
展开全部
试试下面的方法
//注意二维坐标系的定义
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.panel1.CreateGraphics();
//绘制中线
graphics.Clear(Color.LightYellow);
float x1 = this.panel1.Width / 2;
float y1 = 0;
float x2 = this.panel1.Width / 2;
float y2 = this.panel1.Height;
//注意坐标系的定义
graphics.DrawLine(new Pen(Color.Black, 2), x1, y1, x2, y2);
string temp = string.Format("({0},{1})", x1, y1);
graphics.DrawString(temp, this.Font, Brushes.Black, x1, y1);
temp = string.Format("({0},{1})", x2, y2);
SizeF strSize = graphics.MeasureString(temp, this.Font);
graphics.DrawString(temp, this.Font, Brushes.Black, x2 - strSize.Width, y2-strSize.Height);
//绘制对角线
Brush gridBrush = new SolidBrush(Color.Magenta), backColorBrush = new SolidBrush(Color.Magenta);
Pen mypen1 = new Pen(gridBrush);
x1 = 0;
y1 = 0;
x2 = this.panel1.Width;
y2 = this.panel1.Height;
graphics.DrawLine(mypen1, x1, y1, x2, y2);
temp = string.Format("({0},{1})", x1, y1);
graphics.DrawString(temp, this.Font, Brushes.Black, x1, y1);
temp = string.Format("({0},{1})", x2, y2);
strSize = graphics.MeasureString(temp, this.Font);
graphics.DrawString(temp, this.Font, Brushes.Black, x2 - strSize.Width, y2 - strSize.Height);
}
//注意二维坐标系的定义
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.panel1.CreateGraphics();
//绘制中线
graphics.Clear(Color.LightYellow);
float x1 = this.panel1.Width / 2;
float y1 = 0;
float x2 = this.panel1.Width / 2;
float y2 = this.panel1.Height;
//注意坐标系的定义
graphics.DrawLine(new Pen(Color.Black, 2), x1, y1, x2, y2);
string temp = string.Format("({0},{1})", x1, y1);
graphics.DrawString(temp, this.Font, Brushes.Black, x1, y1);
temp = string.Format("({0},{1})", x2, y2);
SizeF strSize = graphics.MeasureString(temp, this.Font);
graphics.DrawString(temp, this.Font, Brushes.Black, x2 - strSize.Width, y2-strSize.Height);
//绘制对角线
Brush gridBrush = new SolidBrush(Color.Magenta), backColorBrush = new SolidBrush(Color.Magenta);
Pen mypen1 = new Pen(gridBrush);
x1 = 0;
y1 = 0;
x2 = this.panel1.Width;
y2 = this.panel1.Height;
graphics.DrawLine(mypen1, x1, y1, x2, y2);
temp = string.Format("({0},{1})", x1, y1);
graphics.DrawString(temp, this.Font, Brushes.Black, x1, y1);
temp = string.Format("({0},{1})", x2, y2);
strSize = graphics.MeasureString(temp, this.Font);
graphics.DrawString(temp, this.Font, Brushes.Black, x2 - strSize.Width, y2 - strSize.Height);
}
展开全部
不该这么画,panel1写它的onpainted事件,将你的代码放进去,不要用 Graphics graphics = this.panel1.CreateGraphics(); 产生Graphics对象,用事件e中的Graphics对象即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你试试在DrawLine函数后面加上graphics.Flush()函数,这个函数是强制刷新缓冲区,显示图像,而无视Paint函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询