MyMdi.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // MyMdi.cpp: implementation of the CMyMdi class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "YLGL.h"
  6. #include "MyMdi.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. BOOL CMyMdi::m_bExitCode = true;
  16. void *CMyMdi::m_pMyMdi = NULL;
  17. CMyMdi::CMyMdi()
  18. {
  19. }
  20. CMyMdi::~CMyMdi()
  21. {
  22. }
  23. ////////////////////////////////////////////////////
  24. //pName 窗口名称
  25. //pNewViewClass 窗口RUNTIME_CLASS名称
  26. //nIcon 窗口图标ID
  27. //pParentWnd 父窗口指针
  28. //返回:
  29. // 0 表示窗口已经存在
  30. // 1 打开窗口成功
  31. // -1 分配内存失败
  32. // -2 创建窗口失败
  33. /////////////////////////////////////////////////////
  34. int CMyMdi::Append(char *pName,
  35. CRuntimeClass* pNewViewClass,
  36. UINT nIcon,
  37. CWnd* pParentWnd,
  38. UINT nID)
  39. {
  40. CChildFrame *pView = (CChildFrame*)GetView(pName);
  41. if (pView) //已存在
  42. {
  43. pView->MDIActivate();
  44. return 1;
  45. }
  46. g_bShowBar=0;
  47. MainClose();
  48. #ifdef NEW_SKIN
  49. g_pMainWnd->m_wndClient.ShowCtrls (0);
  50. if(g_pMainWnd2)
  51. {
  52. g_pMainWnd2->ShowBar (0);
  53. }
  54. #endif
  55. g_bShowBar=1;
  56. pView=new CChildFrame;
  57. if (pView == NULL) return -1;
  58. tabMyMdi *pNewMdi =new tabMyMdi;
  59. if (pNewMdi == NULL)
  60. {
  61. delete pView;
  62. return -1;
  63. }
  64. CCreateContext context;
  65. context.m_pNewViewClass=pNewViewClass;
  66. //创建
  67. if(!pView->LoadFrame(nIcon,WS_CHILDWINDOW,
  68. pParentWnd,&context))
  69. return -2;
  70. pNewMdi->pSubView = NULL;
  71. pNewMdi->pView = (CWnd *)pView;
  72. memcpy(pNewMdi->szName, pName, 64);
  73. pNewMdi->pNext = (tabMyMdi*)m_pMyMdi;
  74. m_pMyMdi = (void*)pNewMdi;
  75. pView->InitialUpdateFrame(NULL,true); //真正创建
  76. // pNewMdi->pSubView = pView->GetDlgItem(nID);
  77. return 0;
  78. }
  79. /////////////////////////////////////////////
  80. //pName 窗口名称
  81. //返回窗口的索引
  82. int CMyMdi::GetIndex(char *pName)
  83. /////////////////////////////////////////////
  84. {
  85. if(m_pMyMdi == NULL) return -1;
  86. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  87. int i = 0;
  88. while(pCur)
  89. {
  90. if(strcmp(pName, pCur->szName) == 0)
  91. return i;
  92. pCur = pCur->pNext;
  93. i++;
  94. }
  95. return -1;
  96. }
  97. ///////////////////////////////////////////////////
  98. //pView 窗口指针
  99. //返回窗口名称
  100. char *CMyMdi::GetName(CWnd *pView)
  101. ///////////////////////////////////////////////////
  102. {
  103. /* for (int i = 0; i < m_caView.GetSize(); i++)
  104. {
  105. if (m_caView.GetAt(i) == pView) //有发现
  106. return m_caName.GetAt(i);
  107. }
  108. return _T("");
  109. */
  110. if(m_pMyMdi == NULL) return NULL;
  111. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  112. while(pCur)
  113. {
  114. if(pView == pCur->pView)
  115. return pCur->szName;
  116. pCur = pCur->pNext;
  117. }
  118. return NULL;
  119. }
  120. ///////////////////////////////////////////////////
  121. //pView 窗口指针
  122. //返回窗口名称
  123. CWnd *CMyMdi::GetView(char *pName)
  124. ///////////////////////////////////////////////////
  125. {
  126. if(m_pMyMdi == NULL) return NULL;
  127. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  128. while(pCur)
  129. {
  130. if(strcmp(pName, pCur->szName)==0)
  131. return pCur->pView;
  132. pCur = pCur->pNext;
  133. }
  134. return NULL;
  135. }
  136. ///////////////////////////////////////////////////
  137. //pView 窗口指针
  138. //返回子窗口指针
  139. CWnd *CMyMdi::GetSubView(CWnd *pView)
  140. ///////////////////////////////////////////////////
  141. {
  142. if(m_pMyMdi == NULL) return NULL;
  143. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  144. while(pCur)
  145. {
  146. if(pView == pCur->pView)
  147. return pCur->pSubView;
  148. pCur = pCur->pNext;
  149. }
  150. return NULL;
  151. }
  152. ///////////////////////////////////////////////////
  153. //设置子窗口指针
  154. //pView 窗口指针
  155. //子窗口指针
  156. BOOL CMyMdi::SetSubView(CWnd *pView, CWnd *pDlg)
  157. ///////////////////////////////////////////////////
  158. {
  159. if(m_pMyMdi == NULL) return false;
  160. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  161. while(pCur)
  162. {
  163. if(pView == pCur->pView)
  164. {
  165. pCur->pSubView = pDlg;
  166. return true;
  167. }
  168. pCur = pCur->pNext;
  169. }
  170. return false;
  171. }
  172. ////////////////////////////////////////////////////
  173. //pView 窗口指针
  174. //删除子窗口
  175. BOOL CMyMdi::Delete(CWnd *pView)
  176. ////////////////////////////////////////////////////
  177. {
  178. if(m_pMyMdi == NULL) return false;
  179. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  180. tabMyMdi *pBefore = NULL;
  181. while(pCur)
  182. {
  183. if(pView == pCur->pView)
  184. {
  185. if(pBefore == NULL) //首个
  186. {
  187. m_pMyMdi = (void*)((tabMyMdi*)m_pMyMdi)->pNext;
  188. }
  189. else
  190. {
  191. pBefore->pNext = pCur->pNext;
  192. }
  193. delete pCur;
  194. return true;
  195. }
  196. pBefore = pCur;
  197. pCur = pCur->pNext;
  198. }
  199. return FALSE;
  200. }
  201. //////////////////////////////////////////
  202. //pName 窗口名称
  203. //删除窗口登记
  204. BOOL CMyMdi::Delete(char *pName)
  205. //////////////////////////////////////////
  206. {
  207. if(m_pMyMdi == NULL) return false;
  208. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  209. tabMyMdi *pBefore = NULL;
  210. while(pCur)
  211. {
  212. if(strcmp(pName,pCur->szName)==0)
  213. {
  214. if(pBefore == NULL) //首个
  215. {
  216. m_pMyMdi = (void*)((tabMyMdi*)m_pMyMdi)->pNext;
  217. }
  218. else
  219. {
  220. pBefore->pNext = pCur->pNext;
  221. }
  222. delete pCur;
  223. }
  224. pBefore = pCur;
  225. pCur = pCur->pNext;
  226. return true;
  227. }
  228. return FALSE;
  229. }
  230. //////////////////////////////////////////
  231. //pName 窗口名称
  232. //删除窗口登记
  233. int CMyMdi::GetCount()
  234. //////////////////////////////////////////
  235. {
  236. if(m_pMyMdi == NULL) return -1;
  237. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  238. int i = 0;
  239. while(pCur)
  240. {
  241. pCur = pCur->pNext;
  242. i++;
  243. }
  244. return i;
  245. // return m_caName.GetSize();
  246. }
  247. BOOL CMyMdi::MainClose(BOOL bMsg)
  248. {
  249. m_bExitCode = true;
  250. if(m_pMyMdi == NULL)return true;
  251. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  252. CWnd *pView;
  253. while(pCur)
  254. {
  255. pView = pCur->pView;
  256. pCur = pCur->pNext; if(pView)
  257. pView->SendMessage(WM_CLOSE, 0, 0);
  258. if (GetExitCode()==false) return false;
  259. }
  260. return true;
  261. }
  262. BOOL CMyMdi::ChildClose(CWnd *pView)
  263. {
  264. m_bExitCode = true;
  265. CWnd *pSubView = GetSubView(pView);
  266. if (pSubView)
  267. pSubView->SendMessage(WM_CLOSE, 0,0);
  268. if(GetExitCode())
  269. {
  270. Delete(pView);
  271. return true;
  272. }
  273. return false;
  274. }
  275. void CMyMdi::DeleteAll()
  276. {
  277. if(m_pMyMdi == NULL) return ;
  278. tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  279. tabMyMdi *pBefore;
  280. while(pCur)
  281. {
  282. pBefore = pCur;
  283. pCur = pBefore->pNext;
  284. delete pBefore;
  285. }
  286. }
  287. void CMyMdi::CloseAll()
  288. {
  289. MainClose();
  290. }