The Delphi IDE Locals window clips the names of the local varia开发者_StackOverflow中文版bles if the Name column is too narrow like this:
I would like to do the same thing in my program. At present I am using a TMS THTMLTreeList and the results I am getting look like this:
Is it possible to clip the text like the IDE does?
Thanks for your help!
I don't know how to do it with a TMS treeview, but for the standard TTreeView you would have to owner-draw the text manually, then you can clip it however you want. I forget the name of it right now, but the RTL does have a function that draws text onto a TCanvas with a user-specified clipping width that draws the ellipses for you.
Try this:
function AbbrText(const AText : string; ACanvas : TCanvas; const AMaxWidthPixels : Integer) : string;
begin
Result := AText;
if ACanvas.TextWidth(Result) > AMaxWidthPixels do
begin
while (Length(Result) <> 0) and (ACanvas.TextWidth(Result + '...') > AMaxWidthPixels) do
SetLength(Result, Length(Result) - 1);
if Result <> '' then
Result := Result + '...';
end;
end;
I won't argue this is the most efficient code, but it should do what you need it to do. Pass your text and the THTMLTreeList canvas to the function and you'll get back the text that will fit into AMaxWidthPixels. If there isn't even enough room for the ellipsis then it will return an empty string.
Thank you all for your input. I'm guessing that DrawText is what I am looking for. The TMS Treelist seems to be pretty much the standard control with features for adding some HTML formatting to the text which might come in handy as this little project develops.
I'm afraid this is going to sound really lame, but I am not getting a hit on my event handler and I don't know why. I set this up to override the event handler for OnCustomDrawItem:
procedure TForm1.trXMLCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
var hContext: HDC;
s: PChar;
iLength: Integer;
uRect: TRect;
begin
DefaultDraw := False;
hContext := trXML.Canvas.Handle;
s := PChar(Node.Text);
iLength := Length(Node.Text);
uRect := Node.DisplayRect(True);
DrawText(hContext, s, iLength, uRect, DT_END_ELLIPSIS);
end;
It seems to me that this event should fire whenever a node needs to be redrawn. I am not seeing it fire when the tree is first loaded and also not when I change the size of the columns so that there is more or less overwriting. I don't know why there is no CustomDraw boolean property for this control, but I am sure that it is just something I am missing. If you have time, I would very much appreciate a little instruction in doing this correctly.
精彩评论