所有者绘制的树视图(WinForms)

本文关键字:WinForms 视图 绘制 所有者 | 更新日期: 2023-09-27 17:50:17

我有一个带有复选框的TreeView控件,它完全是所有者绘制的(DrawMode = TreeViewDrawMode.OwnerDrawAll)。

我想做的是有复选框的所有者绘制,所以他们可以有一个灰色的状态。我使用VisualStyleRenderer

当我必须在项目边界中正确放置展开/折叠字形和复选框时,问题就出现了,因为字形和复选框的"命中测试区域"似乎是未知的和不可更改的。

是否有办法得到这些区域的边界,或者用自定义值代替默认值?

所有者绘制的树视图(WinForms)

我遇到了同样的问题。你必须用适当的数额来抵消你的提款,这是可以预见的。

这里可能比你需要的更多,但这是我的自定义树的绘图,我使用一个日历控件:

private void TreeViewControl_DrawNode(Object sender, DrawTreeNodeEventArgs e)
{
    //What might seem like strange positioning/offset is to ensure that our custom drawing falls in
    //  line with where the base drawing would appear.  Otherwise, click handlers (hit tests) fail 
    //  to register properly if our custom-drawn checkbox doesn't fall within the expected coordinates.
    Int32 boxSize = 16;
    Int32 offset = e.Node.Parent == null ? 3 : 21;
    Rectangle bounds = new Rectangle(new Point(e.Bounds.X + offset, e.Bounds.Y + 1), new Size(boxSize, boxSize));
    ControlPaint.DrawCheckBox(e.Graphics, bounds, e.Node.Checked ? ButtonState.Checked : ButtonState.Normal);
    if (e.Node.Parent != null)
    {
        Color c = Color.Black;
        String typeName = e.Node.Name.Remove(0, 4);
        Object o = Enum.Parse(typeof(CalendarDataProvider.CalendarDataItemType), typeName);
        if (o != null && (o is CalendarDataProvider.CalendarDataItemType))
            c = CalendarDataProvider.GetItemTypeColor((CalendarDataProvider.CalendarDataItemType)o);
        bounds = new Rectangle(new Point(bounds.X + boxSize + 2, e.Bounds.Y + 1), new Size(13, 13));
        using (SolidBrush b = new SolidBrush(c))
            e.Graphics.FillRectangle(b, bounds);
        e.Graphics.DrawRectangle(Pens.Black, bounds);
        e.Graphics.DrawLine(Pens.Black, new Point(bounds.X + 1, bounds.Bottom + 1), new Point(bounds.Right + 1, bounds.Bottom + 1));
        e.Graphics.DrawLine(Pens.Black, new Point(bounds.Right + 1, bounds.Y + 1), new Point(bounds.Right + 1, bounds.Bottom + 1));
    }
    Font font = new Font("Microsoft Sans Serif", 9f, e.Node.Parent == null ? FontStyle.Bold : FontStyle.Regular);
    bounds = new Rectangle(new Point(bounds.X + boxSize + 2, e.Bounds.Y), new Size(e.Bounds.Width - offset - 2, boxSize));
    e.Graphics.DrawString(e.Node.Text, font, Brushes.Black, bounds);
}