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的基本数据类型
1,int 是特殊的数据类型,它等同于操作系统的
位数。如 32位系统( Win32) int就是 32位。
2,重新定义数据类型是为了可移植性。
3,数据类型:(整型)
1,BOOL 布尔类型 ( int型)
2,INT,UINT 与 int有关类型
3,CHAR,(UCHAR)BYTE 与 char有关类型 8bit
4,SHORT,(USHORT)WORD 与 short有关类型 16bit
5,LONG,(ULONG)DWORD 与 long有关类型 32bit
6,LONGLONG,ULONGLONG 64bit类型
4,数据类型:(浮点型)
1,FLOAT, 类型 float 32bit
2,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
typedef struct 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;
void operator+=(SIZE size);
void operator-=(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 sizeBox1={20,40};
SIZE sizeBox2={60,30};
SIZE sizeBox3=sizeBox1+sizeBox2;
?
CSize SIZE
结果拷贝到 CSize sizeBox(sizeBox1);
CSize sizeBigBox
= sizeBox + sizeBox2;
与 CSize类似的类, CPoint
typedef struct tagPOINT
{ LONG x;
LONG y;
} POINT,*PPOINT,*LPPOINT;
class CPoint, public tagPOINT
{ public,
CPoint();
CPoint(int initX,int initY);
CPoint(POINT initPt);
。。。 。。。
};
与 CSize类似的类, CRect
typedef struct 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,
POINT bottomRight);
。。。 。。。
typedef const RECT* LPCRECT;
// pointer to read/only RECT
关于 LPCTSTR和 CString类
? 定义,typedef const 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
是单字节
LPCWSTR
是双字节
void Func(LPCTSTR lpsz)
{ CString str(lpsz);
int l=str.Length();
}
关于 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);
。。。 。。。
? MFC是由一系列类 (Class)构成的窗口
程序支撑体系。
CObject
CCmdTarget
CWnd
CFrameWnd CDialog CView
CDocument CWinApp
? 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的维护。
?