#include "MainWnd.h" #include CMainWnd::~CMainWnd(void) { try { if(IsOpenWifiByMsg) { EnableMzWIFI(false); ShellPostWlan(); } SetScreenAutoOff(); if(ipmsg) { ipmsg->setMsgProcess(NULL); delete pMsgProcess; delete ipmsg; } } catch(...) { } } BOOL CMainWnd::OnInitDialog() { IsOpenWifiByMsg = false; IsFirstLoad = true; ipmsg = NULL; // bklHandle = 0; // Must all the Init of parent class first! if (!CMzWndEx::OnInitDialog()) { return FALSE; } pConfig = Configuration::GetInstance(); //Init wifi #ifndef DEBUG if(!IsWifiAvailable() ) { MzMessageBoxEx(m_hWnd, L"您的小8没有wifi设备:(" , L"Test", MZ_OK); PostQuitMessage(0); return FALSE; } #endif if( ! IsLoadWifi() ) { if( MzMessageBoxEx(m_hWnd, L"是否开启wifi?" , L"Test", MZ_YESNO) == 1) { if(EnableMzWIFI(TRUE)) { IsOpenWifiByMsg = true; ShellPostWlan(); } else { MzMessageBoxEx(m_hWnd, L"开启wifi失败:(" , L"Test", MZ_OK); PostQuitMessage(0); return FALSE; } } else { MzMessageBoxEx(m_hWnd, L"Bye" , L"Test", MZ_OK); PostQuitMessage(0); return FALSE; } } // Init the toolbar and add it into the window m_Toolbar.SetPos(0,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR,GetWidth(),MZM_HEIGHT_TEXT_TOOLBAR); m_Toolbar.SetButton(0, true, true, L"退出"); m_Toolbar.SetButton(1, true, true, L"配置"); m_Toolbar.SetButton(2, true, true, L"刷新"); m_Toolbar.SetID(MZ_IDC_TOOLBAR1); AddUiWin(&m_Toolbar); m_ScrollWin.SetPos(0,0,GetWidth(),GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR); m_ScrollWin.SetID(MZ_IDC_SCROLLWIN); m_ScrollWin.EnableScrollBarV(true); AddUiWin(&m_ScrollWin); m_List.SetPos(0,0,GetWidth(),70); m_List.SetID(MZ_IDC_LIST); m_List.EnableNotifyMessage(true); // add the list to the scrollable container m_ScrollWin.AddChild(&m_List); pProcessDialog = NULL; SetScreenAlwaysOn(m_hWnd); this->PostMessageW(WM_BEGIN_NETWORK, NULL, NULL); return TRUE; } void CMainWnd::OnMzCommand(WPARAM wParam, LPARAM lParam) { UINT_PTR id = LOWORD(wParam); switch(id) { case MZ_IDC_TOOLBAR1: { int nIndex = lParam; if (nIndex==0) { //退出 //SetCursor(LoadCursor(NULL, IDC_WAIT)); MzBeginWaitDlg (this->m_hWnd); if(ipmsg) ipmsg->exit(); //MzMessageBoxEx(m_hWnd, L"Bye!", L"Test", MB_OK); PostQuitMessage(0); return; } else if(nIndex == 1) { //配置 CConfigWnd configWnd; RECT rcWork = MzGetWorkArea(); configWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0); configWnd.SetBgColor(MzGetThemeColor(TCI_WINDOW_BG)); configWnd.SetAnimateType_Show(MZ_ANIMTYPE_SCROLL_BOTTOM_TO_TOP_2); configWnd.SetAnimateType_Hide(MZ_ANIMTYPE_SCROLL_BOTTOM_TO_TOP_2); if(configWnd.DoModal() == ID_OK) Refresh(); } else if (nIndex==2) { Refresh(); return; } } break; } } LRESULT CMainWnd::MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_BEGIN_NETWORK: MzBeginWaitDlg (this->m_hWnd, 0, TRUE); if(IsOpenWifiByMsg) Sleep(6500); ipmsg = new IPMsg(); pMsgProcess = new M8MsgProcess(this,ipmsg); ipmsg->setMsgProcess(pMsgProcess); ipmsg->entry(); MzEndWaitDlg(); return NULL; case WM_UPDATELIST: UpdateList(*(NameHostMap*)wParam); return NULL; case WM_SHOWMSG: { MsgBuf* pMsg = (MsgBuf*)wParam; ShowMsg(pMsg); delete pMsg; return NULL; } case WM_RECVFILE: { wstring path; GetCurrentDirectory(path); path.append(L"success.wav"); int fileCnt = (int)wParam; if(fileCnt > 0) { if(pConfig->IsPlaySound()) PlaySound(path.c_str(),NULL, SND_FILENAME | SND_ASYNC); TCHAR txt[50]; wsprintf(txt, L"收到%d个文件!", fileCnt); MzAutoMsgBoxEx(m_hWnd, txt); } return NULL; } case MZ_WM_MOUSE_NOTIFY: { int nID = LOWORD(wParam); int nNotify = HIWORD(wParam); int x = LOWORD(lParam); int y = HIWORD(lParam); // process the mouse left button down notification if (nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONDOWN) { if (!m_List.IsMouseDownAtScrolling() && !m_List.IsMouseMoved()) { int nIndex = m_List.CalcIndexOfPos(x, y); m_List.SetSelectedIndex(nIndex); m_List.Invalidate(); m_List.Update(); ListItem* item = m_List.GetItem(nIndex); if(pConfig->GetShowLocal() == SHOW_LOCAL_SHOW) { vector localhost; GetLocalhost(localhost); bool showPopup = true; for(int i = 0; i < localhost.size(); i++) { if(item->Text.Compare(localhost[i].c_str()) == 0) { showPopup = false; break; } } if(showPopup) { PopupSendMenu((HostSub*)item->Data); } } else { PopupSendMenu((HostSub*)item->Data); } } return NULL; } // process the mouse move notification if (nID==MZ_IDC_LIST && nNotify==MZ_MN_MOUSEMOVE) { m_List.SetSelectedIndex(-1); m_List.Invalidate(); m_List.Update(); return NULL; } } case WM_BEGIN_PROGRESS: { FileInfo* pFileInfo = (FileInfo*)wParam; BeginProgress(pFileInfo); return NULL; } case WM_UPDATE_PROGRESS: { //FileInfo* pFileInfo = (FileInfo*)wParam; long received = (long)lParam; UpdateProgress( received); return NULL; } case WM_END_PROGRESS: { EndProgress(); return NULL; } default: return CMzWndEx::MzDefWndProc(message,wParam, lParam); } } void CMainWnd::UpdateList(NameHostMap &userMap) { int oricount = m_List.GetItemCount(); m_List.RemoveAll(); for(NameHostMap::iterator it = userMap.begin();it != userMap.end(); it++) { string txt = it->first; wstring itemName; A2W(txt,itemName); bool showItem = true; if(pConfig->GetShowLocal() == SHOW_LOCAL_HIDE) { vector localhost; GetLocalhost(localhost); bool showPopup = true; for(int i = 0; i < localhost.size(); i++) { if(itemName.compare(localhost[i].c_str()) == 0) { showItem = false; break; } } } if(showItem) { ListItem li; li.Text = itemName.c_str(); li.Data = (void*)&(it->second); m_List.AddItem(li); } } m_List.UpdateList(); m_List.Invalidate(); m_List.Update(); if(m_List.GetItemCount() < oricount) Invalidate(); } void CMainWnd::Refresh() { //刷新 try { // pMsgProcess->getUserMap().clear(); ipmsg->entry(); vector localhost; GetLocalhost(localhost); } catch (...) { } } void CMainWnd::ShowMsg(MsgBuf* pMsg) { char buf[MAX_UDPBUF]; sprintf(buf,"%s:\r\n %s",pMsg->hostSub.userName, pMsg->msgBuf); wstring wstr; A2W(buf,wstr); MzAutoMsgBoxEx(m_hWnd, wstr.c_str() ); } void CMainWnd::BeginProgress(FileInfo* pFileInfo) { if(pProcessDialog == NULL) { wstring fileName; A2W(pFileInfo->Fname(), fileName); pProcessDialog = new MzProgressDialog(); pProcessDialog->SetRange(0L, pFileInfo->Size()); pProcessDialog->SetCurValue(0L); pProcessDialog->SetTitle(fileName.c_str()); pProcessDialog->SetInfo(L""); pProcessDialog->SetShowInfo(true); pProcessDialog->SetShowTitle(true); pProcessDialog->SetSleepEnable(false, GetSystemIdleTimeout() / 2); pProcessDialog->BeginProgress(this->m_hWnd); pProcessDialog->UpdateProgress(); tickCount = ::GetTickCount(); } } void CMainWnd::UpdateProgress(long recved ) { if(pProcessDialog != NULL) { //pProcessDialog->SetSleepEnable(false, GetSystemIdleTimeout() / 2); //SystemIdleTimerReset(); DWORD time = ::GetTickCount() - this->tickCount; DWORD speed = recved / time * 1000; LPWSTR speedUnit; double dspeed; if(speed >= 0x40000000) { speed = speed >> 20; dspeed = (double)speed / 1024; speedUnit = L"G/S"; } else if(speed >= 0x100000) { speed = speed >> 10; dspeed = (double)speed / 1024; speedUnit = L"M/S"; } else if(speed * 1000 > 0x400) { dspeed = (double)speed / 1024; speedUnit = L"K/S"; } else { dspeed = (double)speed; speedUnit = L"B/S"; } TCHAR info[25]; wsprintf(info, L"%.2f%s %.2f%%", dspeed , speedUnit , (double) recved /(double)pProcessDialog->GetMaxValue() * 100 ); pProcessDialog->SetInfo(info); pProcessDialog->SetCurValue(recved); pProcessDialog->UpdateProgress(); } } void CMainWnd::EndProgress() { if(pProcessDialog != NULL) { pProcessDialog->EndProgress(); delete pProcessDialog; pProcessDialog = NULL; } } void CMainWnd::PopupSendMenu(HostSub* pHost) { if(IsFirstLoad) { wstring commandLine = ::GetCommandLine(); if (commandLine.compare(L"")) { //::MzMessageBoxEx(this->m_hWnd, commandLine.c_str(),NULL); SelectedFileStats fileList; WCHAR buf[300]; wcscpy(buf,commandLine.c_str()); WCHAR* p = (WCHAR*) buf; WCHAR* begin = wcstok(p, L":"); wstring fullPath = begin; while(begin ) { fullPath = begin; if(fullPath.length() > 0) fileList.push_back(fullPath); begin = wcstok(NULL, L":"); } if(fileList.size() > 1) { wstring name; GetFileName(fileList[0], name); swprintf_s(buf,300, L"是否发送%s等%d个文件?", name.c_str(), fileList.size()); } if( fileList.size() > 0 && MzMessageBoxEx(m_hWnd, buf , L"Test", MZ_YESNO) == 1) { IsFirstLoad = false; ipmsg->NotifyToSendFile(fileList, *pHost); return; } } else { IsFirstLoad = false; } } CPopupMenu ppm; struct PopupMenuItemProp pmip; pmip.itemCr = MZC_BUTTON_PELLUCID; pmip.itemRetID = IDC_PPM_CANCEL; pmip.str = L"取消"; ppm.AddItem(pmip); pmip.itemCr = MZC_BUTTON_ORANGE; pmip.itemRetID = IDC_PPM_SENDFILE; pmip.str = L"发送文件"; ppm.AddItem(pmip); RECT rc = MzGetWorkArea(); rc.top = rc.bottom - 218;//ppm.GetHeight(); ppm.Create(rc.left,rc.top,RECT_WIDTH(rc),RECT_HEIGHT(rc),m_hWnd,0,WS_POPUP); int nID = ppm.DoModal(); if(nID == IDC_PPM_SENDFILE) { SendFile(pHost); } m_ScrollWin.Update(); } void CMainWnd::SendFile(HostSub* pHost) { IMzSelect *pSelect = NULL; IFileBrowser *pFile = NULL; CoInitializeEx(NULL, COINIT_MULTITHREADED ); if ( SUCCEEDED( CoCreateInstance( CLSID_FileBrowser, NULL,CLSCTX_INPROC_SERVER ,IID_MZ_FileBrowser,(void **)&pFile ) ) ) { if( SUCCEEDED( pFile->QueryInterface( IID_MZ_Select, (void**)&pSelect ) ) ) { pFile->SetParentWnd( m_hWnd ); pFile->SetOpenDirectoryPath( L"\\Disk" ); //如果不调用此函数则默认为根目录 //pFile->SetExtFilter( L"*.txt;*.rar;" ); pFile->SetOpenDocumentType(DOCUMENT_SELECT_FILES); //应用根据需求进行文档打开方式的设置 if( pSelect->Invoke() ) { int fileCount = pFile->GetSelectedFileCount(); SelectedFileStats fileList; for( int i = 0 ;i < fileCount; i ++) { wstring fileName = pFile->GetSelectedFilePath(i); //ipmsg->NotifyToSendFile(fileName, *pHost); fileList.push_back(fileName); } int dirCount = pFile->GetSelectedDirectoryCount(); for (int i = 0; i < dirCount; i++) { fileList.push_back(pFile->GetSelectedDirectoryPath(i)); } ipmsg->NotifyToSendFile(fileList, *pHost); } pSelect->Release(); } pFile->Release(); } CoUninitialize(); ::InvalidateRect( m_hWnd, NULL, FALSE ); ::UpdateWindow( m_hWnd ); } void CMainWnd::GetLocalhost(vector& localHost) { char hostName[200]; ::gethostname(hostName, 200); addrinfo aiHints; addrinfo *aiList = NULL; memset(&aiHints, 0, sizeof(aiHints)); aiHints.ai_family = AF_INET; aiHints.ai_socktype = SOCK_STREAM; aiHints.ai_protocol = IPPROTO_TCP; ::getaddrinfo(hostName, NULL, &aiHints,&aiList); while(aiList) { sockaddr_in* addr =(sockaddr_in*) aiList->ai_addr; string temp(inet_ntoa(addr->sin_addr)); wstring ip; A2W(temp, ip); WCHAR hostItem[200]; wsprintf(hostItem, L"%s (%s)", pConfig->NickName().c_str(), ip.c_str()); ip = hostItem; localHost.push_back(ip); aiList = aiList->ai_next; } } /*====================================================================*/ MZ_IMPLEMENT_DYNAMIC(CMainWnd) // Application class derived from CMzApp class CM8MsgApp: public CMzApp { public: // The main window of the app. CMainWnd m_MainWnd; // Initialization of the application virtual BOOL Init() { // Init the COM relative library. CoInitializeEx(0, COINIT_MULTITHREADED); // Create the main window RECT rcWork = MzGetWorkArea(); m_MainWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0); //m_MainWnd.SetBgColor(RGB(0,0,0)); m_MainWnd.SetBgColor(MzGetThemeColor(TCI_WINDOW_BG)); m_MainWnd.Show(); // return TRUE means init success. return TRUE; } }; // The global variable of the application. CM8MsgApp theApp;