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事件。
可以 重写 Paint 事件吗?一般都重写 OnPaint 方法,但一定要调用 base.OnPaint,不能就不会引发 Paint 事件了。
-------------------------------------------还不是太明白好像写任何一个都有相同的效果,如果都写了,则Paint事件的效果没有出来.
而如果仅仅在某个环境下,要做不同的显示,则只需在其的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事件。
onpaint是一个方法:
onpaint原形应该类似以下形式:
protectd virturl void onpaint(paintevertargs e)
{
if(this.paint != null)
{
this.paint(this,e);
}
}
也就是说,重写onpaint后如果不调用base.onpaint是不会出发paint.
不过你也可以自己加那句(上面)
protected override void OnPaint(PaintEventArgs e)
{
}