什么是在用户控件中配置画笔的更好方法 [英] What is better approach to dispose Brush in User Control

查看:55
本文介绍了什么是在用户控件中配置画笔的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Paint事件中使用新的Brush是否是更好的方法,即

Is it better approach to use a new Brush in Paint event i.e

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        using (SolidBrush b = new SolidBrush(Color.FromArgb(129, 242, 121)))
        {
            for (int i = 0; i < 12; i++)
            {
                e.Graphics.FillPath(b, path[i]);
            }
        }
        base.OnPaint(e);
    }


或一次定义一次并使用Dispose方法进行处置,即


or define once at top and dispose in Dispose Method i.e

private SolidBrush _b= null;

    private SolidBrush b
    {
        get
        {
            if (_b== null)
                return _b= new SolidBrush(Color.FromArgb(129, 242, 121));
            else
                return _b;
        }
    }

推荐答案

这完全取决于您是否要在一种以上的方法中使用同一笔刷.如果需要重用,则将其声明为类级别变量,然后将其处置在OnDispose方法中,否则只需像第一个示例中那样创建它即可.

希望对您有帮助
It all depends on whether you want to use the same brush in more than one method. If you need to reuse it then declare it as a class level variable and dispose it in OnDispose method, else just create it as in your first example.

Hope this helps


请参阅此
http://stackoverflow.com/questions/8253398/what-is-better-approach-to-dispose-brush-in-user-control [ http://stackoverflow.com/questions/1209585/when-is-dispose-necessary [ ^ ]
Refer this
http://stackoverflow.com/questions/8253398/what-is-better-approach-to-dispose-brush-in-user-control[^]
http://stackoverflow.com/questions/1209585/when-is-dispose-necessary[^]


这篇关于什么是在用户控件中配置画笔的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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