ChaosUI.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:9k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include "stdafx.h"
- #include "ChaosUI.h"
- #include "LocalServer.h"
- extern volatile HINSTANCE g_hInstance;
- extern volatile HWND g_hMainWnd;
- extern TCHAR g_sAppPath[MAX_PATH];
- extern CLocalServer *g_pLocalServer;
- CChaosUI* CChaosUI::m_pChaosUI = NULL;
- //UINT CChaosUI::m_uMMTimer = 0;
- //CCritSec CChaosUI::m_MMTimerCritSec;
- // { Implementation of CChaosUI
- CChaosUI::CChaosUI()
- : m_bTrayIconExist(FALSE), m_bBalloonShowing(FALSE), m_pblHelp(NULL), m_bthreadstop(FALSE), m_hmsgthread(NULL)
- {
- oldptIcon.x = 0;
- oldptIcon.y = 0;
- m_pblHelp = new CBalloonHelp();
- }
- CChaosUI::~CChaosUI()
- {
- delete m_pblHelp;
- }
- BOOL CChaosUI::GetMessage(CMsgMgr* pMsgMgr, CString& str, BOOL& bForceShow, BOOL& bForceHide, BOOL& bAnimeIcon)
- {
- if(pMsgMgr->GetMsgNum() == 0)
- {
- return FALSE;
- }
- MSGID *pChlMsgIDArray = new MSGID[pMsgMgr->GetMsgNum()];
- pMsgMgr->GetMsgIDArray(pChlMsgIDArray);
- BOOL msgOK = FALSE;
- for(UINT i=0; i<pMsgMgr->GetMsgNum(); i++)
- {
- CString tempstr;
- UINT flags;
- MSGID MsgID = *(pChlMsgIDArray+i);
- if(pMsgMgr->DispatchMessage(MsgID, tempstr, CMsgMgr::GMF_NOPP, &flags))
- {
- msgOK = TRUE;
- str += _T(" ");
- str += tempstr;
- str += _T("n");
- bForceShow = ((flags & CMsgMgr::FORCESHOW) == CMsgMgr::FORCESHOW);
- bForceHide = ((flags & CMsgMgr::FORCEHIDE) == CMsgMgr::FORCEHIDE);
- bAnimeIcon = ((flags & CMsgMgr::WAIT) == CMsgMgr::WAIT);
- }
- }
- delete[] pChlMsgIDArray;
- return msgOK;
- }
- #define UPDATETIMEINTERVAL 300
- //DWORD WINAPI CChaosUI::_MsgProc(LPVOID param)
- unsigned int __stdcall CChaosUI::_MsgProc(void* param)
- {
- CChaosUI *pChaosUI = (CChaosUI *) param;
- assert(pChaosUI);
- if(pChaosUI)
- {
- while(!pChaosUI->m_bthreadstop)
- {
- CString strMessage = _TEXT("");
- BOOL bForceShow = FALSE;
- BOOL bForceHide = FALSE;
- BOOL bdoAnime = FALSE;
- BOOL bGetAnyMsg = FALSE;
- CString tmpMsg;
- BOOL tmpfcs = FALSE;
- BOOL tmpfch = FALSE;
- BOOL tmpdan = FALSE;
- if(pChaosUI->GetMessage(g_pLocalServer->GetMsgMgr(), tmpMsg, tmpfcs, tmpfch, tmpdan))
- {
- bForceShow |= tmpfcs;
- bForceHide |= tmpfch;
- bdoAnime |= tmpdan;
- bGetAnyMsg = TRUE;
- strMessage += tmpMsg;
- }
- UINT uChannelNum = g_pLocalServer->GetChannelNum();
- if(uChannelNum > 0)
- {
- bGetAnyMsg = TRUE;
- CChannelMgr** pChannelMgrArray = new CChannelMgr*[uChannelNum];
- g_pLocalServer->GetChannelMgrArray(pChannelMgrArray);
- for(UINT i=0; i<uChannelNum; i++)
- {
- tmpMsg.Empty();
- pChannelMgrArray[i]->GetChannelName(tmpMsg);
- strMessage += tmpMsg;
- strMessage += _T("n");
- tmpMsg.Empty();
- if(pChaosUI->GetMessage(pChannelMgrArray[i]->GetMsgMgr(), tmpMsg, tmpfcs, tmpfch, tmpdan))
- {
- strMessage += tmpMsg;
- bForceShow |= tmpfcs;
- bForceHide |= tmpfch;
- bdoAnime |= tmpdan;
- }
- }
- delete[] pChannelMgrArray;
- }
- bForceHide = (!bGetAnyMsg) | bForceHide;
- pChaosUI->ShowMessage(strMessage, bForceShow, bForceHide, bdoAnime);
- Sleep(UPDATETIMEINTERVAL);
- }
- }
- PostMessage(g_hMainWnd, WM_SENDTOLOCALSERVER, CASE_THREAD_EXIT, 0);
- return 0;
- }
- CChaosUI* CChaosUI::CreateInstance(CLocalServer *pLocalServer)
- {
- if(m_pChaosUI == NULL)
- {
- m_pChaosUI = new CChaosUI();
- m_pChaosUI->Init(pLocalServer);
- if(m_pChaosUI->InitTrayIconData())
- {
- CString title;
- title.LoadString(g_hInstance, IDS_BALLOONTITLE);
- /* mystring filestr(g_sAppPath);
- filestr.append(_T("\config.ini"));
- ConfigFile config(filestr);
- mystring tmp = config.Value(_T("localserver"), _T("title"));
- CString title;
- if(config.fileNotFound || config.stringNotFound)
- title = _T("www.mysee.com");
- else
- title = tmp.c_str();*/
- m_pChaosUI->ShowTrayIcon(TRUE, TRUE);
- m_pChaosUI->m_pblHelp->Create(_T(""), _T(""), CPoint(0, 0),
- CBalloonHelp::unSHOW_CLOSE_BUTTON|CBalloonHelp::unSHOW_TOPMOST|CBalloonHelp::unHIDE_AFTER_CREATED
- , g_hMainWnd);
- m_pChaosUI->m_pblHelp->SetIcon(m_pChaosUI->m_IconMysee);
- m_pChaosUI->m_pblHelp->SetTitle(title);
- UINT threadID;
- uintptr_t ret = _beginthreadex(NULL, 0, &_MsgProc, m_pChaosUI, 0, &threadID);
- if(ret != -1L)
- {
- g_pLocalServer->AddWndThreadRef();
- }
- }
- }
- return m_pChaosUI;
- }
- void CChaosUI::DeleteInstance()
- {
- m_pChaosUI->m_bthreadstop = TRUE;
- if(m_pChaosUI->m_pblHelp->IsWindow())
- {
- m_pChaosUI->m_pblHelp->DestroyWindow();
- }
- m_pChaosUI->ShowTrayIcon(FALSE, FALSE);
- delete m_pChaosUI;
- m_pChaosUI = NULL;
- }
- void CChaosUI::DeliverExit()
- {
- m_pChaosUI->m_bthreadstop = TRUE;
- }
- void CChaosUI::ShowBalloon()
- {
- if(g_pLocalServer->GetChannelNum() == 0)
- g_pLocalServer->UpdateNoChannelMsg();
- m_pblHelp->ShowBalloon();
- }
- namespace {
- UINT m_counter = 0;
- }
- void CChaosUI::ShowMessage(const CString &strMessage, BOOL bForceShow, BOOL bForceHide, BOOL doAnime)
- {
- m_counter++;
- // CAutoLock AutoLock(&m_CritSec);
- if(m_counter%4 == 0) //大约1.2s才重测试一次图标位置,控制开销
- {
- POINT ptIcon;
- BOOL bIconFound = m_TrayIconPositon.GetTrayIconPosition(ptIcon,
- CTrayIconPosition::UseBothTechniquesDirectPrefered);
- if(bIconFound == -1)
- ptIcon = oldptIcon; //取上一次得到的图标位置
- else
- oldptIcon = ptIcon;
- m_pblHelp->SetAnchorPoint(ptIcon);
- m_TrayIconPositon.RestoreTrayIcon(this->m_TrayIconData.hIcon);
- }
- ShowTrayIcon(TRUE, doAnime);
- m_pblHelp->SetContent(strMessage);
- if(bForceShow) {
- /* m_TrayIconData.uFlags = NIF_INFO;
- m_TrayIconData.dwInfoFlags = NIIF_INFO;;
- m_TrayIconData.uTimeout = 100000;
- _tcscpy(m_TrayIconData.szInfoTitle, _T("title"));
- _tcscpy(m_TrayIconData.szInfo, (LPCTSTR)strMessage);
- Shell_NotifyIcon(NIM_MODIFY, &m_TrayIconData);
- m_bBalloonShowing*/
- m_pblHelp->ShowBalloon();
- }
- else if(bForceHide)
- m_pblHelp->HideBalloonImmediately();
- }
- BOOL CChaosUI::InitTrayIconData()
- {
- m_IconAnime[0] = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYANIME1));
- m_IconAnime[1] = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYANIME2));
- m_IconAnime[2] = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYANIME3));
- m_IconAnime[3] = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYANIME4));
- m_IconAnime[4] = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYANIME5));
- m_IconAnime[5] = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYANIME6));
- // m_IconMysee = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_TRAYICON));
- m_IconMysee = (HICON) ::LoadImage(g_hInstance, MAKEINTRESOURCE(IDI_TRAYICON), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR |LR_SHARED);
- if(NULL == m_IconMysee)
- {
- assert(0);
- return FALSE;
- }
- m_TrayIconData.cbSize = sizeof(m_TrayIconData);
- m_TrayIconData.hWnd = g_hMainWnd;
- m_TrayIconData.uCallbackMessage = WM_TASKBARMSG;
- m_TrayIconData.uID = 1;
- m_TrayIconData.uFlags = NIF_MESSAGE | NIF_ICON ;
- _tcscpy(m_TrayIconData.szTip, _T("www.OPenmysee.com"));
- m_TrayIconData.hIcon = m_IconMysee;
- return TRUE;
- }
- void CChaosUI::ShowTrayIcon(BOOL bShow, BOOL bDoAnime)
- {
- if(bShow)
- {
- if(m_bTrayIconExist)
- {
- if(bDoAnime)
- {
- m_TrayIconData.hIcon = m_IconAnime[m_counter%6];
- Shell_NotifyIcon(NIM_MODIFY, &m_TrayIconData);
- }
- else
- {
- if(m_TrayIconData.hIcon != m_IconMysee)
- {
- m_TrayIconData.hIcon = m_IconMysee;
- Shell_NotifyIcon(NIM_MODIFY, &m_TrayIconData);
- }
- }
- }
- else
- {
- Shell_NotifyIcon(NIM_ADD, &m_TrayIconData);
- m_TrayIconData.uFlags = NIF_ICON ;
- m_bTrayIconExist = TRUE;
- m_TrayIconPositon.InitializePositionTracking(m_TrayIconData.hWnd, m_TrayIconData.uID);
- }
- }
- else
- {
- Shell_NotifyIcon(NIM_DELETE, &m_TrayIconData);
- m_bTrayIconExist = FALSE;
- }
- }
- // Implementation of CChaosUI }