入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

mfc 得到在字符串在屏幕上的长度

创建时间:2017-09-13 投稿人: 浏览次数:148
应用举例:画图时将字符串居中,因此需提前知道字符串在屏幕的大小;

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)


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。