MSG typedef struct tag MSG HWND hwnd UINT





































































- Slides: 69
MSG typedef struct tag. MSG { HWND hwnd; UINT message; WPARAM w. Param; LPARAM l. Param; DWORD time; POINT pt; }MSG;
窗口应用程序组成 ¨ The Win. Main function ¨ A window procedure function ¨ message loop (消息循环) 例3-1
消息循环 while (Get. Message(&msg, NULL, 0, 0)) { if (!Translate. Accelerator(msg. hwnd, h. Accel. Table, &msg)) { Translate. Message(&msg); Dispatch. Message(&msg); } }
MFC 类 ¨ application programming interface (API) ¨ MFC 封装了API的主要结构和通用组件
MFC 的继承关系 ¨ MFC 类封装了 – the win 32 API – 应用程序对象如: documents, views and applictions – Activex, database, Internet 特点 ¨ MFC分类 – CObject_derived 类 – Non_CObject_derived 类
CObject派生类 CObject CCmd. Target CWin. Thread CWin. App CWnd CFrame. Wnd CView CDocument
非CObject派生类 ¨ 通用 具类 – CString – CTime. Span – CRect – Cpoint – CSize ¨ MFC支持类 – CArchive – CDump. Context – CRuntime. Class – CFile. Status – CMemory. State ¨ CType. Pointer. Array
主框架类CMain. Frame ¨ 管理除视图之外的其他主窗口部分 ¨ On. Create ¨ m_wnd. Tool. Bar ¨ m_wnd. Status. Bar
文档/视图结构 ¨ 文档 : a data object with which the user interacts in an editing session ¨ 视图 a window object through which the user interacts with the document
文档/视图应用程序的构成 类名称 基类 说明 CExam 1_1 App CWin. App 应用程序类 CMain. Frame CFrame. Wnd 窗口框架类 CExam 1_1 Doc CDocument 文档类 CExam 1_1 View CView 视图类
视图类的类型 ¨ CScroll. View ¨ CForm. View ¨ CEdit. View ¨ CRich. Edit. View ¨ CList. View ¨ CTree. View ¨ CRecord. View ¨ CCtrl. View
文档模板类层次图 CObject CCmd. Target CDoc. Template CMulti. Doc. Template CSingle. Doc. Template
定义单文档的文档模板类型 CSingle. Doc. Template* p. Doc. Template; p. Doc. Template = new CSingle. Doc. Template( IDR_MAINFRAME, RUNTIME_CLASS(CExam 1_1 Doc), RUNTIME_CLASS(CMain. Frame), RUNTIME_CLASS(CExam 1_1 View)); Add. Doc. Template(p. Doc. Template);
SDI 应用程序组成 Application Object Main Frame Window Document Template Toolbar Document View Status Bar
窗口消息的描述 ¨ message:WM_XXX ¨ w. Param和 l. Param:随WM_XXX而变
命令消息的描述 ¨ message :WM_COMMAND ¨ w. Param:低16位为命令ID、高 16位为 0 ¨ l. Param: 0 L
消息映射系统 ¨ CCmd. Target的派生类 –每个子类都定义一个消息表 ¨ 消息映射表 –DECLARE_MESSAGE_MAP() –BEGIN_MESSAGE_MAP() –END_MESSAGE_MAP()
在类中加入静态消息映射表 ¨ 在. h 文件中加入: DECLARE_MESSAGE_MAP() ¨ 在. CPP文件中加入: BEGIN_MESSAGE_MAP(CMain. Frame, CFrame Wnd) ON_WM_PAINT(). . . END_MESSAGE_MAp()
消息映射表示例 BEGIN_MESSAGE_MAP(CExam 1_1 App, CWin. App) //{{AFX_MSG_MAP(CExam 1_1 App) ON_COMMAND(ID_APP_ABOUT, On. App. About) //}}AFX_MSG_MAP ON_COMMAND(ID_FILE_NEW, CWin. App: : On. File. New) ON_COMMAND(ID_FILE_OPEN, CWin. App: : On. File. Open) ON_COMMAND(ID_FILE_PRINT_SETUP, CWin. App: : On. File. Print. Setup) END_MESSAGE_MAP()
自定义静态窗口消息 ¨ 首先在为类实现文件中定义消息 – #define WM_MYMESSAGE WM_USER+100 ¨ 在类中声明消息处理函数 – Afx_msg LRESULT On. My. Message(WPARAM w. Param, LPARAM l. Param) ¨ 在类的消息映射表中加入映射项 – ON_MESSAGE(WM_MYMESSAGE, On. My. Message) ¨ 在类上实现消息处理函数 ¨ 使用Send. Message函数或Post. Message发送消息 – p. View->Send. Message(WM_MYMESSAGE, 0 L);