-
Notifications
You must be signed in to change notification settings - Fork 20
/
day02.txt
571 lines (259 loc) · 8.67 KB
/
day02.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
消息宏的实现:
======================DECLARE_MESSAGE_MAP===========================================
private:
////用于保存消息ID 和 消息处理函数指针 的对应信息
static const AFX_MSGMAP_ENTRY _messageEntries[];
protected:
//保存了_messageEntries数组地址 和 _GetBaseMessageMap函数地址
static AFX_DATA const AFX_MSGMAP messageMap;
//获取父类的messageMap地址
static const AFX_MSGMAP* PASCAL _GetBaseMessageMap();
//获取自己的messageMap的地址
virtual const AFX_MSGMAP* GetMessageMap() const;
=====================================================================================
数据结构
1. AFX_MSGMAP_ENTRY用于保存消息ID 和 消息处理函数指针 的对应信息
struct AFX_MSGMAP_ENTRY
{
UINT nMessage; // windows message
UINT nCode; // control code or WM_NOTIFY code
UINT nID; // control ID (or 0 for windows messages)
UINT nLastID; // used for entries specifying a range of control id's
UINT nSig; // signature type (action) or pointer to message #
AFX_PMSG pfn; // routine to call (or special value)
};
2. AFX_MSGMAP用于保存GetBaseMap函数的地址 和 AFX_MSGMAP_ENTRY 的地址.
struct AFX_MSGMAP
{
const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)(); //函数指针
const AFX_MSGMAP_ENTRY* lpEntries; //数组指针
};
3.消息映射过程
3.1消息处理函数WindowProc收到消息后, 调用OnWndMsg,
如果OnWndMsg不处理消息,那么WindowProc将
调用DefWindowProc默认处理并返回
3.2 OnWndMsg处理消息过程:
3.2.1 使用GetMessageMap函数获取该窗口类的messageMap的地址
const AFX_MSGMAP* pMessageMap; pMessageMap = GetMessageMap();
3.2.2 在messageMap中的lpEntries数组中查找消息ID对应的数组元素
3.2.3 如果未找到, 则获取父类的messageMap的地址,返回3.2.2
3.2.4 如果找到, 则获取元素(处理函数地址和父类的messageMap地址)lpEntry
3.2.5 根据找到的 lpEntry的nSig表示, 调用消息处理函数
==================== 消息映射过程 伪代码=======================================================
const AFX_MSGMAP* pMessageMap; pMessageMap = GetMessageMap();
for (; pMessageMap != NULL;pMessageMap = (*pMessageMap->pfnGetBaseMap)()) //往上(父类)查找
{
if ((lpEntry = AfxFindMessageEntry(pMessageMap->lpEntries,message, 0, 0)) != NULL)
{
pMsgCache->lpEntry = lpEntry;
AfxUnlockGlobals(CRIT_WINMSGCACHE);
goto LDispatch;
}
int nSig = lpEntry->nSig;
switch (nSig) //根据处理函数类型, 分支
{
case AfxSig_lwl:
lResult = (this->*mmf.pfn_lwl)(wParam, lParam); //调用处理函数
break;
}
}
=================================================================================================
二 MFC的消息分类
1.窗口消息, 如: WM_CREATE WM_PAINT, 鼠标, 键盘等消息
这些消息的处理方式是直接调用消息处理函数
ON_MESSAGE()
ON_WM_XXXX( )
2.命令消息 WM_COMMAND
菜单, 工具栏, 按钮 等点击时的命令
过程: 消息首先发送给主窗口,由主窗口逐层向子窗口派发.
ON_COMMAND()
ON_COMMAND_RANGE()
3.通知消息 WM_NOTIFY
子窗口给父窗口的通知消息
控件消息宏,
例如: EN_CHANGE(编辑框内容发生改变)
4.自注册消息
用户自定义消息处理
//用户注册消息, 然后在消息映射中使用
UINT RegisterWindowMessage(LPCTSTR lpString);
返回注册成功的消息ID (0xC000-0xFFFF)
消息映射宏 ON_REGISTERED_MESSAGE
消息处理与窗口消息处理类似, 但是在查找消息处理函数和执行
消息处理函数时不同
// for Registered Windows messages
#define ON_REGISTERED_MESSAGE(nMessageVariable, memberFxn) \
{ 0xC000, 0, 0, 0, (UINT)(UINT*)(&nMessageVariable), \
/*implied 'AfxSig_lwl'*/ \
(AFX_PMSG)(AFX_PMSGW)(LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))&memberFxn },
===========================================================
// MFCMsg.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
class CMsgFrame : public CFrameWnd
{
public:
virtual LRESULT WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
public:
DECLARE_MESSAGE_MAP() ;
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTest01();
afx_msg void OnTest02_04(UINT uID);
afx_msg void OnEnChange(); //虽然是 走 WM_COMMAND 消息, 但是分为 notify 消息
};
//消息映射 宏实现
BEGIN_MESSAGE_MAP(CMsgFrame, CFrameWnd )
ON_WM_CREATE()
ON_COMMAND(1001, OnTest01)
ON_COMMAND_RANGE(1001, 1004, OnTest02_04)
ON_EN_CHANGE(1005, OnEnChange)
END_MESSAGE_MAP()
void CMsgFrame::OnEnChange()
{
CWnd *pWnd = GetDlgItem(1005);
CString strText;
pWnd->GetWindowText(strText);
AfxMessageBox(strText);
}
int CMsgFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CreateWindow("BUTTON", "Test01", WS_CHILD|WS_VISIBLE, 50, 50, 200, 30, GetSafeHwnd(), (HMENU)1001, AfxGetApp()->m_hInstance, NULL);
CreateWindow("BUTTON", "Test02", WS_CHILD|WS_VISIBLE, 50, 100, 200, 30, GetSafeHwnd(), (HMENU)1002, AfxGetApp()->m_hInstance, NULL);
CreateWindow("BUTTON", "Test03", WS_CHILD|WS_VISIBLE, 50, 150, 200, 30, GetSafeHwnd(), (HMENU)1003, AfxGetApp()->m_hInstance, NULL);
CreateWindow("BUTTON", "Test04", WS_CHILD|WS_VISIBLE, 50, 200, 200, 30, GetSafeHwnd(), (HMENU)1004, AfxGetApp()->m_hInstance, NULL);
CreateWindow("EDIT", "", WS_CHILD|WS_VISIBLE|WS_BORDER,300, 30, 200, 200, GetSafeHwnd(), (HMENU)1005, AfxGetApp()->m_hInstance, NULL);
return 0;
}
void CMsgFrame::OnTest02_04(UINT uID)
{
CString strInfo;
strInfo.Format("%d", uID);
AfxMessageBox(strInfo);
}
void CMsgFrame::OnTest01()
{
AfxMessageBox("OnTest01");
}
LRESULT CMsgFrame::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//switch ... case
return CFrameWnd::WindowProc(uMsg, wParam, lParam);
}
class CMsgApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
CMsgApp theApp;
BOOL CMsgApp::InitInstance()
{
CMsgFrame *pWnd = new CMsgFrame();
pWnd->Create(NULL, "MsgApp");
this->m_pMainWnd = pWnd;
this->m_pMainWnd->ShowWindow(SW_SHOW);
this->m_pMainWnd->UpdateWindow();
return TRUE;
}
========================================================================
============================================================
// RegisterMsg.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
//注册消息id
UINT g_iRegMsg = RegisterWindowMessage("MYREGMSG");
class CRegisterMsgFrame : public CFrameWnd
{
public:
DECLARE_MESSAGE_MAP() ;
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTest();
afx_msg LRESULT OnRegMsg(WPARAM wParam, LPARAM lParam);
};
BEGIN_MESSAGE_MAP(CRegisterMsgFrame, CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(1001, OnTest)
ON_REGISTERED_MESSAGE(g_iRegMsg, OnRegMsg)
END_MESSAGE_MAP()
void CRegisterMsgFrame::OnTest()
{
SendMessage(g_iRegMsg);
}
LRESULT CRegisterMsgFrame::OnRegMsg(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox("Register Msg");
return 0;
}
int CRegisterMsgFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CreateWindow("BUTTON", "Test", WS_CHILD|WS_VISIBLE, 50, 50, 200, 30, GetSafeHwnd(), (HMENU)1001, AfxGetApp()->m_hInstance, NULL);
//CreateWindow("BUTTON", "Test01", WS_CHILD|WS_VISIBLE, 50, 50, 200, 30, GetSafeHwnd(), (HMENU)1001, AfxGetApp()->m_hInstance, NULL);
return 0;
}
class CRegisterMsgApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
CRegisterMsgApp theApp;
BOOL CRegisterMsgApp::InitInstance()
{
CRegisterMsgFrame *pWnd = new CRegisterMsgFrame();
pWnd->Create(NULL, "Register Msg");
m_pMainWnd = pWnd;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
========================================================================
三 MFC菜单
1.MFC菜单 是 HMENU的封装
2.菜单的用法:
2.1 添加菜单资源
2.2 创建时, 在窗口中添加菜单
2.3 菜单项的命令响应 ON_COMMAND
3. XXXApp中也可以添加消息映射,
因为: 只要继承 CCmdTarget, 都可以处理消息
=======================================================
// MFCMenu.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
class CMenuApp : public CWinApp
{
DECLARE_MESSAGE_MAP()
public:
virtual BOOL InitInstance();
afx_msg void OnAbout();
};
BEGIN_MESSAGE_MAP(CMenuApp, CWinApp)
ON_COMMAND(ID_ABOUT, OnAbout)
END_MESSAGE_MAP()
void CMenuApp::OnAbout()
{
AfxMessageBox("aboout");
}
class CMenuFrame : public CFrameWnd
{
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnExit();
};
BEGIN_MESSAGE_MAP(CMenuFrame, CFrameWnd)
ON_COMMAND(ID_EXIT, OnExit)
END_MESSAGE_MAP()
CMenuApp theApp;
void CMenuFrame::OnExit()
{
::PostQuitMessage(WM_QUIT);
}
BOOL CMenuApp::InitInstance()
{
CMenuFrame *pWnd = new CMenuFrame();
pWnd->Create(NULL, "MenuApp", WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault, NULL, MAKEINTRESOURCE(IDR_MAIN));
m_pMainWnd = pWnd;
m_pMainWnd->ShowWindow(TRUE);
m_pMainWnd->UpdateWindow();
return TRUE;
}
=================================================================