【问题标题】:Virtual StringTree: How to determine if the node text is completely shown?Virtual StringTree:如何判断节点文本是否完整显示?
【发布时间】:2010-01-20 04:57:18
【问题描述】:

当TVirtualStreeTree.HintMode = hmTooltip时,当鼠标悬停在节点文本未完全显示的节点和列上时,节点文本将成为提示文本。但是我必须设置 HintMode = hmHint,以便我可以在 even 处理程序中根据当前鼠标光标的位置提供各种提示文本,并且在该 HintMode 中不会自动生成提示文本。

我的问题是如何知道节点文本是否完全显示,以便我知道我应该提供节点文本还是空字符串作为提示文本?
谢谢。

【问题讨论】:

    标签: delphi virtualtreeview


    【解决方案1】:

    您可以调用TBaseVirtualTree.GetDisplayRect 来确定节点的文本边界。根据Unclipped 参数,它将为您提供完整或实际的文本宽度。 TextOnly 应该设置为True:

    function IsTreeTextClipped(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean;
    var
      FullRect, ClippedRect: TRect;
    begin
      FullRect := Tree.GetDisplayRect(Node, Column, True, True);
      ClippedRect := Tree.GetDisplayRect(Node, Column, True, False);
      Result := (ClippedRect.Right - ClippedRect.Left) < (FullRect.Right - FullRect.Left);
    end;
    

    请注意,如果节点尚未初始化,该函数将隐式初始化节点。

    【讨论】:

    • 谢谢你 TOndrej,你的代码就像一个魅力!我试过 GetDisplayRect 但我没有注意到我们可以单独使用这个函数来完成这个任务!
    【解决方案2】:

    您可以使用树控件本身使用的内容。这是hmTooltip 模式生效时单行节点的cm_HintShow 消息处理程序的摘录。

    NodeRect := GetDisplayRect(HitInfo.HitNode, HitInfo.HitColumn, True, True, True);
    BottomRightCellContentMargin := DoGetCellContentMargin(HitInfo.HitNode, HitInfo.HitColumn
    , ccmtBottomRightOnly);
    
    ShowOwnHint := (HitInfo.HitColumn > InvalidColumn) and PtInRect(NodeRect, CursorPos) and
      (CursorPos.X <= ColRight) and (CursorPos.X >= ColLeft) and
      (
        // Show hint also if the node text is partially out of the client area.
        // "ColRight - 1", since the right column border is not part of this cell.
        ( (NodeRect.Right + BottomRightCellContentMargin.X) > Min(ColRight - 1, ClientWidth) ) or
        (NodeRect.Left < Max(ColLeft, 0)) or
        ( (NodeRect.Bottom + BottomRightCellContentMargin.Y) > ClientHeight ) or
        (NodeRect.Top < 0)
      );
    

    如果ShowOwnHint 为真,那么您应该返回节点的文本作为提示文本。否则,将提示文本留空。

    使用该代码的主要障碍是DoGetCellContentMargin 受到保护,因此您不能直接调用它。您可以编辑源代码以将其公开,也可以在自己的函数中复制其功能;如果您不处理 OnBeforeCellPaint 事件,那么它总是返回 (0, 0)。

    HitInfo 的数据来自于调用GetHitTestInfoAt

    【讨论】:

    • 嗨 Rob,抱歉,自从 TOndrej 的代码有效以来,我没有尝试过这个,还是谢谢大家!
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多