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