| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- #include "../pch/pch.h"
- #include "CUpdateWnd.h"
- double CUpdateWnd::m_Percent;
- void CUpdateWnd::Init()
- {
- std::thread(&CUpdateWnd::Run, this).detach();
- }
- void CUpdateWnd::Notify(TNotifyUI& msg)
- {
- if(msg.sType == _T("click"))
- {
- if(msg.pSender->GetName() == _T("closebtn"))
- {
- PostQuitMessage(0);
- return;
- }
- else if(msg.pSender->GetName() == _T("guanwang"))
- {
- ShellExecute(NULL, _T("open"), _T("explorer.exe"), _T("https://www.qiaoba.ren"), NULL, SW_SHOW);
- }
- }
- else if(msg.sType == _T("itemselect"))
- {
- CDuiString name = msg.pSender->GetName();
- if(name == _T("accountcombo"))
- {
- CComboUI* pCom = static_cast<CComboUI*>(m_pm.FindControl(_T("accountcombo")));
- std::wstring name = pCom->GetItemAt(pCom->GetCurSel())->GetText();
- std::string password = CSetting::GetUser(CLewaimaiString::UnicodeToUTF8(name));
- std::wstring wspassword = CLewaimaiString::UTF8ToUnicode(password);
- CEditUI* pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
- pEdit->SetText(name.c_str());
- CEditUI* pPassword = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
- pPassword->SetText(wspassword.c_str());
- }
- }
- }
- LRESULT CUpdateWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
- styleValue &= ~WS_CAPTION;
- ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
- // 把自己的窗口句柄与窗口绘制管理器挂接在一起
- m_pm.Init(m_hWnd);
- m_pm.AddPreMessageFilter(this);
- CDialogBuilder builder;
- CControlUI* pRoot = builder.Create(_T("update.xml"), (UINT)0, NULL, &m_pm);
- ASSERT(pRoot && "Failed to parse XML");
- // 把这些控件绘制到本窗口上
- m_pm.AttachDialog(pRoot);
- // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
- m_pm.AddNotifier(this);
- Init();
- return 0;
- }
- LRESULT CUpdateWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- bHandled = FALSE;
- return 0;
- }
- /*
- *这个是窗口被销毁的时候调用的
- **/
- LRESULT CUpdateWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- bHandled = FALSE;
- return 0;
- }
- LRESULT CUpdateWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- if(::IsIconic(*this))
- {
- bHandled = FALSE;
- }
- return (wParam == 0) ? TRUE : FALSE;
- }
- LRESULT CUpdateWnd::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- return 0;
- }
- LRESULT CUpdateWnd::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- //在这里设置焦点才有用
- CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
- if(pAccountEdit)
- {
- pAccountEdit->SetFocus();
- }
- return 0;
- }
- LRESULT CUpdateWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- POINT pt;
- pt.x = GET_X_LPARAM(lParam);
- pt.y = GET_Y_LPARAM(lParam);
- ::ScreenToClient(*this, &pt);
- RECT rcClient;
- ::GetClientRect(*this, &rcClient);
- RECT rcCaption = m_pm.GetCaptionRect();
- if(pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
- && pt.y >= rcCaption.top && pt.y < rcCaption.bottom)
- {
- CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
- if(pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0)
- {
- return HTCAPTION;
- }
- }
- return HTCLIENT;
- }
- LRESULT CUpdateWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- SIZE szRoundCorner = m_pm.GetRoundCorner();
- if(!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0))
- {
- CDuiRect rcWnd;
- ::GetWindowRect(*this, &rcWnd);
- rcWnd.Offset(-rcWnd.left, -rcWnd.top);
- rcWnd.right++;
- rcWnd.bottom++;
- HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
- ::SetWindowRgn(*this, hRgn, TRUE);
- ::DeleteObject(hRgn);
- }
- bHandled = FALSE;
- return 0;
- }
- LRESULT CUpdateWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- LRESULT lRes = 0;
- BOOL bHandled = TRUE;
- switch(uMsg)
- {
- case WM_CREATE:
- lRes = OnCreate(uMsg, wParam, lParam, bHandled);
- break;
- case WM_CLOSE:
- lRes = OnClose(uMsg, wParam, lParam, bHandled);
- break;
- case WM_DESTROY:
- lRes = OnDestroy(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCACTIVATE:
- lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCCALCSIZE:
- lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCPAINT:
- lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCHITTEST:
- lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
- break;
- case WM_SIZE:
- lRes = OnSize(uMsg, wParam, lParam, bHandled);
- break;
- case WM_UPDATEPROGRESS:
- lRes = OnRefresh(uMsg, wParam, lParam, bHandled);
- break;
- case WM_DOWNLOAD_FINISH:
- lRes = OnDownloadFinish(uMsg, wParam, lParam, bHandled);
- break;
- default:
- bHandled = FALSE;
- }
- if(bHandled)
- {
- return lRes;
- }
- if(m_pm.MessageHandler(uMsg, wParam, lParam, lRes))
- {
- return lRes;
- }
- return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
- }
- LRESULT CUpdateWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
- {
- return false;
- }
- std::string CUpdateWnd::GetFileNameFormUrl(std::string url)
- {
- char fileName[512] = { 0 };
- int urlLen = strlen(url.c_str());
- char mUrl[512] = { 0 };
- char fName[256] = { 0 };
- strcpy(mUrl, url.c_str());
- int cutIndex = 0;
- int i = urlLen - 1, j = 0;
- while (mUrl[--i] != '/');
- i++;
- while (mUrl[i] != '\0' && mUrl[i] != '?' &&mUrl[i] != '&')
- {
- fName[j++] = mUrl[i++];
- }
- fName[j] = '\0';
- strcpy(fileName, fName);
- return fileName;
- }
- double CUpdateWnd::getDownloadFileLength(std::string url)
- {
- CURL *easy_handle = NULL;
- int ret = CURLE_OK;
- double size = -1;
- do
- {
- easy_handle = curl_easy_init();
- if(!easy_handle)
- {
- break;
- }
- // Only get the header data
- ret = curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());
- if (url.find("https://") == 0)
- {
- ret |= curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, false);
- ret |= curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYHOST, false);
- }
-
- ret |= curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1L);
- ret |= curl_easy_setopt(easy_handle, CURLOPT_NOBODY, 1L);
- ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, nousecb); // libcurl_a.lib will return error code 23 without this sentence on windows
- if(ret != CURLE_OK)
- {
- break;
- }
- ret = curl_easy_perform(easy_handle);
- if(ret != CURLE_OK)
- {
- char s[100] = { 0 };
- sprintf_s(s, sizeof(s), "error:%d:%s", ret, curl_easy_strerror(static_cast<CURLcode>(ret)));
- break;
- }
- // size = -1 if no Content-Length return or Content-Length=0
- ret = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
- if(ret != CURLE_OK)
- {
- break;
- }
- }
- while(0);
- curl_easy_cleanup(easy_handle);
- return size;
- }
- void CUpdateWnd::Run()
- {
- m_filepath = m_dir + GetFileNameFormUrl(m_url);
- CURLcode res;
- m_curl = curl_easy_init();
- if(m_curl)
- {
- m_resumeByte = 0;
- m_downloadFileLength = getDownloadFileLength(m_url);
- if(m_resumeByte >= (int)m_downloadFileLength)
- {
- return;
- }
- m_outfile = fopen(m_filepath.c_str(), "ab+");
- curl_easy_setopt(m_curl, CURLOPT_URL, m_url.c_str());//在此要注意,此url必须是多字节
- if (m_url.find("https://") == 0)
- {
- curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, false);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 0);
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, m_outfile);
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, my_write_func);
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, FALSE);
- curl_easy_setopt(m_curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
- curl_easy_setopt(m_curl, CURLOPT_PROGRESSDATA, NULL);//给下载进度回调函数传参数,在此是指针
- res = curl_easy_perform(m_curl);
- fclose(m_outfile);
- curl_easy_cleanup(m_curl);
- }
- //走到这里,下载完成了
- HWND hWnd = FindWindow(NULL, L"自动更新");
- if (hWnd)
- {
- ::PostMessage(hWnd, WM_DOWNLOAD_FINISH, 0, 0);
- }
- }
- size_t CUpdateWnd::my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
- {
- size_t nWrite = fwrite(ptr, size, nmemb, stream);
- if(nWrite == 0)
- {
- return CURL_WRITEFUNC_PAUSE;
- }
- else
- {
- return nWrite;
- }
- }
- int CUpdateWnd::my_progress_func(void *progress_data,
- double t, /* dltotal */
- double d, /* dlnow */
- double ultotal,
- double ulnow)
- {
- if (t > 0)
- {
- CUpdateWnd::m_Percent = d * 100.0 / t;
- }
- else
- {
- CUpdateWnd::m_Percent = 0;
- }
- HWND hWnd = FindWindow(NULL, L"自动更新");
- if(hWnd)
- {
- ::PostMessage(hWnd, WM_UPDATEPROGRESS, 0, 0);
- }
- return 0;
- }
- size_t CUpdateWnd::nousecb(char *buffer, size_t x, size_t y, void *userdata)
- {
- (void)buffer;
- (void)userdata;
- return x * y;
- }
- LRESULT CUpdateWnd::OnRefresh(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- CProgressUI* pProgress = static_cast<CProgressUI*>(m_pm.FindControl(_T("update_progress")));
- if(pProgress)
- {
- pProgress->SetValue((int)m_Percent);
- }
- return 0;
- }
- LRESULT CUpdateWnd::OnDownloadFinish(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- HINSTANCE hNewExe = ShellExecuteA(NULL, "open", m_filepath.c_str(), NULL, NULL, SW_SHOW);
- PostQuitMessage(0);
- return 0;
- }
|