好的,问题解决了。
我没有找到一个简单的答案,但我确实努力解决了这个问题。它基本上只是在自定义绘图中绘制额外的线段:
// _cd is the NMTVCUSTOMDRAW structure
// ITEMHEIGHT is the fixed height set in TreeView_SetItemHeight
// linePen is HPEN of a suitable pen to draw the lines (PS_ALTERNATE etc.)
// indent is the indentation size returned from TreeView_GetIndent
case CDDS_ITEMPREPAINT : {
// Expand line because TreeView is buggy
RECT r = _cd->nmcd.rc;
HDC hdc = _cd->nmcd.hdc;
HTREEITEM hItem = (HTREEITEM) _cd->nmcd.dwItemSpec;
if( r.bottom - r.top > ITEMHEIGHT ) {
HGDIOBJ oldPen = SelectObject( hdc, linePen );
// Draw any lines left of current item
HTREEITEM hItemScan = hItem;
for( int i = _cd->iLevel; i >= 0; --i ) {
// Line should be drawn only if node has a next sibling to connect to
if( TreeView_GetNextSibling( getHWnd(), hItemScan ) ) {
// Lines seem to start 17 pixels from left edge of control. But no idea
// where that constant comes from or if it is really constant.
int x = 17 + indent * i;
MoveToEx( hdc, x, r.top + ITEMHEIGHT, 0 );
LineTo( hdc, x, r.bottom );
}
// Do the same for the parent
hItemScan = TreeView_GetParent( getHWnd(), hItemScan );
}
SelectObject( hdc, oldPen );
}
}
来自 PS_ALTERNATE 画笔的图案有时与控件绘制的线不完全对齐,但这几乎不明显。更糟糕的是,即使我安装了最新的通用控件以及所有服务包和修补程序,TreeView 中仍然存在错误,早在 2005 年就有记录。具体来说,TreeView 没有正确更新其高度。我找到的唯一解决方法是强制节点折叠/扩展并对 InvalidateRect 进行一些调用。
如果可变高度节点位于根级别,那么您似乎无能为力。幸运的是我不需要那个。