C#WinForms-绘画方法问题 [英] C# WinForms - Paint method questions

查看:75
本文介绍了C#WinForms-绘画方法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定使用图形的最佳方法是什么-我应该将类附加到主窗体Paint事件然后进行绘制,还是最好像这样从隐藏的OnPaint void调用它?我的意思是,可以这样做吗?

I am not sure what is the best way of using graphics - should I attach my classes to main form Paint event and then do the drawing, or it is better to call it from overidden OnPaint void like this? I mean, is it OK to do that like this:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e)  //what is this good for? My app works without it as well
    Graphics g=e.Graphics;
    DrawEnemies(g);
    UpdateHUD(g);
    DrawSelectedUnit(g);
}

推荐答案

建议控件重写On...方法,而不要订阅自己的事件.

It is recommended that controls override the On... methods rather than subscribe to their own events.

您应调用base.OnPaint以确保正确触发了Paint方法.

You should call base.OnPaint to ensure the Paint method is fired properly.

来自MSDN :

OnPaint方法还启用 派生类来处理事件 而不附加代表.这是 首选的处理技术 派生类中的事件.

The OnPaint method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

对继承者的说明
覆盖时 OnPaint在派生类中,请务必 调用基类的OnPaint方法 这样注册的代表就可以收到 事件.

Notes to Inheritors
When overriding OnPaint in a derived class, be sure to call the base class's OnPaint method so that registered delegates receive the event.

这篇关于C#WinForms-绘画方法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆