孫鑫VC++講座筆記-(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類派生類窗口設置從中做怪呢?怎麼解決? 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章