Duilib中指定文本字符串的字号大小,获取该文本的高度和宽度

    int nFont = 1;
	int nWidth = 60;
	int nHeight = 60;
	nFont = 1 + strText.length() / 6;
	SIZE szText = { 0 };
    HFONT hFont = m_PaintManager.GetFont(nFont);
	HFONT hOldFont = (HFONT)SelectObject(m_PaintManager.GetPaintDC(), (HGDIOBJ)hFont);
	if (!GetTextExtentPoint32(m_PaintManager.GetPaintDC(), strText.c_str(),             
        strText.length(), &szText))
	{
		ERROR("%s: GetTextExtentPoint32 Fail Error=%u\n", __FUNCTION__, GetLastError());
		szText.cx = strText.length() * 10;
		szText.cy = 30;
	}
	else
	{
		INFO("%s: text=%S, szText=[%d,%d]\n", __FUNCTION__, strText.c_str(), szText.cx, 
        szText.cy);
	}
	
	SelectObject(m_PaintManager.GetPaintDC(), (HGDIOBJ)hOldFont);

1.m_PaintManager.GetFont设定字符串的字号

2.执行SelectObject讲HFONT替换

3.GetTextExtentPoint32获取字符的大小,主要在szText上

4.SelectObject重新刷写一遍

深圳程序交流群550846167

发布了259 篇原创文章 · 获赞 67 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/linjingtu/article/details/88865984