怎样一个字符串的宽度

解决方案 »

  1.   

    获取字符串中字符的个数用 Length函数,如:
    showmessage('中国人'+#13+'的长度为:'+Inttostr(Length('中国人')));
    获取字符串在画布中所需的长度用Canvas.TextWidth()方法
      

  2.   

    不是他的字符数,是宽度(width)
      

  3.   

    在扩展acii,unicode等情况下,字符串的字符个数并不等于内存中的宽度用SizeOf吧
      

  4.   

    楼主该不是问字符显示的宽度吧,如18pix
      

  5.   

    用TCANVAS的TEXTWIDTH方法.TEXTWIDTH('ABC');
      

  6.   

    好问题,帮你找人来
    类似的问题,怎么知道一个字符串中的字符个数,包括unicode
      

  7.   

    SizeOf(String1)返回的是指针在内存的宽度,都是4
      

  8.   

    SizeOf(String1)永远都是四个字节,而且在初始时,它就会指向四个字节,其实说的更确切一些的话应该是  StringValue 是一个指针,指针有宽度吗?没有。试一下:
    type
      PInteger=^Integer;
    var
      P:Pointer;
      PI:PInteger;
      vInt : Integer;
      vStrValue : String;
    begin
      vStrValue := 'asfsadfsad'; 
      P := @vStrValue;
      ShowMessage(IntToStr(PInteger(P)^));//----------------------
    你用类如的方法看一下它的首地址和末尾地址,找差值。
      

  9.   

    前面有人说了,用canvas.textwidth()就可以得到字符串的像素宽度。
    如果要实际的宽度(毫米)可以用getdevicecaps获得输出设备的分辨率,然后换算。
      

  10.   

    楼上说得没错, Canvas.TextWidth 就是计算出的宽度 in pixels
    补一点, 字符的 dpi 在 windows 下始终是 72dpi
    Font.Size = -Font.Height * 72 / Font.PixelsPerInch
    这是 borland 的字符公式
      

  11.   

    再楼上的小小兄, Length 就可以得到正确的宽度, 但楼主问的不是这, 是 Width
      

  12.   

    获取字符串在画布中所需的长度用Canvas.TextWidth()方法
      

  13.   

    我试过了
    获取字符串在画布中所需的长度用Canvas.TextWidth()方法
    是对的
    不过不知道楼主要的是不是这个意思
    我本来以为楼主是要fun()得到像字符串'test测试',fun('test测试')得到6
    大家懂我的意思了吗??length('test测试')得到的是8
      

  14.   

    to :天才弟弟哦, 想得到 fun('test测试') 得到6, 本来是 unicode 支持的, see 这段代码如下var
      a :string;       // ansi string
      w :WideString;   // wide string(unicode string)
    begin
      a := 'test测试';
      w := a;
      ShowMessage('a length:' + IntToStr(Length(a)) + #13 +
        'w length:' + IntToStr(Length(w)));
    end;那就是你想得到的效果
    但... Borland 对 unicode 的支持不够全面,在一些情况下仍存在问题..
      

  15.   

    对上面作一个小补充。
    还乘以字号大小。
    var
      a :string;       // ansi string
      w :WideString;   // wide string(unicode string)
      fFontSize:integer;
    begin
      a := 'test测试';
      w := a;
      fFontSize:=Form1.Font.Size;
      ShowMessage('a length:' + IntToStr(Length(a)*fFontSize) + #13 +
        'w length:' + IntToStr(Length(w)*fFontSize)));
    end;
      

  16.   

    谢谢太可怕哈~~~
    font.size都是8啊,学习
      

  17.   

    哈哈哈哈哈哈~~~我懂了!!
    Font.Size = -Font.Height * 72 / Font.PixelsPerInch因为screen的坐标系是左上角为(0,0),字符的top比bottom要小,所以height是negative啊,所以delphi的帮助里有说
    When the Size property has a positive value, the Height property has a negative value. When the Height property has a positive value, the Size property has a negative value.不过什么时候size为negative就不了解了!!!Use Size to specify the point size of the font. If the value is negative, the internal leading that appears at the top of each line of text is included. If the value is positive, Size represents the height of the characters but not the internal leading.里面的internal leading也不知道是什么意思~~~太可怕再说啊!!
      

  18.   

    晕啊,这已经离楼主的题远了internal leading 是字符顶端的一个空白, 相对最高字符而定, 如果Height是负,那么这一行的高度不定,以最高的为准,好像这点只在 rich edit 有意义, 如果是正的高度确定在最高字符上, 当然中文是没有这 internal leading 的size 和 Height 总是相反的
      

  19.   

    楼主结贴!!!
    keke,偶生角了