win32下绘制多行文本

来源:互联网 发布:access数据库的用途 编辑:程序博客网 时间:2024/04/28 08:30

1、创建字体

LOGFONT lf = { 0 };::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);_tcsncpy(lf.lfFaceName, fontName, LF_FACESIZE);lf.lfCharSet = DEFAULT_CHARSET;lf.lfHeight = -fontHeight;lf.lfWeight += FW_BOLD;HFONT hFont = ::CreateFontIndirect(&lf);


fontName为字体名称,例如,“宋体”;字符集采用默认字符集;FW_BOLD表示加粗显示;lf.lfHeight表示字体高度,设置时是负数。

2、绘制文本

SelectObject(hdc, hFont);::DrawText(hdc, text, -1, &r, DT_NOPREFIX|DT_LEFT);DeleteObject(hFont);


3、绘制多行文本

void DrawMultiLine(HDC hdc,CString str, int* pos, LPCTSTR fontName,int fontHeight){   int comsumed = 0;int len;RECT r;str.Trim();len = str.GetLength();    LOGFONT lf = { 0 };::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);_tcsncpy(lf.lfFaceName, fontName, LF_FACESIZE);lf.lfCharSet = DEFAULT_CHARSET;lf.lfHeight = -fontHeight;HFONT hFont = ::CreateFontIndirect(&lf);if( hFont == NULL ) return;SelectObject(hdc, hFont);while(comsumed < len){   r.top = *pos;r.left = page_left, r.right = mWidth;r.bottom = *pos + text_height + TEXT_BOTTOM;DRAWTEXTPARAMS drawParams;  ZeroMemory(&drawParams, sizeof(DRAWTEXTPARAMS));  drawParams.cbSize = sizeof(DRAWTEXTPARAMS);  str = str.Right(len - comsumed);DrawTextEx(hdc,str.GetBuffer(), str.GetLength(), &r,   DT_LEFT | DT_EDITCONTROL | DT_WORDBREAK | DT_EXTERNALLEADING | DT_NOPREFIX, &drawParams);  comsumed += drawParams.uiLengthDrawn;*pos += TEXT_HEIGHT + TEXT_BOTTOM;}DeleteObject(hFont);}

DRAWTEXTPARAMS drawParams 这个参数获取当前行绘制了多少字符,mWidth为行宽。如果字符串没有绘制完,继续下一行。




0 0