190 likes | 395 Views
C++ 程序设计教程. MFC 简介. MFC 简介. VC++ 分为 API 和 MFC 两部分 Win32 API(Application Programming Interface) 类似于 C 语言的库函数,提供诸如 strcpy() 、 memset() 、 fopen() 等函数。 MFC ( Microsoft Foundation Class ) 微软基本类库,提供窗口( Windows )式程序编程框架。
E N D
C++程序设计教程 MFC简介
MFC简介 • VC++分为API和MFC两部分 • Win32 API(Application Programming Interface)类似于C语言的库函数,提供诸如strcpy()、memset()、fopen()等函数。 • MFC ( Microsoft Foundation Class ) 微软基本类库,提供窗口(Windows)式程序编程框架。 • 通过MFC,我们可以构造基于窗口的应用程序,如对话框(Dialog)、单文档(Single Document)和多文档(Multiply Document)应用软件。
MFC的基本数据类型 • int是特殊的数据类型,它等同于操作系统的位数。如32位系统(Win32)int就是32位。 • 重新定义数据类型是为了可移植性。 • 数据类型:(整型) • BOOL 布尔类型 (int型) • INT, UINT 与int有关类型 • CHAR,(UCHAR)BYTE 与char有关类型8bit • SHORT,(USHORT)WORD 与short有关类型16bit • LONG,(ULONG)DWORD 与long有关类型32bit • LONGLONG,ULONGLONG 64bit类型 • 数据类型:(浮点型) • FLOAT : 类型 float 32bit • DOUBLE : 类型 double 64bit
程序的可读性 • 好的程序体现在下面几个方面: • 正确性、可读性、健壮性、可维护性 • VC++中关于提高可读性的几项基本要求: • 要遵循模块化缩进的原则。 • 工程、类、变量等等的名称一律用英文。 • 变量起名要有意义,让人容易理解其含义。采用英文,不要用汉语拼音。 • 不大容易理解的地方应及时加注释。 • 变量写法遵循匈牙利记法。
变量名的匈牙利记法 Hungarian notation BOOL : bVariable int, INT : nVariable UINT : uVariable char, CHAR : chVariable BYTE : byVariable SHORT : sVariable WORD : wVariable LONG : lVariable DWORD : dwVariable FLOAT : fltVariable DOUBLE : dblVariable CHAR[] : szVariable
关于CSize 类型定义 typedefstruct tagSIZE { LONG cx; LONG cy; } SIZE, *PSIZE, *LPSIZE; 指向该类型的指针 1)typedef的作用是将这个struct定义成一个数据类型。 2)PSIZE和 LPSIZE均表示指向SIZE的指针,在16位系统下有区别,在32位系统下没区别。 3)struct可以认为是成员都是public的类(class)。
重载构造函数 class CSize : public tagSIZE { public: CSize(); CSize(int initCX, int initCY); CSize(SIZE initSize); CSize(POINT initPt); CSize(DWORD dwSize); 。。。 。。。 另外一个数据类型 低16位为cx 高16位为cy
比较 赋值 BOOL operator==(SIZE size) const; BOOL operator!=(SIZE size) const; voidoperator+=(SIZE size); voidoperator-=(SIZE size); CSize operator+(SIZE size) const; CSize operator-(SIZE size) const; CSize operator-() const; CPoint operator+(POINT point) const; CPoint operator-(POINT point) const; CRect operator+(const RECT* lpRect) const; CRect operator-(const RECT* lpRect) const; 运算
CSize SIZE CSize的用法 SIZE sizeBox1={20,40}; SIZE sizeBox2={60,30}; SIZE sizeBox3=sizeBox1+sizeBox2; CSize sizeBox(sizeBox1); CSize sizeBigBox = sizeBox + sizeBox2; 结果拷贝到
与CSize类似的类 : CPoint typedefstruct tagPOINT { LONG x; LONG y; } POINT, *PPOINT, *LPPOINT; class CPoint : public tagPOINT { public: CPoint(); CPoint(int initX, int initY); CPoint(POINT initPt); 。。。 。。。 };
与CSize类似的类 : CRect typedefstruct tagRECT { LONG left; LONG top; LONG right; LONG bottom; } RECT, *PRECT, *LPRECT;
与CSize类似的类 : CRect class CRect : public tagRECT {public: CRect(); CRect(int l, int t, int r, int b); CRect(const RECT& srcRect); CRect(LPCRECT lpSrcRect); CRect(POINT point, SIZE size); CRect(POINT topLeft, POINTbottomRight); 。。。 。。。 typedefconst RECT* LPCRECT; // pointer to read/only RECT
关于LPCTSTR和CString类 • 定义:typedefconst CHAR *LPCTSTR; • CString是管理字符串的类。 class CString {public: CString(); CString(const CString& stringSrc); CString(TCHAR ch, int nRepeat = 1); CString(LPCSTR lpsz); CString(LPCWSTR lpsz); CString(LPCSTR lpch, int nLength); CString(LPCWSTR lpch, int nLength); CString(const unsigned char* psz); 。。。 。。。 LPCTSTR LPCSTR LPCSTR 是单字节 void Func(LPCTSTR lpsz) { CString str(lpsz); int l=str.Length(); } LPCWSTR 是双字节
关于LPCTSTR和CString类 int GetLength() const; BOOL IsEmpty() const; void Empty(); TCHAR GetAt(int nIndex) const; TCHAR operator[](int nIndex) const; void SetAt(int nIndex, TCHAR ch); operator LPCTSTR() const; 取值 CString::operator LPCTSTR() const { return m_pchData; } 类型转换运算 使用实例:CString str; LPCTSTR lpsz = (LPCTSTR) str;
const CString& operator= (const CString& stringSrc); const CString& operator=(TCHAR ch); const CString& operator=(LPCSTR lpsz); const CString& operator=(LPCWSTR lpsz); const CString& operator=(const LPBYTE psz); const CString& operator+=(const CString& string); const CString& operator+=(TCHAR ch); const CString& operator+=(LPCTSTR lpsz); 。。。 。。。
CObject • MFC是由一系列类(Class)构成的窗口程序支撑体系。 CCmdTarget CWnd CDocument CWinApp CFrameWnd CDialog CView
class CObject : • 1) 一般诊断 AssertValid • 2) 运行期识别 RuntimeClass • 3) 串行化 Serialize • 4) 动态创建 DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC • class CCmdTarget : public CObject • 1) 消息发送 • BEGIN_MESSAGE_MAP(CMyView,Cview) • ON_COMMAND(ID_FILE_OPEN,OnFileOpen) • … … • END_MESSAGE_MAP() • 2) 等待光标 • 3) 支持自动化 IDispatch COM
class CWinApp : public CCmdTarget • 1) 获取应用程序的信息。 • 2) 支持注册表 RegistryKey • 3) 支持文档 Document Template • class CWnd : public CCmdTarget • 1) 注册新窗口类。 • 2) 创建和使用窗口 Create, CreateEx; • class CFrameWnd : public CWnd • 标题栏、系统菜单、边框、最小/最大化
class CView : public CWnd • 1) 显示/打印的处理 OnDraw() • 2) 与用户进行交互操作(鼠标/键盘) • OnLButtonDown/OnLButtonUp • OnKeyDown … … • 3) 系统重画 Invalidate(); UpdateWindow(); • class CDocument : public CCmdTarget • 1) 文档的输入/输出。OpenDocument/SaveDocument • 2) 数据的保存和更新; • 3) View的维护。