请教,如何实现多行文本在一个指定RECT内的绘制?

curry913 2003-10-20 04:02:59
向各位高手请教,如何实现多行文本在一个指定RECT内的绘制,并能够实现文本在水平及垂直方向上的对齐?
...全文
81 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
curry913 2003-10-26
  • 打赏
  • 举报
回复
希望有遇到这方面问题的朋友可以一起来讨论一下
dzqsuper 2003-10-25
  • 打赏
  • 举报
回复
........................
....................
................
............
........
....
.
我顶顶顶顶顶
curry913 2003-10-25
  • 打赏
  • 举报
回复
用DT_CALCRECT选项时,如果文字过长或为汉字,用时较长,当绘制表格时延迟过于明显
蒋晟 2003-10-24
  • 打赏
  • 举报
回复
void CFlxRptText::Draw(Gdiplus::Graphics* pGraphics,CDrawContext* pDrawContext,CFlexReportView* pView)
{
//draw text
CRect position=GetPosition();
_bstr_t bstrText;
_bstr_t bstrTag=(*this)->GettagName();
if(bstrTag==CFlxRptObj::GetObjTagName(TextObj)){
bstrText=GetAttributeString("Text");
}
else
bstrText=GetAttributeString("Data");

if(bstrText.length()>0){
_bstr_t bstrFontName=GetAttributeString("Font");
int nFontSize=GetAttributeLong("nFontSize");
int nFontStyle=GetAttributeLong("nFontStyle");
if(nFontSize==0)
nFontSize=pDrawContext->nPageFontSize;

Gdiplus::FontFamily fontFamilyText(bstrFontName);
Gdiplus::Font fontText(&fontFamilyText,(Gdiplus::REAL)nFontSize,nFontStyle,Gdiplus::UnitPoint);
Gdiplus::Font *pFontText=&fontText;

if(bstrFontName==_bstr_t("Default")||pFontText->GetLastStatus()!=Gdiplus::Ok){
pFontText=pDrawContext->pFontDefault;
}
;

Gdiplus::RectF rectF=CGlobal::g_GdiplusRectFFromRect(position);
int nHorizontalAlign=GetAttributeLong("HorizontalAlign");
int nVerticalAlign=GetAttributeLong("VerticalAlign");
switch(nHorizontalAlign){
default:
case 0:
pDrawContext->StringFormatDefault.SetAlignment( Gdiplus::StringAlignmentNear);break;
case 1:
pDrawContext->StringFormatDefault.SetAlignment(Gdiplus::StringAlignmentCenter);break;
case 2:
pDrawContext->StringFormatDefault.SetAlignment( Gdiplus::StringAlignmentFar );break;
}
switch(nVerticalAlign){
default:
case 0:
pDrawContext->StringFormatDefault.SetLineAlignment(Gdiplus:: StringAlignmentNear);break;
case 1:
pDrawContext->StringFormatDefault.SetLineAlignment(Gdiplus::StringAlignmentCenter);break;
case 2:
pDrawContext->StringFormatDefault.SetLineAlignment(Gdiplus:: StringAlignmentFar );break;
}
pGraphics->DrawString(bstrText, -1, pFontText, rectF, &pDrawContext->StringFormatDefault, &pDrawContext->BrushText);
}
enum enumBorder{Border_Top,Border_Bottom,Border_Left,Border_Right,Border_Max};
int nBorderThickness=GetAttributeLong("BorderThickness");
int nLineStyle[Border_Max];
Gdiplus::Point ptStart[Border_Max];
Gdiplus::Point ptEnd[Border_Max];

nLineStyle[Border_Top]=GetAttributeLong("TopLineStyle");
nLineStyle[Border_Bottom]=GetAttributeLong("BottomLineStyle");
nLineStyle[Border_Left]=GetAttributeLong("LeftLineStyle");
nLineStyle[Border_Right]=GetAttributeLong("RightLineStyle");

ptStart[Border_Top].X=position.left;
ptStart[Border_Top].Y=position.top;
ptEnd[Border_Top].X=position.right;
ptEnd[Border_Top].Y=position.top;
ptStart[Border_Bottom].X=position.left;
ptStart[Border_Bottom].Y=position.bottom;
ptEnd[Border_Bottom].X=position.right;
ptEnd[Border_Bottom].Y=position.bottom;
ptStart[Border_Left].X=position.left;
ptStart[Border_Left].Y=position.top;
ptEnd[Border_Left].X=position.left;
ptEnd[Border_Left].Y=position.bottom;
ptStart[Border_Right].X=position.right;
ptStart[Border_Right].Y=position.top;
ptEnd[Border_Right].X=position.right;
ptEnd[Border_Right].Y=position.bottom;

for(int j=0;j<Border_Max;j++){
switch(nLineStyle[j]){
default:
case 0://noline
pDrawContext->pPenCur=&pDrawContext->penDotted;break;
case 1:
pDrawContext->pPenCur=&pDrawContext->penSingle;break;
case 2:
pDrawContext->pPenCur=&pDrawContext->penDouble;break;
}
pDrawContext->pPenCur->SetWidth((float)(nBorderThickness==0?1:nBorderThickness));
pGraphics->DrawLine(pDrawContext->pPenCur,ptStart[j],ptEnd[j]);
}

}


Command what is yours
Conquer what is not

http://www.csdn.net/develop/author/netauthor/jiangsheng/
蒋晟 2003-10-24
  • 打赏
  • 举报
回复
http://search.csdn.net/expert/topic/50/5001/2002/1/10/469317.htm

Command what is yours
Conquer what is not

http://www.csdn.net/develop/author/netauthor/jiangsheng/
flagfly 2003-10-23
  • 打赏
  • 举报
回复
使用DT_CALCRECT选项,先计算rect大小,再真正draw
要调用两次DrawText
curry913 2003-10-21
  • 打赏
  • 举报
回复
pDC->DrawText("test", rect, DT_CENTER | DT_VCENTER);
第三个参数可以有多个选项
主要要加上dt_wordbreak项
----------------------------------------------------------------------------
但是DrawText的最后一个参数的设置上,DT_VCENTER项只能和DT_SINGLELINE搭配才能生效,并且如果需要自动换行的效果的话,就很麻烦了,请问这该如何解决
csdmy 2003-10-21
  • 打赏
  • 举报
回复
自己定义一个RECT,然后计算出上下左右的宽度,还要分析一下文本的内容,每行多少字符换行,或遇到\n就换行,感觉还是drawtext好
E17 2003-10-21
  • 打赏
  • 举报
回复
drawtext
sujiayi 2003-10-21
  • 打赏
  • 举报
回复
还是用drawtext比较好!
VCNULL 2003-10-21
  • 打赏
  • 举报
回复
我的问题和楼主的问题一样,关注中......
VCNULL 2003-10-21
  • 打赏
  • 举报
回复
纠正一下
"/n"应该是"\n"
VCNULL 2003-10-21
  • 打赏
  • 举报
回复
恩,我试过上面的两种做法,没有办法解决这个问题。
他们说来说去只能让程序在1行的情况下使用,没有办法让程序遇到方框的边界时自动换行。
有时候,不能靠/n来实现字符换行,那该怎么办呢?
DT_VCENTER是单行使用的参数,不适用于多行。

回复人: wwwllg(wwwllg) ( ) 信誉:99 2003-10-20 20:41:00 得分:0


CDC* pDC;
pDC->DrawText("test", rect, DT_CENTER | DT_VCENTER);
第三个参数可以有多个选项
主要要加上dt_wordbreak项

加上DT_WORDBREAK好象也不行,我试过了.我楼组的这个问题还是没有解决.

ionlic 2003-10-20
  • 打赏
  • 举报
回复
TextOut

BOOL TextOut(
HDC hdc, // handle to device context
int nXStart, // x-coordinate of starting position
int nYStart, // y-coordinate of starting position
LPCTSTR lpString, // pointer to string
int cbString // number of characters in string
);

自己计算nXStart nYStart
利用for语句调整。
woaini5994 2003-10-20
  • 打赏
  • 举报
回复
用TextOut在指定位置绘制文本,对齐需要自己计算绘制的位置

感觉下面的做法更专业一些
CDC* pDC;
pDC->DrawText("test", rect, DT_CENTER | DT_VCENTER);
第三个参数可以有多个选项

UDX协议 2003-10-20
  • 打赏
  • 举报
回复
CDC* pDC;
pDC->DrawText("test", rect, DT_CENTER | DT_VCENTER);
第三个参数可以有多个选项
主要要加上dt_wordbreak项
zjg751206 2003-10-20
  • 打赏
  • 举报
回复
换行实现只要加上“\n”就可以了
mylocust 2003-10-20
  • 打赏
  • 举报
回复
上边说的不错! drawtext 函数第三个参数 用来设置居中或居左或其它搭配使用,可以查查MSDN
Paris_Luo 2003-10-20
  • 打赏
  • 举报
回复
DrawText
flagfly 2003-10-20
  • 打赏
  • 举报
回复
CDC* pDC;
pDC->DrawText("test", rect, DT_CENTER | DT_VCENTER);
第三个参数可以有多个选项
加载更多回复(1)

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧