c# - 有什么方法可以在 C# 中的其他控件上绘制控件吗?

标签 c# gdi+

我想在其覆盖的绘制事件中的某个其他控件上绘制一个控件。我所说的绘图是指真正的绘图,而不是将控件放在另一个控件内。有什么好的方法可以做到这一点吗?

最佳答案

尝试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/

相关文章:

windows - 如何获得 GDI+ 路径的简单 "block"轮廓?

c# - 如何创建 NodeType 为 ExpressionType.Index 的 .NET 表达式?

listview - 防止 RTL TListView 镜像复选框和/或图形

c# - 我无法关闭此表单

c# - Distinct 在 Entity Framework 上不起作用

c++ - GDI ... 或 C++ 的等价物?

c# - 检测两条重合线段的重合子集

.net - 并行GDI +图像调整大小.net

c# - 在线程中编辑文件

java - C# 等效于 LinkedHashMap