mfc 得到在字符串在屏幕上的长度
应用举例:画图时将字符串居中,因此需提前知道字符串在屏幕的大小;
CString str = _T("啊");
HDC hdc = ::GetDC(NULL);
CSize size;
size.cx =0; size.cy =0;
GetTextExtentPoint32(hdc,str,(int)_tcslen(str),&size);
一个量取弹出菜单尺寸的例子
void CSystemTray::MeasurePopupMenuTitle(LPMEASUREITEMSTRUCT lpMi)
{
LPCTSTR lpszMenuTitle = (LPCTSTR)lpMi->itemData;
// create the font we will use for the title
HFONT font = CreatePopupMenuTitleFont();
if (font)
{
// get the screen dc to use for retrieving size information
CDC dc;
HDC hDc = ::GetDC(NULL);
dc.Attach(hDc);
// select the title font
CFont* old = (CFont*)dc.SelectObject(CFont::FromHandle(font));
// compute the size of the title
CSize size = dc.GetTextExtent(lpszMenuTitle);
// deselect the title font
dc.SelectObject(old);
// delete the title font
::DeleteObject(font);
::ReleaseDC(NULL, hDc);
// add in the left margin for the menu item
size.cx += GetSystemMetrics(SM_CXMENUCHECK)+8;
// return the width and height
lpMi->itemWidth = size.cx;
lpMi->itemHeight = size.cy;
}
} // MeasurePopupMenuTitle(LPMEASUREITEMSTRUCT)
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
