OnPaint方法与Paint是两回事,OnPaint是引发Paint的事件,即当需要重绘时,在组件的内部调用OnPaint这个受保护的方法,然后由这个方法引发Paint事件.如果想要重绘窗体,你可以直接继承窗体,然后重写OnPaint,如:
protected override void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
base.OnPaint(e);
if(this.picture != null && this.pictureLocation != Point.Empty)
{
e.Graphics.DrawImage(this.picture, this.pictureLocation);
}
}
此处调用base.OnPaint(e),是要调用基类的绘制
你也可以直接在处理窗体的Paint事件。
protected override void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
base.OnPaint(e);
if(this.picture != null && this.pictureLocation != Point.Empty)
{
e.Graphics.DrawImage(this.picture, this.pictureLocation);
}
}
此处调用base.OnPaint(e),是要调用基类的绘制
你也可以直接在处理窗体的Paint事件。