如何知道图形对象属于哪个控件?

时间:2016-05-15 02:12:49

标签: c# winforms graphics

我有一种绘制内容的方法,我想更改方法中Control对象的Graphics的大小。

我的意思是这样的:

    class Drawer
    {
        public Drawer()
        {

        }
        public void Draw(Graphics grp)
        {
            grp.Owner.Width = 100;
            grp.Owner.Height = 200;
            //...
            //Draw something
            //...
        }
    }

但是你知道Graphics.Owner不存在。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

不幸的是,这不是一个选择。如果可以,最好通过控制。并确保处置图形对象:

public void Draw(Control ctrl)
{
    ctrl.Width = 100;
    ctrl.Height = 200;

    using(Graphic g = ctrl.CreateGraphics()) 
    {
        //...
        //Draw something
        //...
    }
}

或者用图形传递控件:

public void Draw(Graphics grp, Control ctrl)
{
    ctrl.Width = 100;
    ctrl.Height = 200;  
    //...
    //Draw something
    //...   

}
相关问题