VC++深入详解第五章——文本编程

1、创建文本插入符
    void CreateSolidCaret();
 ShowCaret();//显示插入符
2、CDC类(绘图类)
 //创建设备描述表
 CClientDC dc(this);
 //定义文本信息结构体变量
 TEXTMETRIC tm;
 //获得设备描述表中的文本信息
 dc.GetTextMetrics(&tm);
 //根据字体大小,创建合适的插入符
 CreateSolidCaret(tm.tmAveCharWidth/8, tm.tmHeight);
 //显示插入符
 ShowCaret();
3、插入图形
private:
 CBitmap bitmap;
 bitmap.LoadBitmap(IDB_BITMAP1);
 CreateCaret(&bitmap);
 ShowCaret();
4、窗口重绘
void CMy5_1View::OnDraw(CDC* pDC)
{
 CMy5_1Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 //CString str("VC++ 深入编程");
 CString str;
 str = "VC++ 深入编程";
 pDC->TextOut(50,50,str);
}
5、添加字符串资源
 str.LoadString(ID_VIEW_STATUS_BAR);
 pDC->TextOut(0,200,str);
6、路径
 CSize sz = pDC->GetTextExtent(str);

 pDC->BeginPath();
 pDC->Rectangle(50,50,50+sz.cx,50+sz.cy);
 pDC->EndPath();
7、画线
 for(int i=0; i<300; i+=10)
 {
  pDC->MoveTo(0,i);
  pDC->LineTo(300,i);
  pDC->MoveTo(i,0);
  pDC->LineTo(i,300);
 }
8、裁剪区域
 pDC->SelectClipPath(RGN_DIFF);
9、字符输入
 WM_CHAR
 WM_LBUTTONDOWN
10、设置字体
 CFont font;
 font.CreatePointFont(300,"华文行楷",NULL);
 CFont *pOldFont = dc.SelectObject(&font);
 dc.SelectObject(pOldFont);
11、定时器
 SetTimer(1,100,NULL);//定时器号、时间、回调函数
12、字体变色
 //矩形宽度以5个像素递增
 m_nWidth += 5;

 CClientDC dc(this);
 TEXTMETRIC tm;
 dc.GetTextMetrics(&tm);
 CRect rect;
 rect.left = 0;
 rect.top = 200;
 rect.right = m_nWidth;
 rect.bottom = rect.top + tm.tmHeight;

 dc.SetTextColor(RGB(255,0,0));
 CString str;
 str.LoadString(ID_VIEW_STATUS_BAR);
 dc.DrawText(str,rect,DT_LEFT);

 rect.top = 150;
 rect.bottom = rect.top + tm.tmHeight;
 dc.DrawText(str,rect,DT_RIGHT);
 
 CSize sz = dc.GetTextExtent(str);
 if(m_nWidth > sz.cx)
 {
  m_nWidth = 0;
  dc.SetTextColor(RGB(0,255,0));
  dc.TextOut(0,200,str);
 }


第5课  文本编程

1,创建插入符:
 void CreateSolidCaret( int nWidth, int nHeight );//创建插入符
 void CreateCaret( CBitmap* pBitmap );//创建位图插入符
 void ShowCaret( );//显示插入符
 void HideCaret( );//隐藏插入符
 static void PASCAL SetCaretPos( POINT point );//移动插入符号
 说明:
  1)创建插入符要在窗口创建完成之后,CreateSolidCaret函数创建的插入符被初始化为隐藏,所以需要调用ShowCaret()将其显示。
  2)使用CreateCaret函数创建位图插入符的时候,不能使用局部的位图对象关联位图资源。(与资源相关联的C++对象,当它析构的时候会同时把与它相关联的资源销毁。)
2,获取当前字体信息的度量:CDC::GetTextMetrics
 BOOL GetTextMetrics( LPTEXTMETRIC lpMetrics ) const;
说明:
typedef struct tagTEXTMETRIC {  /* tm */
    int  tmHeight;//字体高度。Specifies the height (ascent + descent) of characters.
    int  tmAscent;//基线以上的字体高度
    int  tmDescent;//基线以下的字体高度
    int  tmInternalLeading;
    int  tmExternalLeading;
    int  tmAveCharWidth;//字符平均宽度
    int  tmMaxCharWidth;
    int  tmWeight;
    BYTE tmItalic;
    BYTE tmUnderlined;
    BYTE tmStruckOut;
    BYTE tmFirstChar;
    BYTE tmLastChar;
    BYTE tmDefaultChar;
    BYTE tmBreakChar;
    BYTE tmPitchAndFamily;
    BYTE tmCharSet;
    int  tmOverhang;
    int  tmDigitizedAspectX;
    int  tmDigitizedAspectY;
} TEXTMETRIC;
3,OnDraw函数:
 virtual void OnDraw( CDC* pDC )
 当窗口(从无到有或尺寸大小改变等)要求重绘的时候,会发送WM_PAIN消息,调用OnDraw函数进行重绘。
4,获取字符串的高度和宽度(区别字符串的长度):
 CDC::GetTextExtent
 CSize GetTextExtent( LPCTSTR lpszString, int nCount ) const;
 CSize GetTextExtent( const CString& str ) const;
说明:
The CSize class is similar to the Windows SIZE structure。
typedef struct tagSIZE {
    int cx;//the x-extent
    int cy;//the y-extent
} SIZE;
 
5,路径层:
 BOOL BeginPath( );
 //在这作图定义路径层剪切区域
 BOOL EndPath( );
 BOOL SelectClipPath( int nMode );//调用这个函数来使当前路径层剪切区域与新剪切区域进行互操作。  
 //在这覆盖作图(包含前定义的路径层区域)定义新的剪切区域
 说明:
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.

nMode:RGN_AND,RGN_COPY,RGN_DIFF,RGN_OR,RGN_XOR
RGN_AND   The new clipping region includes the intersection (overlapping areas) of the current clipping region and the current path.
RGN_COPY   The new clipping region is the current path.
RGN_DIFF   The new clipping region includes the areas of the current clipping region, and those of the current path are excluded.
RGN_OR   The new clipping region includes the union (combined areas) of the current clipping region and the current path.
RGN_XOR   The new clipping region includes the union of the current clipping region and the current path, but without the overlapping areas.
2)应用:当作图的时候,如果想要在整幅图形其中的某个部分和其它部分有所区别,我们可以把这部分图形放到路径层当中,然后指定调用指定互操作模式调用SelectClipPath( int nMode )函数来使路径层和覆盖在其上新绘图剪切区域进行互操作,达到特殊效果。

6,关于文本字符串一些函数:
 COLORREF GetBkColor( ) const;//得到背景颜色
 virtual COLORREF SetBkColor( COLORREF crColor );//设置背景颜色
 BOOL SetTextBkColor( COLORREF cr );//设置文本背景颜色
 virtual COLORREF SetTextColor( COLORREF crColor );//设置文本颜色
 virtual BOOL TextOut( int x, int y, LPCTSTR lpszString, int nCount );//输出文本
 BOOL TextOut( int x, int y, const CString& str );
 CString Left( int nCount ) const;//得到字符串左边nCount个字符
 int GetLength( ) const;//得到字符串长度
 
7,字体CFont::CFont
 CFont( );//构造函数
 //Constructs a CFont object. The resulting object must be initialized with CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndirect before it can be used.
 选用字体事例代码组:
 CClientDC dc(this);
 CFont font;//构造字体对象
 font.CreatePointFont(300,"华文行楷",NULL);//初始化字体对象,与字体资源相关联
 CFont *pOldFont=dc.SelectObject(&font);//将新字体选入DC
 ...
 dc.SelectObject(pOldFont);//恢复原字体
说明:
 1)构造字体对象时候,必须初始化。(初始化是将字体对象与字体资源相关联)。
 2)初始化对象时候,选用的字体也可以是系统字体,但不一定都有效,据测试选用。

8,在MFC中CEditView 和 cRichEditView类已经完成了初步的文字处理。可以让应用程序的View类以CEditView 和 cRichEditView类为基类。

9,平滑变色
 CDC::TextOut()是一个字母一个字母的输出,达不到平滑效果。
 CDC::DrawText():将文字的输出局限于一个矩形区域,超出矩形区域的文字都被截断。利用这一特点,可每隔些时间增加矩形大小,从而可实现人眼中的平滑效果。
 CWnd::SetTimer():设置定时器。按设定的时间定时发送WM_TIMER消息。
说明:
 UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
 //nIDEvent定时器标示,nElapse消息发送间隔时间,void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD)设置回调函数,如果设置则由设置的回调函数处理WM_TIMER消息,如果没有设置回调函数设为NULL,这发送的WM_TIMER消息压入消息队列,交由相关联的窗口处理(添加WM_TIMER消息处理函数OnTimer())。
 afx_msg void OnTimer( UINT nIDEvent );
 //响应WM_TIMER消息,nIDEvent为消息对应定时器标示(可以设置不同的定时器发送WM_TIMER消息)
 
问题:
 1,在CCareView类中添加WM_CREATE消息响应函数OnCreate(),WM_CREATE消息是在什么时候被检查到而被响应的呢?
 (猜测:添加WM_CREATE消息后,消息被压入消息队列,然后经过消息循环进行分发到具体窗口,从而进行响应)
 2,现有一文本文件内容已经读入串STR中,要求在视图客户区按原先文本文件中的格式输出。
 问题是,利用CDC的TextOut()来在CView类派生类窗口中输出串时,忽略了串中的TAB、回车换行等格式,无论串有多长均在一行上输出。
 这其中是CDC类成员函数TextOut()忽略串中格式的,还是CView类派生类窗口设置从中做怪呢?怎么解决

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值