// MyMdi.cpp: implementation of the CMyMdi class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "LYFZIPManage.h" #include "MyMdi.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// BOOL CMyMdi::m_bExitCode = true; void *CMyMdi::m_pMyMdi = NULL; CMyMdi::CMyMdi() { } CMyMdi::~CMyMdi() { } //////////////////////////////////////////////////// //pName 窗口名称 //pNewViewClass 窗口RUNTIME_CLASS名称 //nIcon 窗口图标ID //pParentWnd 父窗口指针 //返回: // 0 表示窗口已经存在 // 1 打开窗口成功 // -1 分配内存失败 // -2 创建窗口失败 ///////////////////////////////////////////////////// int CMyMdi::Append(char *pName, CRuntimeClass* pNewViewClass, UINT nIcon, CWnd* pParentWnd, UINT nID) { CChildFrame *pView = (CChildFrame*)GetView(pName); if (pView) //已存在 { pView->MDIActivate(); return 1; } MainClose(); pView=new CChildFrame; if (pView == NULL) return -1; tabMyMdi *pNewMdi =new tabMyMdi; if (pNewMdi == NULL) { delete pView; return -1; } CCreateContext context; context.m_pNewViewClass=pNewViewClass; //创建 if(!pView->LoadFrame(nIcon,WS_CHILDWINDOW, pParentWnd,&context)) return -2; pNewMdi->pSubView = NULL; pNewMdi->pView = (CWnd *)pView; memcpy(pNewMdi->szName, pName, 64); pNewMdi->pNext = (tabMyMdi*)m_pMyMdi; m_pMyMdi = (void*)pNewMdi; pView->InitialUpdateFrame(NULL,true); //真正创建 // pNewMdi->pSubView = pView->GetDlgItem(nID); return 0; } ///////////////////////////////////////////// //pName 窗口名称 //返回窗口的索引 int CMyMdi::GetIndex(char *pName) ///////////////////////////////////////////// { if(m_pMyMdi == NULL) return -1; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; int i = 0; while(pCur) { if(strcmp(pName, pCur->szName) == 0) return i; pCur = pCur->pNext; i++; } return -1; } /////////////////////////////////////////////////// //pView 窗口指针 //返回窗口名称 char *CMyMdi::GetName(CWnd *pView) /////////////////////////////////////////////////// { /* for (int i = 0; i < m_caView.GetSize(); i++) { if (m_caView.GetAt(i) == pView) //有发现 return m_caName.GetAt(i); } return _T(""); */ if(m_pMyMdi == NULL) return NULL; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; while(pCur) { if(pView == pCur->pView) return pCur->szName; pCur = pCur->pNext; } return NULL; } /////////////////////////////////////////////////// //pView 窗口指针 //返回窗口名称 CWnd *CMyMdi::GetView(char *pName) /////////////////////////////////////////////////// { if(m_pMyMdi == NULL) return NULL; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; while(pCur) { if(strcmp(pName, pCur->szName)==0) return pCur->pView; pCur = pCur->pNext; } return NULL; } /////////////////////////////////////////////////// //pView 窗口指针 //返回子窗口指针 CWnd *CMyMdi::GetSubView(CWnd *pView) /////////////////////////////////////////////////// { if(m_pMyMdi == NULL) return NULL; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; while(pCur) { if(pView == pCur->pView) return pCur->pSubView; pCur = pCur->pNext; } return NULL; } /////////////////////////////////////////////////// //设置子窗口指针 //pView 窗口指针 //子窗口指针 BOOL CMyMdi::SetSubView(CWnd *pView, CWnd *pDlg) /////////////////////////////////////////////////// { if(m_pMyMdi == NULL) return false; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; while(pCur) { if(pView == pCur->pView) { pCur->pSubView = pDlg; return true; } pCur = pCur->pNext; } return false; } //////////////////////////////////////////////////// //pView 窗口指针 //删除子窗口 BOOL CMyMdi::Delete(CWnd *pView) //////////////////////////////////////////////////// { if(m_pMyMdi == NULL) return false; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; tabMyMdi *pBefore = NULL; while(pCur) { if(pView == pCur->pView) { if(pBefore == NULL) //首个 { m_pMyMdi = (void*)((tabMyMdi*)m_pMyMdi)->pNext; } else { pBefore->pNext = pCur->pNext; } delete pCur; return true; } pBefore = pCur; pCur = pCur->pNext; } return FALSE; } ////////////////////////////////////////// //pName 窗口名称 //删除窗口登记 BOOL CMyMdi::Delete(char *pName) ////////////////////////////////////////// { if(m_pMyMdi == NULL) return false; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; tabMyMdi *pBefore = NULL; while(pCur) { if(strcmp(pName,pCur->szName)==0) { if(pBefore == NULL) //首个 { m_pMyMdi = (void*)((tabMyMdi*)m_pMyMdi)->pNext; } else { pBefore->pNext = pCur->pNext; } delete pCur; } pBefore = pCur; pCur = pCur->pNext; return true; } return FALSE; } ////////////////////////////////////////// //pName 窗口名称 //删除窗口登记 int CMyMdi::GetCount() ////////////////////////////////////////// { if(m_pMyMdi == NULL) return -1; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; int i = 0; while(pCur) { pCur = pCur->pNext; i++; } return i; // return m_caName.GetSize(); } BOOL CMyMdi::MainClose(BOOL bMsg) { m_bExitCode = true; if(m_pMyMdi == NULL)return true; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; CWnd *pView; while(pCur) { pView = pCur->pView; pCur = pCur->pNext; if(pView) pView->SendMessage(WM_CLOSE, 0, 0); if (GetExitCode()==false) return false; } return true; } BOOL CMyMdi::ChildClose(CWnd *pView) { m_bExitCode = true; CWnd *pSubView = GetSubView(pView); if (pSubView) pSubView->SendMessage(WM_CLOSE, 0,0); if(GetExitCode()) { Delete(pView); return true; } return false; } void CMyMdi::DeleteAll() { if(m_pMyMdi == NULL) return ; tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi; tabMyMdi *pBefore; while(pCur) { pBefore = pCur; pCur = pBefore->pNext; delete pBefore; } } void CMyMdi::CloseAll() { MainClose(); }