private void Form1_Load(object sender, EventArgs e) { label1.BackColor = Color.Transparent; } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics graphics = this.CreateGraphics(); Pen pen1 = new Pen(Color.Gray, 1); int x = label1.Location.X - 5; int y = label1.Location.Y + 20; graphics.DrawLine(pen1, x - 20, y, x + 30, y); }label1是拖上去的
Graphics graphics = this.CreateGraphics(); Pen pen1 = new Pen(Color.Gray, 1); int x = label1.Location.X - 5; int y = label1.Location.Y + 20; graphics.DrawLine(pen1, x - 20, y, x + 30, y);替换为如下: e.Graphics.DrawLine(Pens.Gray, 28, 180, 300, 180);你这么代码问题很大
之前你是先Label后画线的吧?而这次你是先画线后放Label的吧??
我这次还是先label,后画线的
此代码没有起作用,你代码怎么写的?
{
label1.BackColor = Color.Transparent;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Gray, 1);
int x = label1.Location.X - 5;
int y = label1.Location.Y + 20;
graphics.DrawLine(pen1, x - 20, y, x + 30, y);
}label1是拖上去的
Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Gray, 1);
int x = label1.Location.X - 5;
int y = label1.Location.Y + 20;
graphics.DrawLine(pen1, x - 20, y, x + 30, y);替换为如下:
e.Graphics.DrawLine(Pens.Gray, 28, 180, 300, 180);你这么代码问题很大
方法已经给你了,且不要释放这个图形对象.2.this.CreateGraphics();使用后必须释放.
为何不自己画框?
private void Form1_Load(object sender, EventArgs e)
{
label1.BackColor = Color.Transparent;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
//Graphics graphics = this.CreateGraphics();
Pen pen1 = new Pen(Color.Gray, 1);
int x = label1.Location.X ;
int y = label1.Location.Y ;
graphics.DrawLine(pen1, x - 20, y+20, x + 30, y+20);
}
情况是这样的,
在Form1_Paint中,如果在写成Graphics graphics = e.Graphics;直线就能穿过label1;
如果写成Graphics graphics = this.CreateGraphics();不会报错,但不能穿过label1;
是什么原因?请问