C#中事件执行的先后循序问题...
private void Form1_Load(object sender, EventArgs e) { Squer squer1 = new Squer(); squer1.startpoint.X = 50; squer1.startpoint.Y = 50; squer1.DrawSquer(this.pictureBox1.Handle); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Squer squer1 = new Squer(); squer1.startpoint.X = 50; squer1.startpoint.Y = 50; squer1.DrawSquer(this.pictureBox1.Handle); }
class Squer { private Color bc_color = Color.Red; private int size = 50; public Point startpoint; public Squer() { } public void DrawSquer(System.IntPtr windowhandle) { Graphics g = Graphics.FromHwnd(windowhandle); SolidBrush b=new SolidBrush(Color.Red); g.FillRectangle(b, startpoint.X, startpoint.Y, size, size); } }
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Squer squer1 = new Squer(); squer1.startpoint.X = this.pictureBox1.Left; squer1.startpoint.Y = this.pictureBox1.Top; this.BeginInvoke((Action)(() => squer1.DrawSquer(this.pictureBox1.Handle))); } } class Squer { private Color bc_color = Color.Red; private int size = 50; public Point startpoint; public Squer() { } public void DrawSquer(System.IntPtr windowhandle) { Graphics g = Graphics.FromHwnd(windowhandle); SolidBrush b = new SolidBrush(Color.Red); g.FillRectangle(b, startpoint.X, startpoint.Y, size, size); } }