当我用TTreeView打开表单时,我用一些数据填写它并选择一些节点。但我需要让选定的节点在控件的中心可见(当然可能)。我找不到如何使用标准的TTreeView组件。 有什么想法吗?
答案 0 :(得分:3)
我能找到滚动TreeView的唯一方法是发送WM_VSCROLL
,其值为SB_LINEDOWN
/ SB_LINEUP
。由于某种原因,控件似乎没有响应SB_THUMBPOSITION
的滚动值,例如丰富的编辑。出于这个原因,下面的尝试通过向控件发送一堆行滚动消息来使节点或多或少居中。如果它符合您的需要,请亲自试试。
var
DR, CR: TRect;
ScrollPx, ScrollLines, i: Integer;
begin
if Assigned(TreeView1.Selected) then begin
// calculate how many *pixels* should we scroll
DR := TreeView1.Selected.DisplayRect(False);
CR := TreeView1.ClientRect;
ScrollPx := - Round((CR.Bottom / 2) - DR.Top - ((DR.Bottom - DR.Top) / 2));
// how many lines does it correspond to
ScrollLines := ScrollPx div TreeView_GetItemHeight(TreeView1.Handle);
// scroll that many lines
if ScrollLines > 0 then
for i := 1 to ScrollLines do
TreeView1.Perform(WM_VSCROLL, MakeWParam(SB_LINEDOWN, 0), 0)
else if ScrollLines < 0 then
for i := ScrollLines to -1 do
TreeView1.Perform(WM_VSCROLL, MakeWParam(SB_LINEUP, 0), 0);
end;