创建Brush(或Pen)作为静态成员还是在OnPaint方法中本地? [英] Create Brush (or Pen) as a static member or locally within OnPaint method?

查看:82
本文介绍了创建Brush(或Pen)作为静态成员还是在OnPaint方法中本地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据默认的Windows窗体RadioButton创建(C#)自定义单选按钮。我在重写的OnPaint方法中绘制它。



解决方案1:经典的(我认为)是在OnPaint方法中创建(分配内存)画笔或笔,绘制内容并在最后处置它们方法。



解决方案2:如何只创建一次刷子,作为类的静态成员,在单选按钮的所有实例之间共享它们并在最后处置它们应用程序的生命周期(或者甚至只是在没有明确处理的情况下等待应用程序结束)。



这是一个类似工具的小应用程序,用户可以运行它,通过单选按钮选择一个选项并关闭应用程序。



实际上,在两个提到的解决方案中,画笔一直存在于应用程序中。在解决方案1中,刷子被重新创建和处理,不必要(?),静态解决方案2是一个更好的主意吗?



问候

ROB

Hi, I create (C#) a custom radio button based on the default Windows Forms RadioButton. I paint it in the overridden OnPaint method.

Solution 1: a classic one (I think) is to create (allocate memory) brushes or pens within the OnPaint method, paint stuff and dispose them at the end of the method.

Solution 2: how about creating the brushes only once, as the static members of the class, share them among all instances of the radio button and dispose them by the end of the application life (or maybe even just wait for the application end without explicit disposing).

This is for a small tool-like application, a user would run it, choose an option by a radio button and close the app.

Practically, in both mentioned solutions, the brushes exist all the time the application lives. In solution 1 the brushes are being created and disposed all over again, unnecessary(?), so would static solution 2 be a better idea?

regards
ROB

推荐答案

如果你根本不更换画笔(或者即使你没有在大多数画作事件之间改变画笔)那么我' d将它们创建为单个对象并重复使用它们:它更快,并且在用户界面中可以计算很多。



静态? Dunno - 我可能会让他们成为班级,除非有充分的理由让他们在整个应用程序 的所有控制实例中都很常见 - 请记住,如果控件以两种不同的形式使用,它们仍然可以共享相同的静态对象。
If you aren't changing the brushes at all (or even if you aren't changing them between most paint events) then I'd create them as a single object and reuse them: it's quicker, and that can count a lot in a user interface.

Static? Dunno - I'd probably make them class level, unless there was a good reason for making them common to all instances of the control across the whole application - remember that if the control is used in two different forms they would still share the same static objects.


这篇关于创建Brush(或Pen)作为静态成员还是在OnPaint方法中本地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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