问题缘起
通常如果在对话框中将一个控件映射到一个变量,有三种方法:
1.
2.
3.
例如CEdit m_edit; m_edit.SubclassDlgItem(IDC_EDIT1);
SubclassWindow
AfxGetAfxWndProc返回了AfxWndProc的函数指针,即窗口函数的指针,AfxWndProc包裹了AfxCallWndProc,后者又调用了pWnd->WindowProc(nMsg, wParam, lParam);。
可见SubclassWindow完成了两项功能:
1.
2.
SubclassDlgItem
GetDlgItem
SubclassDlgItem和GetDlgItem二者的区别
HWND hWndControl = ::GetDlgItem(pParent->m_hWnd, IDC_EDIT1);
m_edit.SubclassWindow (hWndControl);
参考资料(MSDN):
This member function attaches the Windows control to a CWnd object and replaces the window’sWndProc and AfxWndProc functions. The function stores a pointer to the old WndProc in the CWndobject.
参考资料(来自CSDN)
http://dev.csdn.net/Develop/article/16/16045.shtm
参考资料(来自Programming Windows)
The window procedure for the scroll bar controls is somewhere inside Windows. However, you can obtain the address of this window procedure by a call to GetWindowLong using the GWL_WNDPROC identifier as a parameter. Moreover, you can set a new window procedure for the scroll bars by calling SetWindowLong. This technique, which is called "window subclassing," is very powerful. It lets you hook into existing window procedures, process some messages within your own program, and pass all other messages to the old window procedure.
The window procedure that does preliminary scroll bar message processing in COLORS1 is namedScrollProc; it is toward the end of the COLORS1.C listing. Because ScrollProc is a function within COLORS1 that is called by Windows, it must be defined as a CALLBACK.
For each of the three scroll bars, COLORS1 uses SetWindowLong to set the address of the new scroll bar window procedure and also obtain the address of the existing scroll bar window procedure:
OldScroll[i] = (WNDPROC) SetWindowLong (hwndScroll[i], GWL_WNDPROC,
Now the function ScrollProc gets all messages that Windows sends to the scroll bar window procedure for the three scroll bars in COLORS1 (but not, of course, for scroll bars in other programs). The ScrollProc window procedure simply changes the input focus to the next (or previous) scroll bar when it receives a Tab or Shift-Tab keystroke. It calls the old scroll bar window procedure using CallWindowProc.
http://www.newsmth.net/pc/pccon.php?id=1363&nid=81054