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

青春壹個敷衍的年華 提交于 2019-12-07 15:53:38

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2099130/virtual-stringtree-how-to-determine-if-the-node-text-is-completely-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!