注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

小人物的自述

为梦想而奋斗,没有梦想就没有希望!

 
 
 

日志

 
 

DrawText显示多行文本的思路  

2009-06-16 10:37:53|  分类: 学习笔记 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

利用ApplicationWizard生成了一个SDI界面的程序代码。在CView派生类的OnDraw成员函数中加入绘图代码。在这里我需要解释一下OnDraw函数的作用,OnDraw函数会在窗口需要重绘时自动被调用,传入的参数CDC* pDC对应的就是DC环境。使用OnDraw的优点就在于在你使用打印功能的时候传入OnDraw的DC环境将会是打印机绘图环境,使用打印预览时传入的 是一个称为CPreviewDC的绘图环境,所以你只需要一份代码就可以完成窗口/打印预览/打印机绘图三重功能。利用Windows的设备无关性和M$ 为打印预览所编写的上千行代码你可以很容易的完成一个具有所见即所得的软件。

输出文字一般使用CDC::BOOL TextOut( int x, int y, const CString& str )和CDC::int DrawText( const CString& str, LPRECT lpRect, UINT nFormat )两个函数,对TextOut来讲只能输出单行的文字,而DrawText可以指定在一个矩形中输出单行或多行文字,并且可以规定对齐方式和使用何种风 格。nFormat可以是多种以下标记的组合(利用位或操作)以达到选择输出风格的目的。
DT_BOTTOM底部对齐   Specifies bottom-justified text. This value must be combined with DT_SINGLELINE.
DT_CALCRECT计算指定文字时所需要矩形尺寸   Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.
DT_CENTER中部对齐   Centers text horizontally.
DT_END_ELLIPSIS or DT_PATH_ELLIPSIS   省略模式,即当单行显示且显示不下时,DrawText可以自动为你显示省略号“...”。DT_END_ELLIPSIS表示省略后面的文字,效果如下:"abcdef...", DT_PATH_ELLIPSIS针对文字路径省略,也就是它会尽量保证最后一个"\"后的内容。如“c:\abc\...\345.txt”。需要注意的是,打开省略模式后,DrawText的第一个参数指向字符串的内存需要多分配三个字节。You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.
DT_EXPANDTABS   Expands tab characters. The default number of characters per tab is eight.
DT_EXTERNALLEADING   Includes the font抯 external leading in the line height. Normally, external leading is not included in the height of a line of text.
DT_LEFT左对齐   Aligns text flush-left.
DT_MODIFYSTRING   Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.
Note Some uFormat flag combinations can cause the passed string to be modified. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the string to be modified, causing an assertion in the CString override.
DT_NOCLIP   Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.
DT_NOPREFIX禁止使用&前缀   Turns off processing of prefix characters. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off.
DT_PATH_ELLIPSIS  
DT_RIGHT右对齐   Aligns text flush-right.
DT_SINGLELINE单行输出   Specifies single line only. Carriage returns and linefeeds do not break the line.
DT_TABSTOP设置TAB字符所占宽度   Sets tab stops. The high-order byte of nFormat is the number of characters for each tab. The default number of characters per tab is eight.
DT_TOP定部对齐   Specifies top-justified text (single line only).
DT_VCENTER中部对齐   Specifies vertically centered text (single line only).
DT_WORDBREAK每行只在单词间被折行   Specifies word-breaking. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by lpRect. A carriage return杔inefeed sequence will also break the line.

举例:
CCetDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 if(m_IsTestOver)    //测试完成标志
 {
  CRect rc;
  GetWindowRect(rc);    //得到窗口RECT
//假如超出指定rect边界就按单词打断,行位加回车;AnswerTxt是CString字符串
  pDC->DrawText(m_AnswerTxt,&rc,DT_WORDBREAK); 
//等价于:pDC->DrawText(AnswerTxt,-1,&rc,DT_WORDBREAK);   
 }

  评论这张
 
阅读(2900)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017