SearchDlg.cpp
上传用户:guangzhiyw
上传日期:2007-01-09
资源大小:495k
文件大小:7k
源码类别:

ICQ/即时通讯

开发平台:

Visual C++

  1. // SearchDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "client.h"
  5. #include "SearchDlg.h"
  6. #include "frienddetail.h"
  7. #include "addfrienddlg.h"
  8. #include "MainFrm.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CSearchDlg dialog
  16. CSearchDlg::CSearchDlg(CWnd* pParent /*=NULL*/)
  17. : CDialog(CSearchDlg::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CSearchDlg)
  20. m_RID = -1;
  21. //}}AFX_DATA_INIT
  22. hIcon=AfxGetApp()->LoadIcon(IDR_SEARCH);
  23. m_pDlg=NULL;
  24. bStartSearch=FALSE;
  25. }
  26. void CSearchDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. //{{AFX_DATA_MAP(CSearchDlg)
  30. DDX_Control(pDX, IDC_BYNAME, m_Name);
  31. DDX_Control(pDX, IDC_BYID, m_ID);
  32. DDX_Control(pDX, IDC_LIST1, m_List);
  33. DDX_Radio(pDX, IDC_RADIO_ID, m_RID);
  34. //}}AFX_DATA_MAP
  35. }
  36. BEGIN_MESSAGE_MAP(CSearchDlg, CDialog)
  37. //{{AFX_MSG_MAP(CSearchDlg)
  38. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  39. ON_NOTIFY(NM_RCLICK, IDC_LIST1, OnRclickList1)
  40. ON_BN_CLICKED(IDC_RADIO_ID, OnRadioId)
  41. ON_BN_CLICKED(IDC_RADIO_NAME, OnRadioName)
  42. ON_COMMAND(ID_FRIENDS_DETAIL, OnFriendsDetail)
  43. ON_COMMAND(ID_ADD_FRIEND, OnAddFriend)
  44. ON_WM_CLOSE()
  45. //}}AFX_MSG_MAP
  46. ON_MESSAGE(WM_RECIEVE_MSG,OnRecvSearch)
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CSearchDlg message handlers
  50. BOOL CSearchDlg::OnInitDialog() 
  51. {
  52. CDialog::OnInitDialog();
  53. SetIcon(hIcon,TRUE);
  54. SetIcon(hIcon,FALSE);
  55. CMenu* pMenu=GetSystemMenu(FALSE);
  56. pMenu->EnableMenuItem(SC_RESTORE,MF_GRAYED|MF_DISABLED);
  57. pMenu->EnableMenuItem(SC_SIZE,MF_GRAYED|MF_DISABLED);
  58. pMenu->EnableMenuItem(SC_MAXIMIZE,MF_GRAYED|MF_DISABLED);
  59. CRect rect;
  60. LV_COLUMN lvcolumn;
  61. TCHAR rgtsz[3][10] = {_T("号码"), _T("姓名"), _T("部门")};
  62. m_List.GetClientRect(&rect);
  63. int cx[3]={rect.Width()*3/10,rect.Width()*3/10,rect.Width()*4/10};
  64. for (int i=0;i<3;i++)
  65. {
  66. lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  67. lvcolumn.fmt = LVCFMT_LEFT;
  68. lvcolumn.pszText = rgtsz[i];
  69. lvcolumn.iSubItem = i;
  70. lvcolumn.cx=cx[i];
  71. m_List.InsertColumn(i, &lvcolumn);
  72. }
  73. m_RID=0;
  74. m_Name.EnableWindow(FALSE);
  75. m_List.SetImageList(&GetApp()->m_imaSmallHead,LVSIL_SMALL);
  76. UpdateData(FALSE);
  77. return TRUE;  
  78. }
  79. void CSearchDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
  80. {
  81. PostMessage(WM_COMMAND,ID_FRIENDS_DETAIL,0);
  82. *pResult = 0;
  83. }
  84. void CSearchDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult) 
  85. {
  86. CMenu menu,*pMenu=NULL;
  87. if(!menu.LoadMenu(IDR_POPMENU))
  88. return;
  89. pMenu=menu.GetSubMenu(1);
  90. CPoint mouse;
  91. GetCursorPos(&mouse);
  92. pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
  93. mouse.x,
  94. mouse.y,
  95. this);
  96. *pResult = 0;
  97. }
  98. void CSearchDlg::OnRadioId() 
  99. {
  100. m_Name.EnableWindow(FALSE);
  101. m_ID.EnableWindow(TRUE);
  102. m_Name.SetWindowText("");
  103. }
  104. void CSearchDlg::OnRadioName() 
  105. {
  106. m_Name.EnableWindow(TRUE);
  107. m_ID.EnableWindow(FALSE);
  108. m_ID.SetWindowText("");
  109. }
  110. void CSearchDlg::OnFriendsDetail() 
  111. {
  112. memset(&Info,0,sizeof(Info));
  113. POSITION pos=m_List.GetFirstSelectedItemPosition();
  114. if(pos==NULL)return;
  115. int index=m_List.GetNextSelectedItem(pos);
  116. Info.Id=m_List.GetItemData(index);
  117. Info.HaveDetail=0;
  118. if(m_pDlg==NULL)
  119. {
  120. m_pDlg=new CFriendDetailDlg;
  121. m_pDlg->Create(CFriendDetailDlg::IDD,GetDesktopWindow());
  122. }
  123. m_pDlg->SendMessage(WM_SHOW_FRIEND_DETAIL,(WPARAM)&Info,0);
  124. m_pDlg->OnRefresh();
  125. }
  126. void CSearchDlg::OnAddFriend() 
  127. {
  128. POSITION pos=m_List.GetFirstSelectedItemPosition();
  129. if(pos==NULL)return;
  130. int index=m_List.GetNextSelectedItem(pos);
  131. CAddFriendDlg *& pDlg=((CMainFrame*)GetApp()->m_pMainWnd)->m_pAddFriendDlg;
  132. if(pDlg==NULL)
  133. {
  134. pDlg=new CAddFriendDlg;
  135. pDlg->Create(CAddFriendDlg::IDD);
  136. }
  137. pDlg->uID=m_List.GetItemData(index);
  138. pDlg->SendMessage(WM_NOTICE_ADDFRIEND);
  139. }
  140. LRESULT CSearchDlg::OnRecvSearch(WPARAM wParam,LPARAM lParam)
  141. {
  142. CData *pData=(CData*)wParam;
  143. switch(pData->index)
  144. {
  145. case FOUND_FRIEND_BY_ID:
  146. {
  147. if(!bStartSearch)break;
  148. bStartSearch=FALSE;
  149. CMsgPerson msg;
  150. CopyDataBuf(&msg,pData);
  151. if(!msg.LoadFromBuf())break;
  152. m_List.DeleteAllItems();
  153. LVITEM lv;
  154. int iPos;
  155. char temp[100];
  156. lv.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
  157. lv.iImage=msg.PhotoId;
  158. lv.iSubItem=0;
  159. lv.iItem=0;
  160. itoa(msg.MyId,temp,10);
  161. lv.pszText=temp;
  162. lv.lParam=msg.MyId;
  163. iPos=m_List.InsertItem(&lv);
  164. lv.mask=LVIF_TEXT;
  165. lv.iItem=iPos;
  166. lv.iSubItem=1;
  167. lstrcpy(temp,msg.Name);
  168. lv.pszText=temp;
  169. m_List.SetItem(&lv);
  170. lv.iSubItem=2;
  171. lstrcpy(temp,msg.Department);
  172. lv.pszText=temp;
  173. m_List.SetItem(&lv);
  174. }
  175. case FOUND_FRIEND_BY_NAME:
  176. {
  177. if(!bStartSearch)break;
  178. bStartSearch=FALSE;
  179. CShowOnlinePeople msg;
  180. CopyDataBuf(&msg,pData);
  181. if(!msg.LoadFromBuf())break;
  182. m_List.DeleteAllItems();
  183. LVITEM lv;
  184. int iPos;
  185. char temp[100];
  186. for(int k=0;k<msg.aId.GetSize();k++)
  187. {
  188. lv.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
  189. lv.iImage=msg.aPhotoId.GetAt(k);
  190. lv.iSubItem=0;
  191. lv.iItem=k;
  192. itoa(msg.aId.GetAt(k),temp,10);
  193. lv.pszText=temp;
  194. lv.lParam=msg.aId.GetAt(k);
  195. iPos=m_List.InsertItem(&lv);
  196. lv.mask=LVIF_TEXT;
  197. lv.iItem=iPos;
  198. lv.iSubItem=1;
  199. lstrcpy(temp,msg.aName.GetAt(k));
  200. lv.pszText=temp;
  201. m_List.SetItem(&lv);
  202. lv.iSubItem=2;
  203. lstrcpy(temp,msg.aDepartment.GetAt(k));
  204. lv.pszText=temp;
  205. m_List.SetItem(&lv);
  206. }
  207. }
  208. }
  209. if(pData!=NULL)
  210. delete pData;
  211. return 1;
  212. }
  213. void CSearchDlg::OnOK() 
  214. {
  215. UpdateData();
  216. CString strID,strName;
  217. DWORD uID=0;
  218. if(m_RID==0)
  219. {
  220. if(m_ID.GetWindowTextLength()==0)
  221. {
  222. m_ID.SetFocus();
  223. return;
  224. }
  225. m_ID.GetWindowText(strID);
  226. uID=atoi(strID);
  227. if(uID>0)
  228. {
  229. CMsg1 msg;
  230. msg.index=FIND_FRIEND_BY_ID;
  231. msg.MyId=GetApp()->m_uCurrentUserID;
  232. msg.FriendId=uID;
  233. msg.tarIP=GetApp()->m_uServerIP;
  234. msg.nPort=GetApp()->m_uServerPort;
  235. bCancel=FALSE;
  236. if(GetApp()->m_Socket.SendData(&msg,bCancel,this))
  237. {
  238. bStartSearch=TRUE;
  239. BeginWaitCursor();
  240. SetTimer(1,TimeWaitForSearch,NULL);
  241. }
  242. else
  243. {
  244. CString str;
  245. str.LoadString(IDS_SERVER_NOT_RES);
  246. MessageBox(str);
  247. return;
  248. }
  249. }
  250. else
  251. {
  252. MessageBox("ID填写错误!");
  253. m_ID.SetWindowText("");
  254. m_ID.SetFocus();
  255. return;
  256. }
  257. }
  258. else
  259. {
  260. m_Name.GetWindowText(strName);
  261. if(strName.IsEmpty())
  262. {
  263. m_Name.SetFocus();
  264. return;
  265. }
  266. CMsg4 msg;
  267. msg.index=FIND_FRIEND_BY_NAME;
  268. msg.MyId=GetApp()->m_uCurrentUserID;
  269. msg.Msg=strName;
  270. msg.tarIP=GetApp()->m_uServerIP;
  271. msg.nPort=GetApp()->m_uServerPort;
  272. bCancel=FALSE;
  273. if(GetApp()->m_Socket.SendData(&msg,bCancel,this))
  274. {
  275. bStartSearch=TRUE;
  276. BeginWaitCursor();
  277. SetTimer(1,TimeWaitForSearch,NULL);
  278. }
  279. else
  280. {
  281. CString str;
  282. str.LoadString(IDS_SERVER_NOT_RES);
  283. MessageBox(str);
  284. return;
  285. }
  286. }
  287. }
  288. void CSearchDlg::OnClose() 
  289. {
  290. OnCancel();
  291. }
  292. void CSearchDlg::OnCancel() 
  293. {
  294. bCancel=TRUE;
  295. m_List.DeleteAllItems();
  296. m_ID.EnableWindow();
  297. m_Name.EnableWindow(FALSE);
  298. m_ID.SetWindowText("");
  299. m_Name.SetWindowText("");
  300. ((CButton*)GetDlgItem(IDC_RADIO_ID))->SetCheck(1);
  301. CDialog::OnCancel();
  302. }