请教各位高手:
我现在自己设计一个C#控件,要求该控件绘有Line,该怎么绘制?
我用的是:
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 8);
g.DrawLine(p, 0, 0, 500, 500);在InitializeComponent();后写以上代码,但是调用控件时没有Line。
不知为什么?
我现在自己设计一个C#控件,要求该控件绘有Line,该怎么绘制?
我用的是:
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 8);
g.DrawLine(p, 0, 0, 500, 500);在InitializeComponent();后写以上代码,但是调用控件时没有Line。
不知为什么?
我试过了,在Graphics g = this.CreateGraphics(); 之后加show,不管用
是不是一定要添加panel?
或者说在UserControl自动生成的面板上是不能绘图的?
public class UserControl1 : UserControl
{
protected override void OnPaint(PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 8))
{
e.Graphics.DrawLine(p, 0, 0, 500, 500);
}
}}
你写错地方了。
一个窗体(包括UserControl)一旦被遮住后,需要更新自己,也就是画自己。
重新画窗体调用的就是OnPaint。
也可以添加
this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);来实现。