积极答复者
画两条线,为何只显示方法二的线条,方法一的线条哪去了,

问题
-
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); }
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
答案
-
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- 已建议为答案 Jason Dot WangModerator 2013年7月3日 7:49
- 已标记为答案 Jason Dot WangModerator 2013年7月8日 9:07
-
是两条线,你看结果:
public partial class Form1 : Form { Pen p = new Pen(Color.Black, 2); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } 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) { base.OnPaint(e); p = new Pen(Brushes.Red); Graphics G = e.Graphics; G.DrawLine(p, 30, 100, 100, 30); } }
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- 已建议为答案 Jason Dot WangModerator 2013年7月3日 7:49
- 已标记为答案 Jason Dot WangModerator 2013年7月8日 9:07
全部回复
-
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- 已建议为答案 Jason Dot WangModerator 2013年7月3日 7:49
- 已标记为答案 Jason Dot WangModerator 2013年7月8日 9:07
-
是两条线,你看结果:
public partial class Form1 : Form { Pen p = new Pen(Color.Black, 2); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } 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) { base.OnPaint(e); p = new Pen(Brushes.Red); Graphics G = e.Graphics; G.DrawLine(p, 30, 100, 100, 30); } }
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- 已建议为答案 Jason Dot WangModerator 2013年7月3日 7:49
- 已标记为答案 Jason Dot WangModerator 2013年7月8日 9:07