|
|
@@ -0,0 +1,416 @@
|
|
|
+#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.zhipuzi.com"), 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;
|
|
|
+
|
|
|
+ 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)
|
|
|
+{
|
|
|
+ if(uMsg == WM_DOWNLOAD_FINISH)
|
|
|
+ {
|
|
|
+ //执行下载的安装包
|
|
|
+
|
|
|
+ HINSTANCE hNewExe = ShellExecuteA(NULL, "open", m_filepath.c_str(), NULL, NULL, SW_SHOW);
|
|
|
+
|
|
|
+ PostQuitMessage(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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());
|
|
|
+ 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必须是多字节
|
|
|
+ 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;
|
|
|
+}
|
|
|
+
|