12

これが私の問題です。テキストの実際の長さをピクセル単位で知りたいのです(フォントによっては、さまざまな文字の長さが異なることに注意してください)。これを使用して、DBGridの列幅をより適切に調整します。

4

2 に答える 2

29

Canvas.TextWidthおよびCanvas.TextHeight関数を使用できます 。

オプション1、コントロールのキャンバスを使用

WidthInPixels := Label1.Canvas.TextWidth('My Text');

オプション2、一時的なキャンバスの作成(Tbitmapを使用)

Function GetWidthText(const Text:String; Font:TFont) : Integer;
var 
  LBmp: TBitmap; 
begin
  LBmp := TBitmap.Create;
  try
   LBmp.Canvas.Font := Font;
   Result := LBmp.Canvas.TextWidth(Text); 
  finally
   LBmp.Free;
  end;
end;
于 2009-10-14T07:29:16.703 に答える
6

Delphiコンポーネントに「Canvas」プロパティがある場合は、Component.Canvas.TextWidthを使用できます。例:DBGridのテキストの幅を取得するには、次を使用できます。

DBGrid1.Canvas.TextWidth('Stack'); 

この問題に関する完全なリファレンスは次のとおり です。Delphi文字列の長さ(ピクセル単位)

于 2009-10-14T07:32:17.183 に答える