开发者

Virtual StringTree: How to determine if the node text is completely shown?

When TVirtualStreeTree.HintMode = hmTooltip, the node text will become the hint text when the mouse is hovered over a node and column where the node text is not completely shown. But I have to set HintMode = hmHint, so that I can in the even handler supply various hint text based on the position the current mouse cursor is, and in that HintMode the hint text is not generated automatically.

My question is how to know if the a node text 开发者_开发百科is shown completely or not, so that I know should I supply the node text or empty string as the hint text?

Thanks.


You can call TBaseVirtualTree.GetDisplayRect to determine the text bounds of a node. Depending on the Unclipped parameter, it will give you the full or actual text width. TextOnly should be set to 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;

Note that the function will implicitly initialize the node if it's not been initialized yet.


You can use what the tree control itself uses. Here's an excerpt from the cm_HintShow message handler for single-line nodes when hmTooltip mode is in effect.

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)
  );

If ShowOwnHint is true, then you should return the node's text as the hint text. Otherwise, leave the hint text blank.

The main obstacle with using that code is that DoGetCellContentMargin is protected, so you can't call it directly. You can either edit the source to make it public, or you can duplicate its functionality in your own function; if you aren't handling the OnBeforeCellPaint event, then it always returns (0, 0) anyway.

The HitInfo data comes from calling GetHitTestInfoAt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜