A TVirtualStringTree object with custom node height, How to reliably scroll Virtual TreeView to the bottom (i.e. the scrollbar gets to the bottom)?
I tried calling tree1.FullExpand then tree1.ScrollIntoView.(tree1.GetLast), but it does not work.
Thank you in advance.
ScrollIntoView works well for me. You can also try tree1.FocusedNode := tree1.GetLast;
Are you setting custom node height in OnMeasureItem event?
If it doesn't work, try to set tree's DefaultNodeHeight to the bigger value and in OnMeasureItem event change it to lower. I noticed that tree recalculates scrollbar's length better that way.
Try this:
SendMessage(VST.Handle, WM_VSCROLL, SB_BOTTOM, 0);
PostMessage(VST.Handle, WM_VSCROLL, SB_BOTTOM, 0);
I've had the same problem working with TVirtualDrawTree's. You have to make sure that node heights are computed before the tree actually scrolls.
This is what I do:
1.- Add this code to the OnInitNode event so that the tree knows that the height of the new node must be computed:
Node.States := node.States + [vsMultiline] - [vsHeightMeasured];
2.- In the OnMeasureItem, if you can't compute the height (e.g. node not initialized yet), make sure you tell the tree to repeat the call when needed:
In the OnMeasureItem event handler:
If (Node = Nil) Or (Node = tree.RootNode) Then Begin
Exclude(Node.States, vsHeightMeasured);
Exit;
End;
NodeData := tree.GetNodeData(Node);
If (NodeData = Nil) Or (NodeData^.XMLNode = Nil) Then Begin
Exclude(Node.States, vsHeightMeasured);
Exit;
End;
Try
// Code to measure node height here.
Except
Exclude(Node.States, vsHeightMeasured);
End;
I hope it helps you.
This also should work:
tree1.TopNode := tree1.GetLast
Related
I need to be able to get and set the top item in a listbox - something like a ListBox.TopItem property would be great. I have been unable to find anything that does this job. Any ideas would be greatly appreciated please.
Edit:
For example:
Listbox1 and ListBox2 items are the following:
1 Data1
2 Data2
3 More Data
4 More again
5 Yet more
6 and this will do.
Showing in 2 listboxes, both 3 items high:
1 Data1
2 Data2
3 More Data
and I want to programmatically make them show
3 More Data
4 More again
5 Yet more
and I want to find out what item the top one is.
(previous answer text deleted)
Update after clarification of question.
Ok, so you want
a) to scroll the list programmatically
b) to read the topmost item programmatically
The following scrolls the list so that a given item becomes the top visible item. Note however, that the list can not be scrolled up beyond the point that there would be empty space between the last item and the viewport's bottom.
procedure TForm25.SetTopVisibleItem(idx: integer);
var
ptf: TPointF;
begin
ptf := PointF(0, idx * ListBox1.ItemHeight);
ListBox1.ViewportPosition := ptf;
end;
To get the current topmost item in the viewport:
function TForm25.GetTopVisibleItem: TListBoxItem;
var
x, y: single;
begin
x := 3; // a small offset is required to assure that
y := 3; // the point is within an item
result := ListBox1.ItemByPoint(x, y);
end;
As shown in figure, I need to make a list with buttons on each item, one button at down left of the item, and some on the down right.
I Make the demo app using ListBox control and some Buttons within Panel above on ListBox, but when the ListBox scrolling, it's difficult to make the Buttons follow the ListItem.
who can help, thanks~~~
I got the way to make it!so, I'll post the answer myself~, but Remy Lebeau inspired me.
in the beginning, I used DrawFrameControl() to make button on list, It works but the style was look like classic windows style, and it's hard to make back color like the pic in the example.
then, I used FillRect() and DrawEdge() make the Button, I think it's well, here is the code:
hitPoint := lst1.ScreenToClient(Mouse.CursorPos);
// there is a btnRect var of the Button Rect
edgeRect.Left := btnRect.Left - 1;
edgeRect.Top := btnRect.Top - 1;
edgeRect.Right := btnRect.Right + 1;
edgeRect.Bottom := btnRect.Bottom + 1;
// make button
lst1.Canvas.FillRect(btnRect);
// make edge, FListMouseDown is bool var and setting value at MouseDown/MouseUp Event
//
if PtInRect(edgeRect, hitPoint) and FListMouseDown then begin
DrawEdge(lst1.Canvas.Handle, edgeRect, EDGE_ETCHED, BF_RECT); // button down style
end else begin
DrawEdge(lst1.Canvas.Handle, edgeRect, EDGE_RAISED, BF_RECT);
end;
The following work is store the Rect of Buttons in memory, write the ButtonOnClick event code, and calling ButtonOnClick event at ListMouseUp() event after judge if Mouse Hit Position is in the Button Rect, The Code is not important like the above drawing Buttons, so it is omitted
I am trying to position something w.r.t. a TJvTreeView, and I would like to know both the width and presence of the vertical scroll view. Does anyone know the canonical way to do that? Or failing that, any way that works?
Test for the presence of the WS_VSCROLL window style:
HasVertScrollBar := (GetWindowLongPtr(hWnd, GWL_STYLE) and WS_VSCROLL) <> 0;
To find the width of system scroll bars, call SystemParametersInfo passing SPI_GETNONCLIENTMETRICS.
var
ncm: TNonClientMetrics;
....
ncm.cbSize := SizeOf(ncm);
Win32Check(
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, Pointer(#ncm), 0)
);
The scroll bar width can then be retrieved from ncm.iScrollWidth.
Why does a Delphi StringGrid sometimes calls the OnClick event after an OnKeyDown?
Debugging screenshot:
My OnKeyDown event handler:
var
Top: Integer;
Bottom: Integer;
CurrentRow: Integer;
begin
Top := Grid.TopRow;
Bottom := Grid.TopRow + Grid.VisibleRowCount - 1;
if (Key = 38) then CurrentRow := Grid.Row - 1
else if (Key = 40) then CurrentRow := Grid.Row + 1;
// Disable OnClick because sometimes a 'TStringGrid.Click' is called anyway...
// (when clicking on form top window bar and navigating)
Grid.OnClick := nil;
if (CurrentRow < Top - 1) or (CurrentRow > Bottom + 1) then begin
if (Key = 38) then Grid.Row := Bottom
else if (Key = 40) then Grid.Row := Top;
end;
Grid.OnClick := GridClick;
end;
Edit:
It seems the 'OnClick' is not being fired when the last line is selected and pushing 'Down' or when the first line is selected and pushing 'Up'.
Way to reproduce:
Add a TStringGrid to a form and populate with a few lines. Add 'OnClick' and 'OnKeyDown' handler. No specific code needs to be added in these two handler methods. Select a row in the stringgrid on the form and press the up or down arrow on your keyboard.
Edit 2:
This isn't the solution, but to prevent the code in 'OnClick' being executed after pressing up, down, pageup or pagedown, I set a variable in 'OnKeyDown' what key was pressed and check for that variable in 'OnClick'.
Edit 3:
Updated stack trace and way to reproduce.
Well, that hard-coded key codes aren't the case making the least bit transparent, but you witness this effect when you use a direction key (up, down, left, etc...) to change the selection.
Why the OnClick event handler is called, is because TCustomGrid.OnKeyDown calls TCustomGrid.FocusCell, which calls Click.
Exactly why changing focus to another cell would establish a click I do not know, we would have to ask the developers I imagine. Perhaps to simulate the default behaviour when changing focus to another cell by clicking instead of keyboard.
Since you seem to handle direction key presses yourself, maybe you could consider to prevent this from happening at all by ignoring the key any further:
if Key in [VK_PRIOR..VK_DOWN] then
Key := 0;
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.