我想在其覆盖的绘制事件中的某个其他控件上绘制一个控件。我所说的绘图是指真正的绘图,而不是将控件放在另一个控件内。有什么好的方法可以做到这一点吗?
最佳答案
尝试ControlPaint 类的静态方法。绘制的控件可能不像 GUI 的其余部分那样被蒙皮,但效果会非常可信。下面是我的一些代码的精简版本。它使用 ControlPaint.DrawButton 方法重写 ownerdrawn ListBox 的 DrawItem 方法,使列表项看起来像按钮。
该类有更多关于复选框、组合、甚至拖动 handle 的好东西。
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index > -1)
{
String itemText = String.Format("{0}", this.Items.Count > 0 ? this.Items[e.Index] : this.Name);
//Snip
System.Windows.Forms.ControlPaint.DrawButton(e.Graphics, e.Bounds, ButtonState.Normal);
e.Graphics.DrawString(itemText, this.Font, SystemBrushes.ControlText, e.Bounds);
}
}
关于c# - 有什么方法可以在 C# 中的其他控件上绘制控件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/450055/