本文介绍了在 ListBox 周围绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在列表框周围绘制具有指定宽度和颜色的边框?这可以在不覆盖 OnPaint 方法的情况下完成吗?
How would I go about drawing a border with a specified width and color around a listbox?Can this be done without overriding the OnPaint method?
推荐答案
按照 Neutone 的建议,这里有一个方便的函数来添加和删除任何控件周围的基于 Panel
的边框,即使它是嵌套..
Following Neutone's suggestion, here is a handy function to add and remove a Panel
-based border around any control, even if it is nested..
只需传入您想要的 Color
和大小,如果您想要 BorderStyle
.要再次删除它,请传入 Color.Transparent
!
Simply pass in the Color
and size you want, and if you want a BorderStyle
. To remove it again pass in Color.Transparent
!
void setBorder(Control ctl, Color col, int width, BorderStyle style)
{
if (col == Color.Transparent)
{
Panel pan = ctl.Parent as Panel;
if (pan == null) { throw new Exception("control not in border panel!");}
ctl.Location = new Point(pan.Left + width, pan.Top + width);
ctl.Parent = pan.Parent;
pan.Dispose();
}
else
{
Panel pan = new Panel();
pan.BorderStyle = style;
pan.Size = new Size(ctl.Width + width * 2, ctl.Height + width * 2);
pan.Location = new Point(ctl.Left - width, ctl.Top - width);
pan.BackColor = col;
pan.Parent = ctl.Parent;
ctl.Parent = pan;
ctl.Location = new Point(width, width);
}
}
这篇关于在 ListBox 周围绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!