Graphics g = label1.CreateGraphics();
g.Clear(Color.Black);
Pen p = new Pen(Color.Red );
g.DrawRectangle(p,0,0,10,10);
// g.DrawLine(p, 400, 0, 800, 300);我在窗口中托了一个label控件,用上面的代码画图,怎么没反应呢,运行后,控件就一片黑暗用pant事件就能够画起这是怎么回事啊??下面是完整代码
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Graphics g = label1.CreateGraphics();
g.Clear(Color.Black);
Pen p = new Pen(Color.Red );
g.DrawRectangle(p,0,0,10,10);
// g.DrawLine(p, 400, 0, 800, 300);
}
}
}
g.Clear(Color.Black);
Pen p = new Pen(Color.Red );
g.DrawRectangle(p,0,0,10,10);
// g.DrawLine(p, 400, 0, 800, 300);我在窗口中托了一个label控件,用上面的代码画图,怎么没反应呢,运行后,控件就一片黑暗用pant事件就能够画起这是怎么回事啊??下面是完整代码
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Graphics g = label1.CreateGraphics();
g.Clear(Color.Black);
Pen p = new Pen(Color.Red );
g.DrawRectangle(p,0,0,10,10);
// g.DrawLine(p, 400, 0, 800, 300);
}
}
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
{
InitializeComponent();
Graphics g = label1.CreateGraphics();
g.Clear(Color.Black);
Pen p = new Pen(Color.Red );
g.DrawRectangle(p,0,0,10,10);
// g.DrawLine(p, 400, 0, 800, 300);
}
根据你的代码,你只在窗体构造函数中画了一次,当然不行了,
当窗体激活的时候,控件会调用paint事件重画,这个时候因为没有上述的代码,当然就没有任何效果了。
这个是必须在Paint事件里执行,当有重绘请求时,自然调用Paint事件里的代码,达到你的要求了。
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.Black);
Pen p = new Pen(Color.Red);
g.DrawRectangle(p, 0, 0, 10, 10);
g.DrawLine(p, 400, 0, 800, 300);
//label1.Refresh();
} private void label1_Paint(object sender, PaintEventArgs e)
{ }
绘图就只能依靠Paint事件 了??
这个例子对你会有帮组的
这玩意不是你绘一下就行的
可能要绘N次
要不然
你可以绘制到一个BMP中,然后将BMP设置为背景图片