绘画多行文字,设置行高,省略号

一、按段落显示,末尾省略

int  DrawMultiLine(CDC *pDC, const CString& text, CRect rcText, int lineHeight, BOOL isCalCrect)  
{  
    int textHeight(0);


    if (!pDC)  
    {  
        return textHeight;  
    }  


    CDC dcMem; 
    CBitmap bmp; 
    dcMem.CreateCompatibleDC(pDC);
    CRect rect;
    GetClientRect(rect);
    bmp.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height()); 
    dcMem.SelectObject(&bmp); 
    if (isCalCrect)
    {
        pDC = &dcMem;
    }
   
    int lastStartPos = 0;  
    int textLen = text.GetLength();  
    int lineCount = rcText.Height() / lineHeight;  
    CRect rcSubText(rcText);  


    for (int i(0); i < lineCount; ++i)  
    {  
        rcSubText.top = rcText.top + lineHeight*(i);  
        rcSubText.bottom = rcSubText.top + lineHeight;  


        CString subText = text.Mid(lastStartPos);  
        if (i < lineCount - 1)  
        {  
            DRAWTEXTPARAMS drawParams;  
            ZeroMemory(&drawParams, sizeof(DRAWTEXTPARAMS));  
            drawParams.cbSize = sizeof(DRAWTEXTPARAMS); 
            UINT nFormat = DT_LEFT | DT_EDITCONTROL | DT_WORDBREAK | DT_EXTERNALLEADING | DT_NOPREFIX;
            int height = pDC->DrawTextEx(subText, rcSubText, nFormat, &drawParams); 
            textHeight += lineHeight;
            if (drawParams.uiLengthDrawn > 0)  
            {  
                lastStartPos += drawParams.uiLengthDrawn;  
            }  
            // 文本绘制已经完成  
            if (lastStartPos >= textLen)  
            {  
                break;  
            }  
        }  
        // 最后一行
        else  
        {  
            UINT nFormat = DT_LEFT | DT_END_ELLIPSIS | DT_EDITCONTROL | DT_EXTERNALLEADING | DT_NOPREFIX;
            if (isCalCrect)
            {
                nFormat |= DT_CALCRECT;
            }
            int height = pDC->DrawText(subText, rcSubText, nFormat);  
            if (lineHeight < height)
            {
                textHeight += lineHeight;
            }
            // 认为最后一行,不需要空白
            else
            {
                textHeight += height;    
            }
        }  
    } 


    dcMem.DeleteDC();
    bmp.DeleteObject();


    return textHeight;  
}  
void CDlg***::OnPaint()
{
    CPaintDC dc(this);  
    dc.SelectObject(&m_font);


    CRect rect(12,36,240,126);


    if (m_text.Find('\n') < 0)
    {
        CSize size = dc.GetTextExtent(m_text);
        if (size.cx < 240 -12)
        {
            dc.DrawText(m_text, rect, DT_SINGLELINE | DT_CENTER |DT_VCENTER | DT_WORDBREAK| DT_EDITCONTROL);
            return;
        }
    }


    CRect rc(12,37,240,120); 
    int height = DrawMultiLine(&dc, m_text,rc, 18, TRUE);
    if(rect.Height() > height) 
    {
        rect.top += (rect.Height() - height)/2; 
    }


    DrawMultiLine(&dc, m_text, rect, 18);
}


二、按行显示并省略(没有优化)

int DrawMultiLine(CDC *pDC, const CString& text, CRect rcText, int lineHeight, BOOL isCalCrect)  
{  
    int textHeight(0);


    if (!pDC)  
    {  
        return textHeight;  
    }  


    CDC dcMem; 
    CBitmap bmp; 
    dcMem.CreateCompatibleDC(pDC);
    CRect rect;
    GetClientRect(rect);
    bmp.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height()); 
    dcMem.SelectObject(&bmp); 
    if (isCalCrect)
    {
        pDC = &dcMem;
    }


    int lastStartPos = 0;  
    int textLen = text.GetLength();  
    int lineCount = rcText.Height() / lineHeight;  
    CRect rcSubText(rcText);  


    int isFindLineEnd(-1);
    CString textTemp(text);
    isFindLineEnd = textTemp.Find('\n');
    int i(0);
    while (isFindLineEnd > -1)
    {
        rcSubText.top = rcText.top + lineHeight*(i);  
        rcSubText.bottom = rcSubText.top + lineHeight;


        CString subText = textTemp.Left(isFindLineEnd);  
        textTemp = textTemp.Mid(isFindLineEnd + 1);  


        DRAWTEXTPARAMS drawParams;  
        ZeroMemory(&drawParams, sizeof(DRAWTEXTPARAMS));  
        drawParams.cbSize = sizeof(DRAWTEXTPARAMS); 
        UINT nFormat = DT_LEFT | DT_END_ELLIPSIS | DT_EDITCONTROL | DT_WORDBREAK;
        int height = pDC->DrawTextEx(subText, rcSubText, nFormat, &drawParams); 
        textHeight += lineHeight;
        
        isFindLineEnd = textTemp.Find('\n');
        ++i;
    }  
    // 最后一行
    {  
        UINT nFormat = DT_LEFT | DT_END_ELLIPSIS | DT_EDITCONTROL;
        if (isCalCrect)
        {
            nFormat |= DT_CALCRECT;
        }
        int height = pDC->DrawText(textTemp, rcSubText, nFormat);  
        if (lineHeight < height)
        {
            textHeight += lineHeight;
        }
        // 认为最后一行,不需要空白
        else
        {
            textHeight += height;    
        }
    }  




    dcMem.DeleteDC();
    bmp.DeleteObject();


    return textHeight;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值