Windows API一日一练(30)GetTextMetrics函数
来源:互联网 发布:网络运营工作要求 编辑:程序博客网 时间:2024/05/14 02:46
在做报表里,经常要把输出的内容进行错落有致的排列,让用户看起来更加舒服。比如使用标题的字体输出后,再使用其小号的字体进行输出。这样就需要知道每种字体的高度,才让两行文字输出不重叠在一起,就需要计算每种字体的高度。这时就需使用GetTextMetrics函数来获取字体的高度。
函数GetTextMetrics声明如下:
#ifndef NOTEXTMETRIC
WINGDIAPI BOOL WINAPI GetTextMetricsA( __in HDC hdc, __out LPTEXTMETRICA lptm);
WINGDIAPI BOOL WINAPI GetTextMetricsW( __in HDC hdc, __out LPTEXTMETRICW lptm);
#ifdef UNICODE
#define GetTextMetrics GetTextMetricsW
#else
#define GetTextMetrics GetTextMetricsA
#endif // !UNICODE
hdc是当前设备的句柄。
lptm是获取当前字体属性的保存结构。它的结构定义如下:
typedef struct tagTEXTMETRICW
{
LONG tmHeight;
LONG tmAscent;
LONG tmDescent;
LONG tmInternalLeading;
LONG tmExternalLeading;
LONG tmAveCharWidth;
LONG tmMaxCharWidth;
LONG tmWeight;
LONG tmOverhang;
LONG tmDigitizedAspectX;
LONG tmDigitizedAspectY;
WCHAR tmFirstChar;
WCHAR tmLastChar;
WCHAR tmDefaultChar;
WCHAR tmBreakChar;
BYTE tmItalic;
BYTE tmUnderlined;
BYTE tmStruckOut;
BYTE tmPitchAndFamily;
BYTE tmCharSet;
} TEXTMETRICW;
调用这个函数的例子如下:
#001 //
#002 //界面显示输出.
#003 //
#004 //蔡军生 2007/09/06 QQ:9073204 深圳
#005 //
#006 void CCaiWinMsg::OnDraw(HDC hDC)
#007 {
#008 //
#009 std::wstring strShow(_T("C++窗口类的实现,2007-09-04"));
#010
#011 //设置输出字符串的颜色.
#012 COLORREF crOld = SetTextColor(hDC,RGB(255,0,0));
#013
#014 RECT rcText;
#015
#016 //设置新字体.
#017 HGDIOBJ hOldFont = SelectObject(hDC,GetFont());
#018
#019 //获取当前字体的高度.
#020 TEXTMETRIC tmFont;
#021 if (GetTextMetrics(hDC,&tmFont))
#022 {
#023 rcText.left = 10;
#024 rcText.top = 40;
#025 rcText.right = 300;
#026 rcText.bottom = rcText.top + tmFont.tmHeight;
#027 }
#028 else
#029 {
#030 rcText.left = 10;
#031 rcText.top = 40;
#032 rcText.right = 300;
#033 rcText.bottom = 80;
#034 }
#035
#036 //创建黑色的画刷,
#037 HBRUSH hbrush = CreateSolidBrush(RGB(0, 0, 0));
#038
#039 //用黑色的画刷填充四边形的颜色.
#040 FillRect(hDC,&rcText,hbrush);
#041
#042 //删除画刷.
#043 DeleteObject(hbrush);
#044
#045 //设置透明背景
#046 int nOldMode = SetBkMode(hDC,TRANSPARENT);
#047
#048 //显示字符串在四边形的中间位置.
#049 DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#050 DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
#051
#052 //恢复原来的字体.
#053 HGDIOBJ hFont = SelectObject(hDC,hOldFont);
#054 DeleteObject(hFont);
#055
#056 //恢复原来的模式.
#057 SetBkMode(hDC,nOldMode);
#058
#059 //恢复原来的颜色.
#060 SetTextColor(hDC,crOld);
#061 }
#062
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练(30)GetTextMetrics函数
- Windows API一日一练
- Windows API一日一练
- Windows API一日一练(69)GetTickCount函数 计时函数
- FPGA 工程师的要求
- http://blog.csdn.net/tanmingbo/archive/2007/09/07/1776373.aspx
- TestComplete对数据库的支持
- 关于directshow的开发常见问题
- Visual C# 2005编程技巧大全
- Windows API一日一练(30)GetTextMetrics函数
- mysql 和 sql server 的执行脚本对比
- 筛选网页信息获取天气预报
- 跳操
- Open Session In View
- iPhone旋风背后 乔布斯的成功哲学
- 代码片段 拖放
- 写个vc工具
- 用数据源访问数据库