如何判断一个字符串占多少像素  ,又相关的函数么比如800*600的显示器上判断一列字符在屏幕上占的的宽度,随便什么字体,就是看看有没有什么方法

解决方案 »

  1.   

    你可以用GetTextMetrics得到字体宽度的像素,然后自己运算一下(注意w和i等宽度是不一样的,不过你也可以得到)
      

  2.   

    有个简单的方法就是你把这个字符串写到(利用canvas.textrect())一个paintbox上面,然后
    用一个循环扫描一下就可以知道结果了
    呵呵~~~
      

  3.   

    TCanvas.TextWidthprocedure TForm1.FormCreate(Sender: TObject);var
      i: Integer;
    begin
      with PageControl1 do
      begin
        for i := 0 to PageCount - 1 do
        begin
          if (Canvas.TextWidth(Pages[i].Caption) * 2) > TabWidth then
            TabWidth := Canvas.TextWidth(Pages[i].Caption) * 2;
          if (Canvas.TextHeight(Pages[i].Caption) * 2) > TabHeight then
            TabHeight := Canvas.TextHeight(Pages[i].Caption) * 2;
        end;  end;
    end;
      

  4.   

    TCanvas.TextWidthReturns the width, in pixels, of a string rendered in the current font. function TextWidth(const Text: string): Integer;
    function TextWidth(const Text: WideString): Integer;DescriptionUse TextWidth to determine the length a string will occupy in the image. TextWidth indicates whether a given string will fit in the available space. Other graphical elements in the image such as lines, or additional strings can be positioned to accommodate the width of the text.TextWidth returns the same value as TextExtent(Text).cx.
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);var iHeight,iWidth:integer;begin
      //self.Canvas.Font.Name:='宋体';  //设置字体
      //self.Canvas.Font.Size:=20;      //字体大小
      iHeight:=self.Canvas.TextHeight('1234567');
      iWidth:=self.Canvas.TextWidth('1234567');  ShowMessage('H:'+IntToStr(iHeight)+#13+'W:'+IntToStr(iWidth));
    end;
      

  6.   

    用canvas.TextWidth不就可以了吗。