请教各位高手:
我现在自己设计一个C#控件,要求该控件绘有Line,该怎么绘制?
我用的是:
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 8);
g.DrawLine(p, 0, 0, 500, 500);在InitializeComponent();后写以上代码,但是调用控件时没有Line。
不知为什么?

解决方案 »

  1.   

    看看有没有g.show之类的方法。
      

  2.   

    有些控件要覆盖OnPaint方法,才能绘制。你覆盖这个方法试试
      

  3.   


    我试过了,在Graphics g = this.CreateGraphics(); 之后加show,不管用
      

  4.   


    是不是一定要添加panel?
      

  5.   

    绘图一定需要一个panel,或者form么?
    或者说在UserControl自动生成的面板上是不能绘图的?
      

  6.   


    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。
      

  7.   

    谢谢gomoku,明白了。
    也可以添加
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);来实现。