分类: C/C++
2008-08-07 10:28:44
typedef struct tagWNDCLASSA(注:该结构为ANSII版本) { UINT style ; WNDPROC lpfnWndProc ; int cbClsExtra ; int cbWndExtra ; HINSTANCE hInstance ; HICON hIcon ; HCURSOR hCursor ; HBRUSH hbrBackground ; LPCSTR lpszMenuName ; LPCSTR lpszClassName ; }WNDCLASSA, * PWNDCLASSA, NEAR * NPWNDCLASSA, FAR * LPWNDCLASSA ;
#include这是一个标准的Windows程序代码,程序被启动后,填写一个窗口类,然后调用RegisterClass将该类注册,接着创建该窗口,最后显示窗口和进入消息循环。LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("HelloWin") ; WNDCLAS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuNam = NULL ; wndclass.lpszClassName = szAppName ; RegisterClass (&wndclass); hwnd = CreateWindow( szAppName, // window class name TEXT ("The Hello Program"), // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; }
#define AFX_WND_REG (0x0001) #define AFX_WNDCONTROLBAR_REG (0x0002) #define AFX_WNDMDIFRAME_REG (0x0004) #define AFX_WNDFRAMEORVIEW_REG (0x0008) #define AFX_WNDDOLECONTROL_REG (0x0020)在WINCORE.cpp定义了这些窗口类对应的字符串名称:
const TCHAR _afxWnd[] = AFX_WND; const TCHAR _afxWndControlBar[] = AFX_WNDCONTROLBAR; const TCHAR _afxWndMDIFrame[] = AFX_WNDMDIFRAME; const TCHAR _afxWndFrameOrView[] = AFX_WNDFRAMEORVIEW; const TCHAR _afxWndOleControl[] = AFX_WNDOLERONTROL;在AFXIMPL.h中定义了五个AFX_XXX对应的字符串:
#define AFX_WND AFX_WNDCLASS("WND") #define AFX_WNDCONTROLBAR AFX_WNDCLASS("ControlBar") #define AFX_WNDMDIFRAME AFX_WNDCLASS("MDIFrame") #define AFX_WNDFRAMEORVIEW AFX_WNDCLASS("FrameOrView") #define AFX_WNDOLECONTROL AFX_WNDCLASS("OleControl")看到这里也许有些心急了,其实上面一堆代码只是定义了五个默认窗口类的字符串名称和二进制名称,具体注册行为在全局函数AfxDeferRegisterClass中:
#define AfxDeferRegisterClass(fClass) \ ((afxRegisteredClasses & fClass) ? TRUE:AfxEndDeferRegisterClass(fClass) #define afxRegisteredClasses AfxGetModuleState()->m_fRegisteredClasses BOOL AFXAPI AfxEndDeferRegisterClass(short fClass) { WNDCLASS wndCls; wndCls.lpfnWndProc = DefWindowProc; if(fClass & AFX_WND_REG) { wndCls.lpszClassName=_afxWnd; AfxRegisterClass(&wndCls); }else if(fClass & AFX_WNDOLECONTROL_REG) { wndCls.lpszClassName=_afxWndOleControl; AfxRegisterClass(&wndCls); }else if(fClass & AFX_WNDCONTROLBAR_REG) { wndCls.lpszClassName=_afxWndControlBar; AfxRegisterClass(&wndCls); }else if(fClass & AFX_WNDMDIFRAME_REG) { RegisterWithIcon(&wndCls,_afxWndMDIFrame,AFX_IDI_MDIFRAME); }else if(fClass & AFX_WNDFRAMEORVIEW_REG) { RegisterWithIcon(&wndCls,_afxWndFrameOrView,AFX_IDI_STD_FRAME); }else if(fClass & AFX_WNDCOMMCTLS_REG) { InitCommonControls(); } }从上面的代码可以看出,AfxDeferRegisterClass函数首先判断该窗口类是否注册,如已注册则直接返回,否则调用AfxEndDeferRegisterClass进行注册,即注册要求的默认窗口类。其中RegisterWithIcon和InitCommonControls最终也是转化为调用AfxRegisterClass,而AfxRegisterClass函数调用RegisterClass进行注册,啊,终于看到SDK中的RegisterClass了,看到它总有一种亲切感!
BOOL CWnd::CreateEx(DWORD dwExStyle, LPCSTSTR lpszClassName, …… LPVOID lpParam) { CREATESTRUCT cs; cs.dwExStyle = dwExStyle; … … cs.lpCreateParams = lpParam; PreCreateWindow(cs); AfxHookWindowCreate(this); HWND hWnd=::CreateWindowEx(cs.dwStyle,cs.lpszClass,…,cs.lpCreateParams); …… }啊,一看到CreateWindowEx,亲切感又来了,这不是和SDK中的CreateWindow一样嘛,是创建窗口!既然这样,那么注册窗口肯定在该函数之前,会是谁呢?如果你做过一点MFC程序,你就会对将眼光停留PreCreateWindow上。对!就是它了。
BOOL CWnd::PreCreateWindow(CREATESTRUCT &cs) { if(cs.lpszClass = = NULL) { AfxDeferRegisterClass(AFX_WND_REG); cs.lpszClass = _afxWnd; } return TRUE; } BOOL CFrameWnd::PreCreateWindow(CREATESTRUCT &cs) { if(cs.lpszClass = = NULL) { AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG); cs.lpszClass = _afxWndFrameOrView; } return TRUE; } BOOL CMDIFrameWnd::PreCreateWindow(CREATESTRUCT &cs) { if(cs.lpszClass = = NULL) { AfxDeferRegisterClass(AFX_WNDMDIFRAME_REG); cs.lpszClass = _afxWndMDIFrame; } } BOOL CMDIChildWnd::PreCreateWindow(CREATESTRUCT &cs) { return CFrameWnd::PreCreateWindow(cs); } BOOL CView::PreCreateWindow(CREATESTRUCT &cs) { if(cs.lpszClass = = NULL) { AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG); cs.lpszClass = _afxWndFrameOrView; } }就是通过继承的方法,MFC实现常用类的窗口注册(代码并不完全,是从MFC中抽取对我们有意义的一部分代码)。