📄 csdn技术中心 孙鑫vc++讲座笔记-(5)文本编程.htm
字号:
<TD></TD></TR>
<TR>
<TD noWrap align=middle bgColor=#003399 height=16><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> <SPAN
id=ArticleTitle1_ArticleTitle1_lblTitle>孙鑫VC++讲座笔记-(5)文本编程</SPAN></B>
选择自 <A id=ArticleTitle1_ArticleTitle1_AuthorLink
href="http://dev.csdn.net/user/hbyufan">hbyufan</A> 的 Blog
</TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=16><FONT
color=#ffffff>关键字</FONT></TD>
<TD width=500> <SPAN
id=ArticleTitle1_ArticleTitle1_lblKeywords>孙鑫VC++讲座笔记-(5)文本编程</SPAN></TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=16><FONT
color=#ffffff>出处</FONT></TD>
<TD> <SPAN
id=ArticleTitle1_ArticleTitle1_lblSource></SPAN></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width=10></TD>
<TD><SPAN id=ArticleContent1_ArticleContent1_lblContent>
<P>1,创建插入符:<BR>void CreateSolidCaret( int nWidth, int nHeight
);//创建插入符<BR>void CreateCaret( CBitmap* pBitmap );//创建位图插入符<BR>void
ShowCaret( );//显示插入符<BR>void HideCaret( );//隐藏插入符<BR>static void
PASCAL SetCaretPos( POINT point
);//移动插入符号<BR>说明:<BR>1)创建插入符要在窗口创建完成之后,CreateSolidCaret函数创建的插入符被初始化为隐藏,所以需要调用ShowCaret()将其显示。<BR>2)使用CreateCaret函数创建位图插入符的时候,不能使用局部的位图对象关联位图资源。(与资源相关联的C++对象,当它析构的时候会同时把与它相关联的资源销毁。)</P>
<P>2,获取当前字体信息的度量:CDC::GetTextMetrics<BR>BOOL GetTextMetrics(
LPTEXTMETRIC lpMetrics ) const;<BR>说明:<BR>typedef struct
tagTEXTMETRIC { /* tm */<BR> int
tmHeight;//字体高度。Specifies the height (ascent + descent) of
characters.<BR> int
tmAscent;//基线以上的字体高度<BR> int
tmDescent;//基线以下的字体高度<BR> int
tmInternalLeading;<BR> int
tmExternalLeading;<BR> int
tmAveCharWidth;//字符平均宽度<BR> int
tmMaxCharWidth;<BR> int
tmWeight;<BR> BYTE tmItalic;<BR>
BYTE tmUnderlined;<BR> BYTE
tmStruckOut;<BR> BYTE
tmFirstChar;<BR> BYTE
tmLastChar;<BR> BYTE
tmDefaultChar;<BR> BYTE
tmBreakChar;<BR> BYTE
tmPitchAndFamily;<BR> BYTE
tmCharSet;<BR> int
tmOverhang;<BR> int
tmDigitizedAspectX;<BR> int
tmDigitizedAspectY;<BR>} TEXTMETRIC;</P>
<P>3,OnDraw函数:<BR>virtual void OnDraw( CDC* pDC
)<BR>当窗口(从无到有或尺寸大小改变等)要求重绘的时候,会发送WM_PAIN消息,调用OnDraw函数进行重绘。</P>
<P>4,获取字符串的高度和宽度(区别字符串的长度):<BR>CDC::GetTextExtent<BR>CSize
GetTextExtent( LPCTSTR lpszString, int nCount ) const;<BR>CSize
GetTextExtent( const CString& str ) const;<BR>说明:<BR>The CSize
class is similar to the Windows SIZE structure。<BR>typedef struct
tagSIZE {<BR> int cx;//the x-extent
<BR> int cy;//the y-extent <BR>} SIZE;</P>
<P><BR>5,路径层:<BR>BOOL BeginPath( );<BR>//在这作图定义路径层剪切区域<BR>BOOL
EndPath( );<BR>BOOL SelectClipPath( int nMode
);//调用这个函数来使当前路径层剪切区域与新剪切区域进行互操作。
<BR>//在这覆盖作图(包含前定义的路径层区域)定义新的剪切区域</P>
<P>说明:<BR>1)SelectClipPath Selects the current path as a clipping
region for the device context, combining the new region with any
existing clipping region by using the specified mode. The device
context identified must contain a closed
path.<BR>////<BR>nMode:RGN_AND,RGN_COPY,RGN_DIFF,RGN_OR,RGN_XOR<BR>RGN_AND
The new clipping region includes the intersection (overlapping
areas) of the current clipping region and the current
path.<BR>RGN_COPY The new clipping region is the current
path.<BR>RGN_DIFF The new clipping region includes the
areas of the current clipping region, and those of the current path
are excluded.<BR>RGN_OR The new clipping region includes
the union (combined areas) of the current clipping region and the
current path.<BR>RGN_XOR The new clipping region
includes the union of the current clipping region and the current
path, but without the overlapping
areas.<BR>2)应用:当作图的时候,如果想要在整幅图形其中的某个部分和其它部分有所区别,我们可以把这部分图形放到路径层当中,然后指定调用指定互操作模式调用SelectClipPath(
int nMode )函数来使路径层和覆盖在其上新绘图剪切区域进行互操作,达到特殊效果。</P>
<P>6,关于文本字符串一些函数:<BR>COLORREF GetBkColor( )
const;//得到背景颜色<BR>virtual COLORREF SetBkColor( COLORREF crColor
);//设置背景颜色<BR>BOOL SetTextBkColor( COLORREF cr
);//设置文本背景颜色<BR>virtual COLORREF SetTextColor( COLORREF crColor
);//设置文本颜色<BR>virtual BOOL TextOut( int x, int y, LPCTSTR
lpszString, int nCount );//输出文本<BR>BOOL TextOut( int x, int y, const
CString& str );<BR>CString Left( int nCount )
const;//得到字符串左边nCount个字符<BR>int GetLength( ) const;//得到字符串长度</P>
<P>7,字体CFont::CFont <BR>CFont( );//构造函数<BR>//Constructs a CFont
object. The resulting object must be initialized with CreateFont,
CreateFontIndirect, CreatePointFont, or CreatePointFontIndirect
before it can be used.<BR>选用字体事例代码组:<BR>CClientDC dc(this);<BR>CFont
font;//构造字体对象<BR>font.CreatePointFont(300,"华文行楷",NULL);//初始化字体对象,与字体资源相关联<BR>CFont
*pOldFont=dc.SelectObject(&font);//将新字体选入DC<BR>...<BR>dc.SelectObject(pOldFont);//恢复原字体<BR>说明:<BR>1)构造字体对象时候,必须初始化。(初始化是将字体对象与字体资源相关联)。<BR>2)初始化对象时候,选用的字体也可以是系统字体,但不一定都有效,据测试选用。</P>
<P>8,在MFC中CEditView 和
cRichEditView类已经完成了初步的文字处理。可以让应用程序的View类以CEditView 和
cRichEditView类为基类。</P>
<P>9,平滑变色<BR>CDC::TextOut()是一个字母一个字母的输出,达不到平滑效果。<BR>CDC::DrawText():将文字的输出局限于一个矩形区域,超出矩形区域的文字都被截断。利用这一特点,可每隔些时间增加矩形大小,从而可实现人眼中的平滑效果。<BR>CWnd::SetTimer():设置定时器。按设定的时间定时发送WM_TIMER消息。</P>
<P>说明:<BR>UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK
EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD)
);<BR>//nIDEvent定时器标示,nElapse消息发送间隔时间,void (CALLBACK EXPORT*
lpfnTimer)(HWND, UINT, UINT,
DWORD)设置回调函数,如果设置则由设置的回调函数处理WM_TIMER消息,如果没有设置回调函数设为NULL,这发送的WM_TIMER消息压入消息队列,交由相关联的窗口处理(添加WM_TIMER消息处理函数OnTimer())。</P>
<P>afx_msg void OnTimer( UINT nIDEvent
);<BR>//响应WM_TIMER消息,nIDEvent为消息对应定时器标示(可以设置不同的定时器发送WM_TIMER消息)</P>
<P></P>
<P><BR>问题:<BR>1,在CCareView类中添加WM_CREATE消息响应函数OnCreate(),WM_CREATE消息是在什么时候被检查到而被响应的呢?<BR>(猜测:添加WM_CREATE消息后,消息被压入消息队列,然后经过消息循环进行分发到具体窗口,从而进行响应)</P>
<P>2,现有一文本文件内容已经读入串STR中,要求在视图客户区按原先文本文件中的格式输出。<BR>问题是,利用CDC的TextOut()来在CView类派生类窗口中输出串时,忽略了串中的TAB、回车换行等格式,无论串有多长均在一行上输出。<BR>这其中是CDC类成员函数TextOut()忽略串中格式的,还是CView类派生类窗口设置从中做怪呢?怎么解决?</P></SPAN><BR>
<DIV
style="FONT-SIZE: 14px; LINE-HEIGHT: 25px"><STRONG>作者Blog:</STRONG><A
id=ArticleContent1_ArticleContent1_AuthorBlogLink
href="http://blog.csdn.net/hbyufan/"
target=_blank>http://blog.csdn.net/hbyufan/</A></DIV>
<DIV
style="FONT-SIZE: 14px; COLOR: #900; LINE-HEIGHT: 25px"><STRONG>相关文章</STRONG></DIV>
<TABLE id=ArticleContent1_ArticleContent1_RelatedArticles
cellSpacing=0 border=0>
<TBODY>
<TR>
<TD><A
href="http://dev.csdn.net/article/79/79688.shtm">MASM-文件记录操作</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/79/79687.shtm">MASM-打字练习程序</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/79/79685.shtm">MASM-两个16位二进制数减法模拟</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/78/78189.shtm">VC++技术内幕(第四版)笔记(第8章)</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/78/78188.shtm">VC++技术内幕(第四版)笔记(第7章)</A>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><A name=#Comment></A>
<TABLE cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center
bgColor=#006699 border=0>
<TBODY>
<TR bgColor=#006699>
<TD id=white align=middle width=556 bgColor=#006699><FONT
color=#ffffff>对该文的评论</FONT> </TD></TR></TBODY></TABLE>
<DIV align=right><A id=CommnetList1_CommnetList1_Morelink
href="http://comment.csdn.net/Comment.aspx?c=2&s=78181">【评论】</A>
<A id=CommnetList1_CommnetList1_Hyperlink1
href="javascript:window.close();">【关闭】</A> <A
href="mailto:webmaster@csdn.net">【报告bug】</A>
</DIV><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM><!-- 版权 -->
<DIV align=center>
<SCRIPT language=JavaScript src="CSDN技术中心 孙鑫VC++讲座笔记-(5)文本编程.files/footer_gb.js"
type=text/javascript></SCRIPT>
</DIV><!-- /版权 -->
<SCRIPT>
document.write("<img src=http://count.csdn.net/count/pageview1.asp?columnid=4&itemid=11 border=0 width=0 height=0>");
</SCRIPT>
<SCRIPT>document.write("<img src=http://counter.csdn.net/pv.aspx?id=37 border=0 width=0 height=0>");</SCRIPT>
<SCRIPT language=JavaScript
src="CSDN技术中心 孙鑫VC++讲座笔记-(5)文本编程.files/common.htm"></SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -