2008年(909)
分类:
2008-05-06 22:03:22
一、概述
MDI窗口包含一个框架窗口和若干子窗口。
实际上,框架窗口本身是一个普通主窗口,不过它的客户去被一个特殊窗口覆盖。
这个特殊窗口是系统预定义的“窗口类”,类名称为:"MDICLIENT"。它负责各个MDI子窗口的管理。
二、窗口建立
1.注册一个MDI框架窗口类,提供MDI框架窗口消息处理函数
MDI框架窗口消息处理函数中,将未处理消息交由DefFrameProc处理
//MDI框架窗口消息处理函数 LRESULT CALLBACK MDIFrameWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { //... //其他消息交给由系统提供的缺省框架处理函数DefFrameProc //其中,第二个参数是客户区窗口句柄 return ::DefFrameProc (hwnd,hwndClient, message, wParam, lParam) ; }2.注册多个MDI子窗口类、对应提供各MDI子窗口的消息处理函数
//MDI子窗口消息处理函数 LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { //... //... //其他消息交给由系统提供的缺省MDI子窗口处理函数 return ::DefMDIChildProc (hwnd, message, wParam, lParam) ; }3.在框架窗口的客户区建立MDI管理子窗口
在主窗口收到WM_CREATE消息后:
case WM_CREATE: { hinst=((LPCREATESTRUCT) lParam)->hInstance; //填充CLIENTCREATESTRUCT结构 CLIENTCREATESTRUCT clientcreate ; clientcreate.hWindowMenu = hMenuInitWindow ; //用于添加窗口列表的菜单句柄 clientcreate.idFirstChild = 50000 ; //起始ID hwndClient =CreateWindowEx(0, "MDICLIENT", //类名称为"MDICLIENT" NULL, WS_CHILD |WS_CLIPCHILDREN| WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)1,//ID hinst, //实例句柄 &clientcreate); //参数 } return 0;窗口的大小没有关系,缺省的框架窗口消息处理函数为让它覆盖整个客户区。
4.MDI子窗口的建立
可以在菜单中添加命令项,以建立子窗口。
框架窗口的消息处理函数收到命令后,向MDI客户区窗口发建立命令。
case ID_NEW: { MDICREATESTRUCT mdicreate; mdicreate.szClass = szMDIChildName ; //MDI子窗口的类名称 mdicreate.szTitle = TEXT ("Hello") ; mdicreate.hOwner = hinst ; mdicreate.x = CW_USEDEFAULT ; mdicreate.y = CW_USEDEFAULT ; mdicreate.cx = CW_USEDEFAULT ; mdicreate.cy = CW_USEDEFAULT ; mdicreate.style = 0 ; mdicreate.lParam = 0 ; SendMessage ( hwndClient, //MDI客户区窗口句柄 WM_MDICREATE, //创建MDI子窗口 0, (LPARAM) (LPMDICREATESTRUCT) &mdicreate //创建参数 ) ; } break;三、消息循环中处理针对MDI的热键
while (GetMessage (&msg, NULL, 0, 0)) { if (!TranslateMDISysAccel (hwndClient, &msg) && !TranslateAccelerator (hwndFrame, hAccel, &msg)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } }四、命令的流向
case WM_COMMAND: switch (LOWORD (wParam)) { //针对框架的命令 case ID_ONE: //... return 0; //针对MDI子窗口管理的命令 case IDM_WINDOW_TILE: SendMessage (hwndClient, WM_MDITILE, 0, 0) ; return 0 ; //针对子窗口的命令又子窗口去处理 default: hwndChild = (HWND) SendMessage (hwndClient, WM_MDIGETACTIVE, 0, 0) ; if (IsWindow (hwndChild)) SendMessage (hwndChild, WM_COMMAND, wParam, lParam) ; break ; //..and then to DefFrameProc } break ; //跳出针对WM_COMMAND的case分支,又DefFrameProc处理剩下的命令五、子窗口的管理
case WM_COMMAND: switch (LOWORD (wParam)) { case IDM_WINDOW_TILE: SendMessage (hwndClient, WM_MDITILE, 0, 0) ; return 0 ; case IDM_WINDOW_CASCADE: SendMessage (hwndClient, WM_MDICASCADE, 0, 0) ; return 0 ; case IDM_WINDOW_ARRANGE: SendMessage (hwndClient, WM_MDIICONARRANGE, 0, 0) ; return 0; //... //... } break;2.当前子窗口的关闭
如果用户直接按下子窗口的关闭按钮,则WM_CLOSE消息直接发送到了子窗口消息处理函数。
例如:
框架窗口命令处理中:
case IDM_FILE_CLOSE: //获得当前激活窗口 hwndChild = (HWND) SendMessage (hwndClient, WM_MDIGETACTIVE, 0, 0); //询问通过后,销毁窗口 if (SendMessage (hwndChild, WM_QUERYENDSESSION, 0, 0)) SendMessage (hwndClient, WM_MDIDESTROY, (WPARAM) hwndChild, 0); return 0;子窗口的消息处理函数中:
LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { //... //... case WM_QUERYENDSESSION: case WM_CLOSE: if (IDOK != MessageBox (hwnd, TEXT ("OK to close window?"), TEXT ("Hello"), MB_ICONQUESTION | MB_OKCANCEL)) return 0 ; break ; // i.e., call DefMDIChildProc } return DefMDIChildProc (hwnd, message, wParam, lParam) ; }3.关闭所有子窗口
case IDM_WINDOW_CLOSEALL: //针对所有子窗口执行CloseEnumProc EnumChildWindows (hwndClient, CloseEnumProc, 0) ; return 0 ;枚举函数:
BOOL CALLBACK CloseEnumProc (HWND hwnd, LPARAM lParam) { if (GetWindow (hwnd, GW_OWNER)) // Check for icon title return TRUE ; SendMessage (GetParent (hwnd), WM_MDIRESTORE, (WPARAM) hwnd, 0) ; if (!SendMessage (hwnd, WM_QUERYENDSESSION, 0, 0)) return TRUE ; SendMessage (GetParent (hwnd), WM_MDIDESTROY, (WPARAM) hwnd, 0) ; return TRUE ; }六、菜单控制
case WM_MDIACTIVATE: //激活时,设置框架菜单 if (lParam == (LPARAM) hwnd) SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuHello, (LPARAM) hMenuHelloWindow) ; //失去焦点时,将框架菜单还原 if (lParam != (LPARAM) hwnd) SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuInit, (LPARAM) hMenuInitWindow) ; DrawMenuBar (hwndFrame) ; //注: hwndFrame的得到方法: //hwndClient = GetParent (hwnd) ; //hwndFrame = GetParent (hwndClient) ; return 0 ;(全文完) 下载本文示例代码