gpt4 book ai didi

delphi - 自动调整备忘录大小

转载 作者:行者123 更新时间:2023-12-02 01:37:26 46 4
gpt4 key购买 nike

Possible Duplicate:
Can I make a TMemo size itself to the text it contains?

需要自动调整大小备忘录:高度和宽度。

我按如下方式自动调整高度:

function TForm1.AutoSizeMemoY(Memo: TMemo): word;
begin
Canvas.Font := Memo.Font;
Result := Canvas.TextExtent(Memo.Lines.Strings[0]).cy * Memo.Lines.Count +
Canvas.TextExtent(Memo.Lines.Strings[0]).cy;
end;

但我不知道如何自动调整宽度。我有一个想法:如果滚动条被激活,则增加宽度直到它变得不活动,但我不知道如何实现。

最佳答案

不是最好的解决方案,但它有效:

function GetTextWidth(F: TFont; s: string): integer;
var
l: TLabel;
begin
l := TLabel.Create(nil);
try
l.Font.Assign(F);
l.Caption := s;
l.AutoSize := True;
result := l.Width + 8;
finally
l.Free;
end;
end;

并将以下代码添加到 this answer 中的 Memo1.Onchange 事件末尾

  LineInd := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);//focused Memo1 line Index
Wd := GetTextWidth(Memo1.Font, Memo1.Lines[LineInd]);
//MaxWidthLineInd = index of the line which has the largest width.
//Init value of MaxWidthLineInd = 0
if MaxWidthLineInd = LineInd then
Memo1.Width := Wd
else begin
if Wd > Memo1.Width then
begin
Memo1.Width := Wd;
MaxWidthLineInd := LineInd;
end;
end;

关于delphi - 自动调整备忘录大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11116961/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com