ChatControls.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:10k
- #include "stdAfx.h"
- #include "trfAgent.h"
- #include "ChatControls.h"
- IMPLEMENT_DYNCREATE(CChatRichEditCtrl, CRichEditCtrl)
- CChatRichEditCtrl::CChatRichEditCtrl()
- {
- m_hlogfile = INVALID_HANDLE_VALUE;
-
- m_guifont.CreateFont(
- 12, // nHeight
- 0, // nWidth
- 0, // nEscapement
- 0, // nOrientation
- FW_NORMAL, // nWeight
- FALSE, // bItalic
- FALSE, // bUnderline
- 0, // cStrikeOut
- ANSI_CHARSET, // nCharSet
- OUT_DEFAULT_PRECIS, // nOutPrecision
- CLIP_DEFAULT_PRECIS, // nClipPrecision
- DEFAULT_QUALITY, // nQuality
- DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
- _T("宋体")); // lpszFacename
- }
- CChatRichEditCtrl::~CChatRichEditCtrl()
- {
- m_guifont.DeleteObject();
- if(INVALID_HANDLE_VALUE != m_hlogfile)
- {
- ::CloseHandle(m_hlogfile);
- m_hlogfile = INVALID_HANDLE_VALUE;
- }
- }
- BEGIN_MESSAGE_MAP(CChatRichEditCtrl, CRichEditCtrl)
- //{{AFX_MSG_MAP(CChatRichEditCtrl)
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- int CChatRichEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if(CRichEditCtrl::OnCreate(lpCreateStruct) == -1) return -1;
- this->SetFont(&m_guifont);
- return 0;
- }
- BOOL CChatRichEditCtrl::Setlogfile(LPCSTR lpszLogfile)
- {
- if(INVALID_HANDLE_VALUE != m_hlogfile)
- {
- ::CloseHandle(m_hlogfile);
- m_hlogfile = INVALID_HANDLE_VALUE;
- }
- if(FileExists(lpszLogfile))
- {
- m_hlogfile = ::CreateFile(lpszLogfile, GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- }
- else
- {
- m_hlogfile = ::CreateFile(lpszLogfile, GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- }
- return (INVALID_HANDLE_VALUE != m_hlogfile);
- }
- void CChatRichEditCtrl::WriteLog(LPCSTR lpszloginfo, UINT uncount)
- {
- if(m_hlogfile == INVALID_HANDLE_VALUE) return ;
- DWORD dwriten = 0x0000;
- ::SetFilePointer(m_hlogfile, 0, NULL, FILE_END);
- ::WriteFile(m_hlogfile, lpszloginfo, uncount, &dwriten, NULL);
- }
- void CChatRichEditCtrl::AppendText(LPCSTR lpszToOrFrm, LPCSTR lpszUserName, LPCSTR lpszContents)
- {
- CString strhead, strtime;
- CTime now = CTime::GetCurrentTime();
-
- strtime.Format(SYS_TIMEFORMAT, now.GetYear(), now.GetMonth(), now.GetDay(),
- now.GetHour(), now.GetMinute(), now.GetSecond());
- strhead.Format("%s > %s %s : ", strtime.GetBuffer(0), lpszToOrFrm, lpszUserName);
-
- //1
- long ltotalen = this->GetTextLength();
- this->HideSelection(TRUE, TRUE);
- this->SetSel(ltotalen, ltotalen);
- this->ReplaceSel(strhead.GetBuffer(0));
- //2
- CString strmsg;
- strmsg = lpszContents;
- strmsg += "n";
- ltotalen = this->GetTextLength();
- this->SetSel(ltotalen, ltotalen);
- this->ReplaceSel(strmsg.GetBuffer(0));
- //3
- ltotalen = this->GetTextLength();
- this->SetSel(ltotalen, ltotalen);
- this->HideSelection(FALSE, TRUE);
- this->SendMessage(EM_SCROLLCARET, 0, 0);
- //add log information.
- CString strloginfo = strhead + strmsg;
- this->WriteLog(strloginfo.GetBuffer(0), strloginfo.GetLength());
- }
- DWORD CChatRichEditCtrl::StreamOutCallback(DWORD dwCookie,
- LPBYTE pbBuff,
- LONG cb, LONG *pcb)
- {
- CFile* pFile = (CFile*)dwCookie;
-
- pFile->Write(pbBuff, cb);
- *pcb = cb;
-
- return 0;
- }
- BOOL CChatRichEditCtrl::SaveToFile(LPCSTR lpszFilename, const BOOL bOverwrite)
- {
- CFile pFile;
- CFileFind ffFinder;
- if(this->GetTextLength() == 0) return FALSE;
- if(ffFinder.FindFile(lpszFilename) !=0)
- {
- if(!bOverwrite)
- {
- ffFinder.Close();
- return FALSE;
- }
- }
- ffFinder.Close();
- if(!pFile.Open(lpszFilename, CFile::modeWrite | CFile::modeCreate)) return FALSE;
-
- EDITSTREAM es;
-
- es.pfnCallback = StreamOutCallback;
- es.dwCookie = (DWORD)&pFile;
- this->SendMessage(EM_STREAMOUT, (WPARAM)SF_TEXT, (LPARAM)&es);
- pFile.Close();
- return TRUE;
- }
- //////////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNCREATE(CChatInputEdit, CEdit)
- CChatInputEdit::CChatInputEdit()
- {
- // Initializes a CFont object with the specified characteristics.
-
- m_guifont.CreateFont(
- 12, // nHeight
- 0, // nWidth
- 0, // nEscapement
- 0, // nOrientation
- FW_NORMAL, // nWeight
- FALSE, // bItalic
- FALSE, // bUnderline
- 0, // cStrikeOut
- ANSI_CHARSET, // nCharSet
- OUT_DEFAULT_PRECIS, // nOutPrecision
- CLIP_DEFAULT_PRECIS, // nClipPrecision
- DEFAULT_QUALITY, // nQuality
- DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
- _T("宋体")); // lpszFacename
- }
- CChatInputEdit::~CChatInputEdit()
- {
- m_guifont.DeleteObject();
- }
- BEGIN_MESSAGE_MAP(CChatInputEdit, CEdit)
-
- //{{AFX_MSG_MAP(CChatInputEdit)
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- int CChatInputEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if(CEdit::OnCreate(lpCreateStruct) == -1)
- {
- TRACE0("Failed to create CChatInputEdit window. n");
- return -1;
- }
- this->SetFont(&m_guifont);
- return 0;
- }
- BOOL CChatInputEdit::PreTranslateMessage(MSG* pMsg)
- {
- if(pMsg->hwnd == this->GetSafeHwnd())
- {
- SHORT keystae = ::GetKeyState(VK_CONTROL);
- BOOL bctrldown = ((keystae & 0x80000000) != 0);
-
- if(pMsg->message == WM_KEYDOWN)
- {
- if(bctrldown)
- {
- switch(pMsg->wParam)
- {
- case 'C':
- this->Copy();
- break;
-
- case 'X':
- this->Cut();
- break;
-
- case 'V':
- this->Paste();
- break;
- }
- }
- else
- {
- if(pMsg->wParam == VK_RETURN)
- {
- CWnd *parWnd = this->GetParent();
- ASSERT(NULL != parWnd);
- parWnd->SendMessage(UWM_CHARINPUTOVER, 0, 0);
- return TRUE;
- }
- }
- }
- }
- return CEdit::PreTranslateMessage(pMsg);
- }
- //////////////////////////////////////////////////////////////////////////
- CChatGfxOutBarCtrl::CChatGfxOutBarCtrl()
- {
- this->m_nImageManCount = 0;
- this->m_nImageWomanCount = 0;
- }
- CChatGfxOutBarCtrl::~CChatGfxOutBarCtrl()
- {
- this->m_imaSmall.DeleteImageList();
- this->m_imaSmall.Detach();
- this->m_imaLarge.DeleteImageList();
- this->m_imaLarge.Detach();
- }
- BEGIN_MESSAGE_MAP(CChatGfxOutBarCtrl, CGfxOutBarCtrl)
- //{{AFX_MSG_MAP(CChatGfxOutBarCtrl)
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- int CChatGfxOutBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if(CGfxOutBarCtrl::OnCreate(lpCreateStruct) == -1)
- {
- TRACE0("Failed to create GfxOutBarCtrl window. n");
- return -1;
- }
- //////////////////////////////////////////////////////////////////////////
-
- CBitmap imglstbmp;
-
- //load large image list.
- //make "MAN" and "WOMAN" large imagelist.
- if(this->m_imaLarge.Create(32, 32, ILC_COLOR8 | TRUE, 80, 1))
- {
- imglstbmp.LoadBitmap(IDB_MAN32_HOT);
- this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
-
- imglstbmp.LoadBitmap(IDB_MAN32_DISABLE);
- this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
- if(this->m_nImageManCount == 0)
- this->m_nImageManCount = this->m_imaLarge.GetImageCount() / 2;
- imglstbmp.LoadBitmap(IDB_WOMAN32_DISABLE);
- this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
- imglstbmp.LoadBitmap(IDB_WOMAN32_DISABLE);
- this->m_imaLarge.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
- if(this->m_nImageWomanCount == 0)
- this->m_nImageWomanCount = (this->m_imaLarge.GetImageCount() -
- (2 * m_nImageManCount)) / 2;
- }
- //make "MAN" and "WOMAN" small imagelist.
- if(this->m_imaSmall.Create(16, 16, ILC_COLOR8 | TRUE, 40, 1))
- {
- imglstbmp.LoadBitmap(IDB_MAN16_HOT);
- this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
-
- imglstbmp.LoadBitmap(IDB_MAN16_DISABLE);
- this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
- if(this->m_nImageManCount == 0)
- this->m_nImageManCount = this->m_imaSmall.GetImageCount() / 2;
-
- imglstbmp.LoadBitmap(IDB_WOMAN16_DISABLE);
- this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
-
- imglstbmp.LoadBitmap(IDB_WOMAN16_DISABLE);
- this->m_imaSmall.Add(&imglstbmp, RGB(0, 128, 128));
- imglstbmp.Detach();
- if(this->m_nImageWomanCount == 0)
- this->m_nImageWomanCount = (this->m_imaSmall.GetImageCount() -
- (2 * m_nImageManCount)) / 2;
- }
- this->SetImageList(&m_imaLarge, CGfxOutBarCtrl::fLargeIcon);
- this->SetImageList(&m_imaSmall, CGfxOutBarCtrl::fSmallIcon);
-
- this->SetAnimationTickCount(10);
-
- this->SetAnimSelHighlight(200);
-
- this->SetIfQueryRemove(true);
- this->AddFolder(_LoadString(IDS_CHATFRIENDSDIR).GetBuffer(0), 0);
- this->AddFolder(_LoadString(IDS_CHATGRPFRIENDSDIR).GetBuffer(0), 0);
- return 0;
- }
- BOOL CChatGfxOutBarCtrl::ExistsAgent(const int nFolderIndex, const int nAgentId, int &nItemIndex)
- {
- if(nFolderIndex < 0 || nFolderIndex > this->GetFolderCount() - 1) return FALSE;
- for(int i = 0; i < this->GetFolderItemCount(nFolderIndex); ++i)
- {
- DWORD dwdata = this->GetItemData(i);
- if(HIWORD(dwdata) == nAgentId)
- {
- nItemIndex = i;
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CChatGfxOutBarCtrl::GetImageIndex(WORD wHeadportraitIndex, int &nNormalImageIndex, int &nDisableImageIndex)
- {
- switch(HIBYTE(wHeadportraitIndex))
- {
- case 0:
- {
- nNormalImageIndex = LOBYTE(wHeadportraitIndex);
- nDisableImageIndex= nNormalImageIndex + this->m_nImageManCount;
- }
- break;
- case 1:
- {
- nNormalImageIndex = (2 * this->m_nImageManCount) + LOBYTE(wHeadportraitIndex);
- nDisableImageIndex= nNormalImageIndex + this->m_nImageWomanCount;
- }
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }