none
画两条线,为何只显示方法二的线条,方法一的线条哪去了, RRS feed

  • 问题

  • Pen p = new Pen(Color.Black, 2);
            //方法一:
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics G = e.Graphics;
                G.DrawLine(p, 30, 30, 100, 30);
            }
            //方法二:
            protected override void OnPaint(PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.DrawLine(p, 30, 100, 100, 100);
            }


    杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............

    2013年6月27日 12:24

答案

  • OnPaint应该要触发Paint事件,所以你应该再次调用父函数的方法:

     protected override void OnPaint(PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.DrawLine(p, 30, 100, 100, 100);
                base.OnPaint(e);
            }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月28日 1:30
    版主
  • 是两条线,你看结果:

    public partial class Form1 : Form
        {
            Pen p = new Pen(Color.Black2);
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object senderEventArgs e)
            {
     
            }
     
            private void Form1_Paint(object senderPaintEventArgs e)
            {
                Graphics G = e.Graphics;
                G.DrawLine(p303010030);
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                p = new Pen(Brushes.Red);
                Graphics G = e.Graphics;
                G.DrawLine(p3010010030);
            }
        }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月29日 3:16
    版主

全部回复

  • OnPaint应该要触发Paint事件,所以你应该再次调用父函数的方法:

     protected override void OnPaint(PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.DrawLine(p, 30, 100, 100, 100);
                base.OnPaint(e);
            }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月28日 1:30
    版主
  •  

    试过了,加了base.OnPaint(e)仍然没有显示方法一的线条,两个方法是否不能同时使用。


    杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............

    2013年6月28日 10:59
  • 是两条线,你看结果:

    public partial class Form1 : Form
        {
            Pen p = new Pen(Color.Black2);
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object senderEventArgs e)
            {
     
            }
     
            private void Form1_Paint(object senderPaintEventArgs e)
            {
                Graphics G = e.Graphics;
                G.DrawLine(p303010030);
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                p = new Pen(Brushes.Red);
                Graphics G = e.Graphics;
                G.DrawLine(p3010010030);
            }
        }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月29日 3:16
    版主