Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.453 Beiträge
Delphi 12 Athens
|
AW: Memo, je nach Bedarf 1-3 Zeilen groß. Wie? [VCL]
9. Jun 2016, 15:21
Vielleicht so (sicher noch optimierbar):
Delphi-Quellcode:
type
TMemoHelper = class helper for TMemo
public
procedure AdjustHeight(MaxLines: Integer);
end;
procedure TMemoHelper.AdjustHeight(MaxLines: Integer);
var
border: Integer;
DC: HDC;
metrics: TTextMetric;
S: string;
saveFont: HFont;
size: TSize;
sysMetrics: TTextMetric;
W: Integer;
begin
WordWrap := false;
DC := GetDC(0);
try
GetTextMetrics( DC, sysMetrics);
saveFont := SelectObject( DC, Font.Handle);
GetTextMetrics( DC, metrics);
W := 0;
for S in Lines do begin
GetTextExtentPoint32( DC, S, Length(S), size);
W := Max(W, size.cx);
end;
SelectObject( DC, saveFont);
finally
ReleaseDC(0, DC);
end;
if NewStyleControls then begin
border := GetSystemMetrics(SM_CYBORDER) * IfThen(Ctl3D, 8, 6);
end
else begin
border := Min(sysMetrics.tmHeight, metrics.tmHeight) div 4 + GetSystemMetrics(SM_CYBORDER) * 4;
end;
if Lines.Count > MaxLines then begin
ScrollBars := ssVertical;
W := W + GetSystemMetrics(SM_CXVSCROLL);
end
else begin
ScrollBars := ssNone;
end;
Height := MaxLines * metrics.tmHeight + border;
Width := W + border;
end;
|
|
Zitat
|