MyTreeView.cpp
上传用户:power_led
上传日期:2013-04-11
资源大小:373k
文件大小:7k
源码类别:

ICQ/即时通讯

开发平台:

Visual C++

  1. // MyTreeView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CChatServer.h"
  5. #include "MyTreeView.h"
  6. #include "ClientSocket.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMyTreeView
  14. IMPLEMENT_DYNCREATE(CMyTreeView, CTreeView)
  15. CMyTreeView::CMyTreeView():m_Tree(GetTreeCtrl())
  16. {
  17. }
  18. CMyTreeView::~CMyTreeView()
  19. {
  20. }
  21. BEGIN_MESSAGE_MAP(CMyTreeView, CTreeView)
  22. //{{AFX_MSG_MAP(CMyTreeView)
  23. ON_WM_CREATE()
  24. ON_WM_TIMER()
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CMyTreeView drawing
  29. void CMyTreeView::OnDraw(CDC* pDC)
  30. {
  31. CDocument* pDoc = GetDocument();
  32. // TODO: add draw code here
  33. }
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CMyTreeView diagnostics
  36. #ifdef _DEBUG
  37. void CMyTreeView::AssertValid() const
  38. {
  39. CTreeView::AssertValid();
  40. }
  41. void CMyTreeView::Dump(CDumpContext& dc) const
  42. {
  43. CTreeView::Dump(dc);
  44. }
  45. #endif //_DEBUG
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CMyTreeView message handlers
  48. void CMyTreeView::OnInitialUpdate() 
  49. {
  50. CTreeView::OnInitialUpdate();
  51. }
  52. extern CCChatServerApp theApp;
  53. int CMyTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  54. {
  55. if (CTreeView::OnCreate(lpCreateStruct) == -1)
  56. return -1;
  57. // TODO: Add your specialized creation code here
  58. m_ImageList.Create(IDB_IMAGELIST,32,1,RGB(255,0,255));
  59. m_Tree.SetImageList(&m_ImageList,TVSIL_NORMAL);
  60. m_ParentItem = m_Tree.InsertItem("聊天室列表",2,3);
  61. POSITION pos = theApp.m_ChatRoomList.GetHeadPosition();
  62. for(int i = 0; i < theApp.m_ChatRoomList.GetCount(); i ++)
  63. {
  64. CString * m_pstrRoom = static_cast <CString *>(theApp.m_ChatRoomList.GetNext(pos));
  65. m_Tree.InsertItem(m_pstrRoom->GetBuffer(8),2,3,m_ParentItem);
  66. // m_pstrRoom = static_cast <CString *>(theApp.m_ChatRoomList.GetNext(pos));
  67. }
  68. m_Tree.Expand(m_ParentItem,TVE_EXPAND);
  69. return 0;
  70. }
  71. void CMyTreeView::OnClientOut(Message wParam, LPARAM lParam)
  72. {
  73. Message msObj = (Message)wParam;
  74. CString strRoom = msObj.strRoom;
  75. CString strUsrName = msObj.strName;
  76. HTREEITEM hItem = m_Tree.GetChildItem(m_ParentItem);
  77. BOOL bFind = FALSE;
  78. while(hItem && !bFind)
  79. {
  80. CString strNodeName = m_Tree.GetItemText(hItem);
  81. if(strNodeName == strRoom)
  82. {
  83. //查找用户名的那一项
  84. HTREEITEM hChildItem = m_Tree.GetChildItem(hItem);
  85. while(hChildItem)
  86. {
  87. CString strUserNode = m_Tree.GetItemText(hChildItem);
  88. if(strUserNode == strUsrName)
  89. {
  90. m_Tree.DeleteItem(hChildItem);
  91. m_Tree.Expand(hItem,TVE_EXPAND);
  92. bFind = TRUE;
  93. break;
  94. }
  95. hChildItem = m_Tree.GetNextItem(hChildItem,1);
  96. }
  97. }
  98. hItem = m_Tree.GetNextItem(hItem,1);
  99. }
  100. //向所有用户发送用户退出消息
  101. //重新构造msObj
  102. msObj .iType = USERLOG;
  103. msObj .iSubType = USERLOGOUT;
  104. int iLen = strUsrName.GetLength();
  105. iLen > 20 ? 20 : iLen;
  106. lstrcpy(msObj .strName,strUsrName.GetBuffer(iLen));
  107. strUsrName.ReleaseBuffer();
  108. iLen = strRoom.GetLength();
  109. iLen > 20 ? 20 : iLen;
  110. lstrcpy(msObj .strRoom,strRoom.GetBuffer(iLen));
  111. strRoom.ReleaseBuffer();
  112. g_cs.Lock();
  113. CPtrList * m_pClientList = theApp.m_pClientSocketList;
  114. POSITION pos = m_pClientList ->GetHeadPosition();
  115. if(pos)
  116. {
  117. CClientSocket *  m_pClientSocket;
  118. for(int i = 0; i < m_pClientList ->GetCount(); i++)
  119. {
  120. m_pClientSocket = static_cast < CClientSocket *>(m_pClientList->GetNext(pos));
  121. CString strTemp = m_pClientSocket ->GetRoomName();
  122. m_pClientSocket ->Send(&msObj,sizeof(Message));
  123. Sleep(100);
  124. }
  125. }
  126. g_cs.Unlock();
  127. }
  128. void CMyTreeView::OnNewClientIn(Message wParam, LPARAM lParam)
  129. {
  130. Message msObj = wParam;
  131. CString strRoom = msObj.strRoom;
  132. CString strUsrName = msObj.strName;
  133. HTREEITEM hItem = m_Tree.GetChildItem(m_ParentItem);
  134. while(hItem)
  135. {
  136. CString strNodeName = m_Tree.GetItemText(hItem);
  137. if(strNodeName == strRoom)
  138. {
  139. m_Tree.InsertItem(strUsrName,0,1,hItem);
  140. m_Tree.Expand(hItem,TVE_EXPAND);
  141. break;
  142. }
  143. hItem = m_Tree.GetNextItem(hItem,1);
  144. }
  145. //给所有的用户发送消息
  146. msObj .iType = USERLOG;
  147. msObj .iSubType = USERLOGIN;
  148. int iLen = strUsrName.GetLength();
  149. iLen > 20 ? 20 : iLen;
  150. lstrcpy(msObj .strName,strUsrName.GetBuffer(iLen));
  151. strUsrName.ReleaseBuffer();
  152. iLen = strRoom.GetLength();
  153. iLen > 20 ? 20 : iLen;
  154. lstrcpy(msObj .strRoom,strRoom.GetBuffer(iLen));
  155. strRoom.ReleaseBuffer();
  156. g_cs.Lock();
  157. CPtrList * m_pClientList = theApp.m_pClientSocketList;
  158. int iCount = m_pClientList->GetCount();
  159. POSITION pos = m_pClientList ->GetHeadPosition();
  160. if(pos)
  161. {  
  162. for(int i = 0; i < m_pClientList ->GetCount(); i++)
  163. {
  164. CClientSocket *m_pClientSocket = static_cast < CClientSocket *>(m_pClientList->GetNext(pos));
  165. iLen = m_pClientSocket ->Send(&msObj,sizeof(Message));
  166. Sleep(100);
  167. }
  168. }
  169. g_cs.Unlock();
  170. return;
  171. }
  172. void CMyTreeView::OnChangeRoom(Message wParam, LPARAM lParam)
  173. {
  174. Message msObj = wParam;
  175. CString strName = msObj .strName;
  176. CString strNewRoom = msObj.strContent;
  177. int iType = msObj .iType;
  178. int iSubType = msObj.iSubType;
  179. OnClientOut(msObj,0);
  180. msObj .iSubType = iSubType;
  181. msObj .iType = iType;
  182. int iLen = strName.GetLength();
  183. iLen > 20 ? 20 : iLen;
  184. lstrcpy(msObj .strName,strName.GetBuffer(iLen));
  185. iLen = strNewRoom.GetLength();
  186. iLen > 20 ? 20 : iLen;
  187. lstrcpy(msObj .strRoom,strNewRoom.GetBuffer(iLen));
  188. strNewRoom.ReleaseBuffer();
  189. OnNewClientIn(msObj,lParam);
  190. return;
  191. }
  192. void CMyTreeView::OnSendInPrivate(Message wParam,LPARAM lParam)
  193. {
  194. Message msObj = wParam;
  195. CString strUserName = msObj.strName;
  196. CString strClientName = msObj.strClientName;
  197. g_cs.Lock();
  198. CPtrList * m_pClientList = theApp.m_pClientSocketList;
  199. int iCount = m_pClientList->GetCount();
  200. POSITION pos = m_pClientList ->GetHeadPosition();
  201. //找到用户发送
  202. if(pos)
  203. {  
  204. for(int i = 0; i < m_pClientList ->GetCount(); i++)
  205. {
  206. CClientSocket *m_pClientSocket = static_cast < CClientSocket *>(m_pClientList->GetNext(pos));
  207. CString strName = m_pClientSocket ->GetUserName();
  208. if(strName == strClientName )
  209. {
  210. m_pClientSocket ->Send(&msObj,sizeof(Message));
  211. CString strOut;
  212. strOut.Format("%d %d %s %sn",msObj.iType,msObj.iSubType,msObj.strRoom,msObj.strName);
  213. TRACE0(strOut);
  214. g_cs.Unlock();
  215. return;
  216. }
  217. }
  218. }
  219. g_cs.Unlock();
  220. return;
  221. }
  222. void CMyTreeView::OnTimer(UINT nIDEvent) 
  223. {
  224. // TODO: Add your message handler code here and/or call default
  225. CTreeView::OnTimer(nIDEvent);
  226. }
  227. BOOL CMyTreeView::PreCreateWindow(CREATESTRUCT& cs) 
  228. {
  229. // TODO: Add your specialized code here and/or call the base class
  230. cs.style |= TVS_HASLINES;
  231. cs.style |= TVS_LINESATROOT;
  232. cs.style |= TVS_HASBUTTONS;
  233. return CTreeView::PreCreateWindow(cs);
  234. }