用SDK的Win32 API编写各种Windows应用程序,有其共同的规律:首先是编写WinMain函数,编写处理消息和事件的窗口过程WndProc,在WinMain里头注册窗口(Register Window),创建窗口,然后开始应用程序的消息循环。
MFC应用程序也不例外,因为MFC是一个建立在SDK API基础上的编程框架。对程序员来说所不同的是:一般情况下,MFC框架自动完成了Windows登记、创建等工作。
CObject是MFC的根类,绝大多数MFC类是其派生的,包括CCmdTarget。CObject 实现了一些重要的特性,包括动态类信息、动态创建、对象序列化、对程序调试的支持,等等。所有从CObject派生的类都将具备或者可以具备CObject所拥有的特性。CCmdTarget通过封装一些属性和方法,提供了消息处理的架构。MFC中,任何可以处理消息的类都从CCmdTarget派生。
CObject -> CCmdTarget -> CWmd/CWimApp/CDocTemplate/CDocument/CView/CFrameWnd.
CWmd派生CEdit
CEdit类的结构很复杂,定义如下::
class CEdit : public CWnd { // DECLARE_DYNAMIC virtual OK - CWnd already has DECLARE_DYNAMIC DECLARE_DYNAMIC(CEdit) // Constructors定义构造函数 public: CEdit(); BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID); // Attributes定义成员函数 BOOL CanUndo() const; int GetLineCount() const; BOOL GetModify() const; void SetModify(BOOL bModified = TRUE); void GetRect(LPRECT lpRect) const; DWORD GetSel() const; void GetSel(int& nStartChar, int& nEndChar) const; HLOCAL GetHandle() const; void SetHandle(HLOCAL hBuffer); void SetMargins(UINT nLeft, UINT nRight); DWORD GetMargins() const; void SetLimitText(UINT nMax); UINT GetLimitText() const; CPoint PosFromChar(UINT nChar) const; int CharFromPos(CPoint pt) const; // NOTE: first word in lpszBuffer must contain the size of the buffer! // NOTE: may not return null character int GetLine(_In_ int nIndex, _Out_ LPTSTR lpszBuffer) const; // NOTE: may not return null character int GetLine(_In_ int nIndex, _Out_writes_to_(nMaxLength, return) LPTSTR lpszBuffer, _In_ int nMaxLength) const; AFX_ANSI_DEPRECATED BOOL SetCueBanner(_In_z_ LPCWSTR lpszText, _In_ BOOL fDrawIfFocused = FALSE); AFX_ANSI_DEPRECATED BOOL GetCueBanner(_Out_writes_z_(cchText) LPWSTR lpszText, _In_ int cchText) const; #if defined(UNICODE) CString GetCueBanner() const; BOOL ShowBalloonTip(_In_z_ LPCWSTR lpszTitle, _In_z_ LPCWSTR lpszText, _In_ INT ttiIcon = TTI_NONE); BOOL ShowBalloonTip(_In_ PEDITBALLOONTIP pEditBalloonTip); BOOL HideBalloonTip(); #endif // (UNICODE) #if defined(UNICODE) // REVIEW: Sets the characters in the edit control that are highlighted. void SetHighlight(_In_ int ichStart, _In_ int ichEnd); // REVIEW: Retrieves the characters in the edit control that are highlighted. BOOL GetHighlight(_Out_ int* pichStart, _Out_ int* pichEnd) const; #endif // defined(UNICODE) // Operations void EmptyUndoBuffer(); BOOL FmtLines(BOOL bAddEOL); void LimitText(int nChars = 0); int LineFromChar(int nIndex = -1) const; int LineIndex(int nLine = -1) const; int LineLength(int nLine = -1) const; void LineScroll(int nLines, int nChars = 0); void ReplaceSel(LPCTSTR lpszNewText, BOOL bCanUndo = FALSE); void SetPasswordChar(TCHAR ch); void SetRect(LPCRECT lpRect); void SetRectNP(LPCRECT lpRect); void SetSel(DWORD dwSelection, BOOL bNoScroll = FALSE); void SetSel(int nStartChar, int nEndChar, BOOL bNoScroll = FALSE); BOOL SetTabStops(int nTabStops, LPINT rgTabStops); void SetTabStops(); BOOL SetTabStops(const int& cxEachStop); // takes an 'int' // Clipboard operations BOOL Undo(); void Clear(); void Copy(); void Cut(); void Paste(); BOOL SetReadOnly(BOOL bReadOnly = TRUE); int GetFirstVisibleLine() const; TCHAR GetPasswordChar() const; // Implementation public: // virtual OK here - ~CWnd already virtual virtual ~CEdit(); //虚拟析构函数 };
注明:WORD :16位无符号数,LONG :32位有符号数
DWORD :32位无符号数 LPTSTR :指向字符串的32位指针