我在C#winforms中创建自定义控件。我已经添加一个图标资源,那么这个图标使用下面的代码绘制在控制:在窗体上绘制自定义图标
using (Icon oIcon = Properties.Resources.DropDownCustom)
{
Rectangle RectangleIcon = new Rectangle((DropDownRectangle.X + ((DropDownRectangle.Width/2) - (oIcon.Width/2))),
(DropDownRectangle.Y + (((DropDownRectangle.Height/2) - (oIcon.Height/2)) + 1)),
oIcon.Width,
oIcon.Height);
graphics.DrawIcon(oIcon, RectangleIcon);
}
这一切工作正常没有问题,但后来我决定一个选项添加到控件属性允许开发者加载他们自己的图标来使用,而不是使用我放置在资源中的图标。我创建了一个私人图标变量:
private Icon _DropDownCustom;
改变在上面的代码中的“使用”行改为:
using (Icon oIcon = _DropDownCustom)
,然后添加到构造以下行来设置默认值的一个在资源。
_DropDownCustom = Properties.Resources.DropDownCustom;
我加入一个图标属性,以便开发人员可以使用thier自己的图标:
[Category("Appearance"), DisplayName("IconDropDown")]
public Icon IconDropDownCustom
{
get { return _DropDownCustom; }
set { _DropDownCustom = value; this.Invalidate(); }
}
这一切似乎是工作正常,但现在,当我查看窗体上的控件(在开发模式中)它将图标绘制到控件上 - 很好,但是一旦我选择了窗体,或者控件的图标消失了,但其他绘画仍然保留(即渐变背景)。
有谁知道为什么它似乎没有重新绘制图标?
非常感谢。
编辑: 我刚刚删除的代码“Using(){}
”部分,将其改为:
Icon oIcon = _DropDownCustom;
Rectangle RectangleIcon = new Rectangle((DropDownRectangle.X + ((DropDownRectangle.Width/2) - (oIcon.Width/2))),
(DropDownRectangle.Y + (((DropDownRectangle.Height/2) - (oIcon.Height/2)) + 1)),
oIcon.Width,
oIcon.Height);
graphics.DrawIcon(oIcon, RectangleIcon);
这似乎按预期工作,所以我猜这是什么做用和处置 - 仍试图理解违规部分 - 请你解释为什么会发生这种情况?我猜我的“oIcon”基本上只是引用我自定义的Icon变量而不是“按值”(我来自VB背景)。