ChatControls.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:10k
源码类别:

网格计算

开发平台:

Visual C++

  1. #include "stdAfx.h"
  2. #include "trfAgent.h"
  3. #include "ChatControls.h"
  4. IMPLEMENT_DYNCREATE(CChatRichEditCtrl, CRichEditCtrl)
  5. CChatRichEditCtrl::CChatRichEditCtrl()
  6. {
  7. m_hlogfile = INVALID_HANDLE_VALUE;
  8. m_guifont.CreateFont(
  9. 12,                        // nHeight
  10. 0,                         // nWidth
  11. 0,                         // nEscapement
  12. 0,                         // nOrientation
  13. FW_NORMAL,                 // nWeight
  14. FALSE,                     // bItalic
  15. FALSE,                     // bUnderline
  16. 0,                         // cStrikeOut
  17. ANSI_CHARSET,              // nCharSet
  18. OUT_DEFAULT_PRECIS,        // nOutPrecision
  19. CLIP_DEFAULT_PRECIS,       // nClipPrecision
  20. DEFAULT_QUALITY,           // nQuality
  21. DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
  22. _T("宋体"));              // lpszFacename
  23. }
  24. CChatRichEditCtrl::~CChatRichEditCtrl()
  25. {
  26. m_guifont.DeleteObject();
  27. if(INVALID_HANDLE_VALUE != m_hlogfile)
  28. {
  29. ::CloseHandle(m_hlogfile);
  30. m_hlogfile = INVALID_HANDLE_VALUE;
  31. }
  32. }
  33. BEGIN_MESSAGE_MAP(CChatRichEditCtrl, CRichEditCtrl)
  34. //{{AFX_MSG_MAP(CChatRichEditCtrl)
  35. ON_WM_CREATE()
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. int CChatRichEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  39. {
  40. if(CRichEditCtrl::OnCreate(lpCreateStruct) == -1) return -1;
  41. this->SetFont(&m_guifont);
  42. return 0;
  43. }
  44. BOOL CChatRichEditCtrl::Setlogfile(LPCSTR lpszLogfile)
  45. {
  46. if(INVALID_HANDLE_VALUE != m_hlogfile)
  47. {
  48. ::CloseHandle(m_hlogfile);
  49. m_hlogfile = INVALID_HANDLE_VALUE;
  50. }
  51. if(FileExists(lpszLogfile))
  52. {
  53. m_hlogfile = ::CreateFile(lpszLogfile, GENERIC_READ | GENERIC_WRITE, 
  54. FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  55. }
  56. else
  57. {
  58. m_hlogfile = ::CreateFile(lpszLogfile, GENERIC_READ | GENERIC_WRITE, 
  59. FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  60. }
  61. return (INVALID_HANDLE_VALUE != m_hlogfile);
  62. }
  63. void CChatRichEditCtrl::WriteLog(LPCSTR lpszloginfo, UINT uncount)
  64. {
  65. if(m_hlogfile == INVALID_HANDLE_VALUE) return ;
  66. DWORD dwriten = 0x0000;
  67. ::SetFilePointer(m_hlogfile, 0, NULL, FILE_END);
  68. ::WriteFile(m_hlogfile, lpszloginfo, uncount, &dwriten, NULL);
  69. }
  70. void CChatRichEditCtrl::AppendText(LPCSTR lpszToOrFrm, LPCSTR lpszUserName, LPCSTR lpszContents)
  71. {
  72. CString strhead, strtime;
  73. CTime now = CTime::GetCurrentTime();
  74. strtime.Format(SYS_TIMEFORMAT, now.GetYear(), now.GetMonth(), now.GetDay(),
  75. now.GetHour(), now.GetMinute(), now.GetSecond());
  76. strhead.Format("%s > %s %s : ", strtime.GetBuffer(0), lpszToOrFrm, lpszUserName);
  77. //1
  78. long ltotalen = this->GetTextLength();
  79. this->HideSelection(TRUE, TRUE);
  80. this->SetSel(ltotalen, ltotalen);
  81. this->ReplaceSel(strhead.GetBuffer(0));
  82. //2
  83. CString strmsg;
  84. strmsg = lpszContents;
  85. strmsg += "n";
  86. ltotalen = this->GetTextLength();
  87. this->SetSel(ltotalen, ltotalen);
  88. this->ReplaceSel(strmsg.GetBuffer(0));
  89. //3
  90. ltotalen = this->GetTextLength();
  91. this->SetSel(ltotalen, ltotalen);
  92. this->HideSelection(FALSE, TRUE);
  93. this->SendMessage(EM_SCROLLCARET, 0, 0);
  94. //add log information.
  95. CString strloginfo = strhead + strmsg;
  96. this->WriteLog(strloginfo.GetBuffer(0), strloginfo.GetLength());
  97. }
  98. DWORD CChatRichEditCtrl::StreamOutCallback(DWORD dwCookie, 
  99.    LPBYTE pbBuff, 
  100.    LONG cb, LONG *pcb)
  101. {
  102.    CFile* pFile = (CFile*)dwCookie;
  103.    
  104.    pFile->Write(pbBuff, cb);
  105.    *pcb = cb;
  106.    
  107.    return 0;
  108. }
  109. BOOL CChatRichEditCtrl::SaveToFile(LPCSTR lpszFilename, const BOOL bOverwrite)
  110. {
  111. CFile pFile;
  112. CFileFind ffFinder;
  113. if(this->GetTextLength() == 0) return FALSE;
  114. if(ffFinder.FindFile(lpszFilename) !=0)
  115. {
  116. if(!bOverwrite)
  117. {
  118. ffFinder.Close();
  119. return FALSE;
  120. }
  121. }
  122. ffFinder.Close();
  123. if(!pFile.Open(lpszFilename, CFile::modeWrite | CFile::modeCreate)) return FALSE;
  124. EDITSTREAM es;
  125. es.pfnCallback = StreamOutCallback;
  126. es.dwCookie = (DWORD)&pFile;
  127. this->SendMessage(EM_STREAMOUT, (WPARAM)SF_TEXT, (LPARAM)&es);
  128. pFile.Close();
  129. return TRUE;
  130. }
  131. //////////////////////////////////////////////////////////////////////////
  132. IMPLEMENT_DYNCREATE(CChatInputEdit, CEdit)
  133. CChatInputEdit::CChatInputEdit()
  134. {
  135. // Initializes a CFont object with the specified characteristics.
  136. m_guifont.CreateFont(
  137. 12,                        // nHeight
  138. 0,                         // nWidth
  139. 0,                         // nEscapement
  140. 0,                         // nOrientation
  141. FW_NORMAL,                 // nWeight
  142. FALSE,                     // bItalic
  143. FALSE,                     // bUnderline
  144. 0,                         // cStrikeOut
  145. ANSI_CHARSET,              // nCharSet
  146. OUT_DEFAULT_PRECIS,        // nOutPrecision
  147. CLIP_DEFAULT_PRECIS,       // nClipPrecision
  148. DEFAULT_QUALITY,           // nQuality
  149. DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
  150. _T("宋体"));              // lpszFacename
  151. }
  152. CChatInputEdit::~CChatInputEdit()
  153. {
  154. m_guifont.DeleteObject();
  155. }
  156. BEGIN_MESSAGE_MAP(CChatInputEdit, CEdit)
  157. //{{AFX_MSG_MAP(CChatInputEdit)
  158. ON_WM_CREATE()
  159. //}}AFX_MSG_MAP
  160. END_MESSAGE_MAP()
  161. int CChatInputEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
  162. {
  163. if(CEdit::OnCreate(lpCreateStruct) == -1)
  164. {
  165. TRACE0("Failed to create CChatInputEdit window. n");
  166. return -1;
  167. }
  168. this->SetFont(&m_guifont);
  169. return 0;
  170. }
  171. BOOL CChatInputEdit::PreTranslateMessage(MSG* pMsg)
  172. {
  173. if(pMsg->hwnd == this->GetSafeHwnd())
  174. {
  175. SHORT keystae = ::GetKeyState(VK_CONTROL);
  176. BOOL  bctrldown = ((keystae & 0x80000000) != 0);
  177. if(pMsg->message == WM_KEYDOWN)
  178. {
  179. if(bctrldown)
  180. {
  181. switch(pMsg->wParam)
  182. {
  183. case 'C':
  184. this->Copy();
  185. break;
  186. case 'X':
  187. this->Cut();
  188. break;
  189. case 'V':
  190. this->Paste();
  191. break;
  192. }
  193. }
  194. else
  195. {
  196. if(pMsg->wParam == VK_RETURN)
  197. {
  198. CWnd *parWnd = this->GetParent();
  199. ASSERT(NULL != parWnd);
  200. parWnd->SendMessage(UWM_CHARINPUTOVER, 0, 0);
  201. return TRUE;
  202. }
  203. }
  204. }
  205. }
  206. return CEdit::PreTranslateMessage(pMsg);
  207. }
  208. //////////////////////////////////////////////////////////////////////////
  209. CChatGfxOutBarCtrl::CChatGfxOutBarCtrl()
  210. {
  211. this->m_nImageManCount = 0;
  212. this->m_nImageWomanCount = 0;
  213. }
  214. CChatGfxOutBarCtrl::~CChatGfxOutBarCtrl()
  215. {
  216. this->m_imaSmall.DeleteImageList();
  217. this->m_imaSmall.Detach();
  218. this->m_imaLarge.DeleteImageList();
  219. this->m_imaLarge.Detach();
  220. }
  221. BEGIN_MESSAGE_MAP(CChatGfxOutBarCtrl, CGfxOutBarCtrl)
  222. //{{AFX_MSG_MAP(CChatGfxOutBarCtrl)
  223. ON_WM_CREATE()
  224. //}}AFX_MSG_MAP
  225. END_MESSAGE_MAP()
  226. int CChatGfxOutBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  227. {
  228. if(CGfxOutBarCtrl::OnCreate(lpCreateStruct) == -1)
  229. {
  230. TRACE0("Failed to create GfxOutBarCtrl window. n");
  231. return -1;
  232. }
  233. //////////////////////////////////////////////////////////////////////////
  234. CBitmap imglstbmp;
  235. //load large image list.
  236. //make "MAN" and "WOMAN" large imagelist.
  237. if(this->m_imaLarge.Create(32, 32, ILC_COLOR8 | TRUE, 80, 1))
  238. {
  239. imglstbmp.LoadBitmap(IDB_MAN32_HOT);
  240. this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
  241. imglstbmp.Detach();
  242. imglstbmp.LoadBitmap(IDB_MAN32_DISABLE);
  243. this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
  244. imglstbmp.Detach();
  245. if(this->m_nImageManCount == 0)
  246. this->m_nImageManCount = this->m_imaLarge.GetImageCount() / 2;
  247. imglstbmp.LoadBitmap(IDB_WOMAN32_DISABLE);
  248. this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
  249. imglstbmp.Detach();
  250. imglstbmp.LoadBitmap(IDB_WOMAN32_DISABLE);
  251. this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
  252. imglstbmp.Detach();
  253. if(this->m_nImageWomanCount == 0)
  254. this->m_nImageWomanCount = (this->m_imaLarge.GetImageCount() - 
  255. (2 * m_nImageManCount)) / 2;
  256. }
  257. //make "MAN" and "WOMAN" small imagelist.
  258. if(this->m_imaSmall.Create(16, 16, ILC_COLOR8 | TRUE, 40, 1))
  259. {
  260. imglstbmp.LoadBitmap(IDB_MAN16_HOT);
  261. this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
  262. imglstbmp.Detach();
  263. imglstbmp.LoadBitmap(IDB_MAN16_DISABLE);
  264. this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
  265. imglstbmp.Detach();
  266. if(this->m_nImageManCount == 0)
  267. this->m_nImageManCount = this->m_imaSmall.GetImageCount() / 2;
  268. imglstbmp.LoadBitmap(IDB_WOMAN16_DISABLE);
  269. this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
  270. imglstbmp.Detach();
  271. imglstbmp.LoadBitmap(IDB_WOMAN16_DISABLE);
  272. this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
  273. imglstbmp.Detach();
  274. if(this->m_nImageWomanCount == 0)
  275. this->m_nImageWomanCount = (this->m_imaSmall.GetImageCount() - 
  276. (2 * m_nImageManCount)) / 2;
  277. }
  278. this->SetImageList(&m_imaLarge, CGfxOutBarCtrl::fLargeIcon);
  279. this->SetImageList(&m_imaSmall, CGfxOutBarCtrl::fSmallIcon);
  280. this->SetAnimationTickCount(10);
  281. this->SetAnimSelHighlight(200);
  282. this->SetIfQueryRemove(true);
  283. this->AddFolder(_LoadString(IDS_CHATFRIENDSDIR).GetBuffer(0), 0);
  284. this->AddFolder(_LoadString(IDS_CHATGRPFRIENDSDIR).GetBuffer(0), 0);
  285. return 0;
  286. }
  287. BOOL CChatGfxOutBarCtrl::ExistsAgent(const int nFolderIndex, const int nAgentId, int &nItemIndex)
  288. {
  289. if(nFolderIndex < 0 || nFolderIndex > this->GetFolderCount() - 1) return FALSE;
  290. for(int i = 0; i < this->GetFolderItemCount(nFolderIndex); ++i)
  291. {
  292. DWORD dwdata = this->GetItemData(i);
  293. if(HIWORD(dwdata) == nAgentId)
  294. {
  295. nItemIndex = i;
  296. return TRUE;
  297. }
  298. }
  299. return FALSE;
  300. }
  301. BOOL CChatGfxOutBarCtrl::GetImageIndex(WORD wHeadportraitIndex, int &nNormalImageIndex, int &nDisableImageIndex)
  302. {
  303. switch(HIBYTE(wHeadportraitIndex))
  304. {
  305. case 0:
  306. {
  307. nNormalImageIndex = LOBYTE(wHeadportraitIndex);
  308. nDisableImageIndex= nNormalImageIndex + this->m_nImageManCount;
  309. }
  310. break;
  311. case 1:
  312. {
  313. nNormalImageIndex = (2 * this->m_nImageManCount) + LOBYTE(wHeadportraitIndex);
  314. nDisableImageIndex= nNormalImageIndex + this->m_nImageWomanCount;
  315. }
  316. break;
  317. default:
  318. return FALSE;
  319. }
  320. return TRUE;
  321. }