主题:一个字节等于多少个象素
zfxs_wang
[专家分:180] 发布于 2009-07-18 11:40:00
我在使用TreeView控件时,遇到这样一个问题:我希望子节点比父节点缩进二个字符,各个节点的Text文本右对齐.但由于各个Nodes对象的缩进宽度属性Indentaion是用象素来表示的,Nodes对象的Text属性是一些字符,因此无法实现自己的愿望.我想请教师傅们,一个字符的宽度等于多少个象素,或者Indentaion属性能用字符个数来表示.或者还有其他的办法解决我的问题.先谢了!
回复列表 (共3个回复)
沙发
北京惬意 [专家分:1330] 发布于 2009-07-18 13:17:00
字节和象素本身没有可比性,03年时我根据字体大小研究过这个问题,搞了一个表,也不好用。不知现在有没有好用的字体象素对比表。估计没有,因为不同字体同一字号的宽度也不相同。
板凳
zxl931 [专家分:3420] 发布于 2009-07-19 01:14:00
以下是美国的Andy Kramek的一个程序,可以获得字符串宽度(像素),经测试非常棒!
将以下代码保存为SIZESTR.PRG,调用方法:
lnLen = SizStr( tuInStr, tcFName, tnFSize, tcFStyle)
tuInStr 字符串
tcFName 字体名称 如:"Arial",“宋体”,“黑体”等
tnFSize 字号 如:9,10,11,12等
tcFStyle 字形
tcFStyle可以取以下值
字符 字形
B 粗体
I 斜体
N 常规
O 轮廓
Q 不透明
S 阴影
– 删除线
T 透明
U 下划线
比如:lnLen = SizeStr( "梅子论坛", "宋体", 9, "BI" )
lnLen的值为 52(像素)
***********************************************************************
* Program....: SIZESTR.PRG
* Author.....: Andy Kramek
* Date.......: 28 August 2001
* Notice.....: Copyright (c) 2001 Tightline Computers Ltd, All Rights Reserved.
* Compiler...: Visual FoxPro 07.00.0000.9262 for Windows
* Purpose....: Return the exact length of a string (in Pixels) in the specified font
* Prototype..: lnLen = SizStr( "This String", "Arial", 9, "BI" )
***********************************************************************
LPARAMETERS tuInStr, tcFName, tnFSize, tcFStyle
LOCAL lcInStr, lnLen, lnParams, lnRetVal
lnParams = PCOUNT()
**********************************************************************
*** Set Default Values if nothing passed
*** Default style is "Arial", 9
**********************************************************************
IF lnParams < 4
tcFStyle = ""
IF lnParams < 3
tnFSize = 9
IF lnParams < 2
tcFName = "Arial"
IF lnParams < 1
tuInStr = "1"
ENDIF
ENDIF
ENDIF
ENDIF
**********************************************************************
*** Convert the input string to character equivalent
**********************************************************************
lcInStr = TRANSFORM( tuInStr )
lnLen = LEN( lcInStr )
**********************************************************************
*** Get the exact length in Pixels
**********************************************************************
lnRetVal = INT( TXTWIDTH( lcInStr, tcFName, tnFSize, tcFStyle) * ;
FONTMETRIC(6, tcFName, tnFSize, tcFStyle))
**********************************************************************
*** Display full details in a simple form
**********************************************************************
loParams = CREATEOBJECT( 'line' )
WITH loParams
.AddProperty( 'cString', lcInStr )
.AddProperty( 'nChars', lnLen )
.AddProperty( 'cFont', tcFName )
.AddProperty( 'cStyle', tcFStyle )
.AddProperty( 'nSize', tnFSize )
.AddProperty( 'nMax', lnLen * FONTMETRIC( 7, tcFName, tnFSize, tcFStyle ) )
.AddProperty( 'nAvg', lnLen * FONTMETRIC( 6, tcFName, tnFSize, tcFStyle ) )
.AddProperty( 'nExact', IIF(ISBLANK(lcInStr), "N/A", TRANSFORM( lnRetVal )) )
.AddProperty( 'nHeight', FONTMETRIC( 1, tcFName, tnFSize, tcFStyle ) )
*** Add the Border Factor to the height (28.125% seems about right)
.nHeight = .nHeight + INT(.nHeight * 0.28125)
ENDWITH
**********************************************************************
*** And return the length
**********************************************************************
RETURN lnRetVal
3 楼
liupeisong [专家分:2340] 发布于 2009-07-20 00:34:00
LZ只要知道一个事实就够了:就算是英文字体,其中也有一些是等宽的!所以不要再纠缠于像素了
我来回复