如何计算字体高度?我为了使字符串显示在CRithEditCtrl中间

iProgram 2005-05-10 07:37:55
我的对话框上有一个不是很大的CRithEditCtrl. 剪贴板中有一个不是很长的字符串, 格式化成RTF格式,使用字体FontA (如Arial), 字号N (如12), 我想把这个字符串显示在CRithEditCtrl m_rtfData正中央, 原理是计算出该字体的高度h和单位宽度l, 则字符串长度 * l就是字符串屏幕长度, 用CRithEditCtrl::SetRect(LPRECT) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cricheditctrl.3a3a.setrect.asp)
就可以解决居中的问题. 但是如何得到某个字体在某字号下的屏幕高度和单位字符宽度呢?
...全文
262 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzheng318 2005-05-11
  • 打赏
  • 举报
回复
GetTextMetrics
This function fills the specified buffer with the metrics for the currently selected font.

BOOL GetTextMetrics(
HDC hdc,
LPTEXTMETRIC lptm
);
xqk 2005-05-11
  • 打赏
  • 举报
回复
或者用GetTextExtent() 函数试试,这个函数返回字符串的逻辑宽度和高度 一个CSIZE
但这个函数是CDC类里边的
xqk 2005-05-11
  • 打赏
  • 举报
回复
TEXTMETRIC
The TEXTMETRIC structure contains basic information about a physical font. All sizes are specified in logical units; that is, they depend on the current mapping mode of the display context.

typedef struct tagTEXTMETRIC {
LONG tmHeight;
LONG tmAscent;
LONG tmDescent;
LONG tmInternalLeading;
LONG tmExternalLeading;
LONG tmAveCharWidth;
LONG tmMaxCharWidth;
LONG tmWeight;
LONG tmOverhang;
LONG tmDigitizedAspectX;
LONG tmDigitizedAspectY;
TCHAR tmFirstChar;
TCHAR tmLastChar;
TCHAR tmDefaultChar;
TCHAR tmBreakChar;
BYTE tmItalic;
BYTE tmUnderlined;
BYTE tmStruckOut;
BYTE tmPitchAndFamily;
BYTE tmCharSet;
} TEXTMETRIC, *PTEXTMETRIC;
Members
tmHeight
Specifies the height (ascent + descent) of characters.
tmAscent
Specifies the ascent (units above the base line) of characters.
tmDescent
Specifies the descent (units below the base line) of characters.
tmInternalLeading
Specifies the amount of leading (space) inside the bounds set by the tmHeight member. Accent marks and other diacritical characters may occur in this area. The designer may set this member to zero.
tmExternalLeading
Specifies the amount of extra leading (space) that the application adds between rows. Since this area is outside the font, it contains no marks and is not altered by text output calls in either OPAQUE or TRANSPARENT mode. The designer may set this member to zero.
tmAveCharWidth
Specifies the average width of characters in the font (generally defined as the width of the letter x). This value does not include the overhang required for bold or italic characters.
tmMaxCharWidth
Specifies the width of the widest character in the font.
tmWeight
Specifies the weight of the font.
tmOverhang
Specifies the extra width per string that may be added to some synthesized fonts. When synthesizing some attributes, such as bold or italic, graphics device interface (GDI) or a device may have to add width to a string on both a per-character and per-string basis. For example, GDI makes a string bold by expanding the spacing of each character and overstriking by an offset value; it italicizes a font by shearing the string. In either case, there is an overhang past the basic string. For bold strings, the overhang is the distance by which the overstrike is offset. For italic strings, the overhang is the amount the top of the font is sheared past the bottom of the font.
The tmOverhang member enables the application to determine how much of the character width returned by a GetTextExtentPoint32 function call on a single character is the actual character width and how much is the per-string extra width. The actual width is the extent minus the overhang.

tmDigitizedAspectX
Specifies the horizontal aspect of the device for which the font was designed.
tmDigitizedAspectY
Specifies the vertical aspect of the device for which the font was designed. The ratio of the tmDigitizedAspectX and tmDigitizedAspectY members is the aspect ratio of the device for which the font was designed.
tmFirstChar
Specifies the value of the first character defined in the font.
tmLastChar
Specifies the value of the last character defined in the font.
tmDefaultChar
Specifies the value of the character to be substituted for characters not in the font.
tmBreakChar
Specifies the value of the character that will be used to define word breaks for text justification.
tmItalic
Specifies an italic font if it is nonzero.
tmUnderlined
Specifies an underlined font if it is nonzero.
tmStruckOut
Specifies a strikeout font if it is nonzero.
tmPitchAndFamily
Specifies information about the pitch, the technology, and the family of a physical font.
The four low-order bits of this member specify information about the pitch and the technology of the font. A constant is defined for each of the four bits.


CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm); //tmHeight 这个成员就是你要的高度了
//至于第二个参数,如果你用的是MFC封装好的类的话就不需要HDC句柄了,否则的话得自己创建HDC设备 用GetDC从窗口句柄中

HDC hdc;
hdc=GetDC(hwnd);
TEXTMETRIC tm;
dc.GetTextMetrics(hdc,&tm);
ReleaseDC(hwnd,hdc); //API的话最后别忘了释放DC
iProgram 2005-05-10
  • 打赏
  • 举报
回复
能否给个例子?GetTextMetrics第二个参数hdc用谁的?
jerry 2005-05-10
  • 打赏
  • 举报
回复
GetTextMetrics

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧