UpgradeWnd.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
  2. // (“Fluent UI”)。该示例仅供参考,
  3. // 用以补充《Microsoft 基础类参考》和
  4. // MFC C++ 库软件随附的相关电子文档。
  5. // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
  6. // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
  7. // https://go.microsoft.com/fwlink/?LinkId=238214.
  8. //
  9. // 版权所有(C) Microsoft Corporation
  10. // 保留所有权利。
  11. #include "stdafx.h"
  12. #include "framework.h"
  13. #include "UpgradeWnd.h"
  14. #include "Resource.h"
  15. #include "MainFrm.h"
  16. #include "SATHelper.h"
  17. #include <filehelper.h>
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. extern CMainFrame* g_pMainFrame;
  24. #define ID_BTN_UPGRADE 7
  25. #define ID_EDIT_IMAGE_PATH 8
  26. #define ID_EIDT_FTP_USER 9
  27. #define ID_EDIT_FTP_PASSWORD 10
  28. #define ID_CHECK_REMEMBER_FTP 11
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CResourceViewBar
  31. CUpgradeWnd::CUpgradeWnd() noexcept
  32. {
  33. m_nCtrlHeight = 0;
  34. }
  35. CUpgradeWnd::~CUpgradeWnd()
  36. {
  37. RedRat::UnloadLibrary();
  38. }
  39. BEGIN_MESSAGE_MAP(CUpgradeWnd, CDockablePane)
  40. ON_WM_CREATE()
  41. ON_WM_SIZE()
  42. ON_WM_SETFOCUS()
  43. ON_WM_SETTINGCHANGE()
  44. ON_WM_PAINT()
  45. ON_COMMAND(ID_BTN_UPGRADE, OnUpgradeBtnClick)
  46. ON_UPDATE_COMMAND_UI(ID_BTN_UPGRADE, OnUpdateBtnUpgrade)
  47. ON_COMMAND(ID_CHECK_REMEMBER_FTP, OnCheckRememberFtpClick)
  48. ON_UPDATE_COMMAND_UI(ID_CHECK_REMEMBER_FTP, OnUpdateCheckRememberFtp)
  49. ON_EN_CHANGE(ID_EDIT_IMAGE_PATH, OnEnChangeEdit)
  50. ON_UPDATE_COMMAND_UI(ID_EIDT_FTP_USER, OnUpdateFtpUser)
  51. ON_UPDATE_COMMAND_UI(ID_EDIT_FTP_PASSWORD, OnUpdateFtpPassword)
  52. ON_WM_CONTEXTMENU()
  53. ON_WM_CTLCOLOR()
  54. END_MESSAGE_MAP()
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CResourceViewBar 消息处理程序
  57. void CUpgradeWnd::AdjustLayout()
  58. {
  59. if (GetSafeHwnd() == nullptr || (AfxGetMainWnd() != nullptr && AfxGetMainWnd()->IsIconic()))
  60. {
  61. return;
  62. }
  63. const int margin = 6;
  64. const int top = 10; // 离顶部距离;
  65. const int dr = 3; // 行间距
  66. const int dc = 5; // 列间距;
  67. int nHeight = top;
  68. CRect rcCtrl;
  69. CRect rectClient;
  70. GetClientRect(rectClient);
  71. // 1、编辑框;
  72. m_text_path.GetWindowRect(rcCtrl);
  73. ScreenToClient(rcCtrl);
  74. m_text_path.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + top, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
  75. m_edit_path.SetWindowPos(nullptr, rcCtrl.right + dr, rectClient.top + top, rectClient.Width() - rcCtrl.Width() - margin*2 - dr, m_nCtrlHeight, SWP_NOACTIVATE | SWP_NOZORDER);
  76. nHeight += m_nCtrlHeight + dr;
  77. // 2、ftp用户;
  78. m_text_user.GetWindowRect(rcCtrl);
  79. ScreenToClient(rcCtrl);
  80. m_text_user.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
  81. m_ftp_user.SetWindowPos(nullptr, rcCtrl.right + dr, rectClient.top + nHeight, rectClient.Width() - rcCtrl.Width() - margin * 2 - dr, m_nCtrlHeight, SWP_NOACTIVATE | SWP_NOZORDER);
  82. nHeight += m_nCtrlHeight + dr;
  83. // 3、ftp密码;
  84. m_text_password.GetWindowRect(rcCtrl);
  85. ScreenToClient(rcCtrl);
  86. m_text_password.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
  87. m_ftp_password.SetWindowPos(nullptr, rcCtrl.right + dr, rectClient.top + nHeight, rectClient.Width() - rcCtrl.Width() - margin * 2 - dr, m_nCtrlHeight, SWP_NOACTIVATE | SWP_NOZORDER);
  88. nHeight += m_nCtrlHeight + dr;
  89. // 4、是否记住ftp信息;
  90. m_check_remember.GetWindowRect(rcCtrl);
  91. ScreenToClient(rcCtrl);
  92. m_check_remember.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rectClient.Width() - margin * 2, rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
  93. nHeight += rcCtrl.Height() + dr;
  94. // 5、升级;
  95. m_btnUpgrade.GetWindowRect(rcCtrl);
  96. ScreenToClient(rcCtrl);
  97. m_btnUpgrade.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
  98. nHeight += rcCtrl.Height() + dr;
  99. // 提示语
  100. m_text_notify.GetWindowRect(rcCtrl);
  101. ScreenToClient(rcCtrl);
  102. m_text_notify.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight + dr, rectClient.Width() - margin * 2 , m_nCtrlHeight * 2, SWP_NOACTIVATE | SWP_NOZORDER);
  103. }
  104. int CUpgradeWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
  105. {
  106. if (CDockablePane::OnCreate(lpCreateStruct) == -1)
  107. return -1;
  108. CRect rectDummy(0,0,60,23);
  109. // 创建组合:
  110. const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP;
  111. // 编辑框;
  112. m_text_path.Create(GlobalString::g_strUpgradePath, SS_CENTER | SS_CENTERIMAGE | WS_CHILD | WS_VISIBLE , rectDummy, this, IDC_STATIC);
  113. //if (!m_edit_path.Create( WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS| WS_TABSTOP, rectDummy, this, ID_EDIT_IMAGE_PATH))
  114. if (!m_edit_path.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |ES_AUTOHSCROLL | WS_TABSTOP, rectDummy, this, ID_EDIT_IMAGE_PATH))
  115. {
  116. return -1;
  117. }
  118. m_text_user.Create(GlobalString::g_strUpgradeFTPUser, SS_CENTER | SS_CENTERIMAGE | WS_CHILD | WS_VISIBLE, rectDummy, this, IDC_STATIC);
  119. if (!m_ftp_user.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP, rectDummy, this, ID_EIDT_FTP_USER))
  120. {
  121. return -1;
  122. }
  123. m_text_password.Create(GlobalString::g_strUpgradeFTPPassword, SS_CENTER | SS_CENTERIMAGE | WS_CHILD | WS_VISIBLE, rectDummy, this, IDC_STATIC);
  124. if (!m_ftp_password.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD | ES_AUTOHSCROLL | WS_TABSTOP, rectDummy, this, ID_EDIT_FTP_PASSWORD))
  125. {
  126. //m_ftp_password.SetPasswordChar(‘*');
  127. return -1;
  128. }
  129. if (!m_check_remember.Create(GlobalString::g_strUpgradeRememberFTP, WS_CHILD | BS_CHECKBOX| WS_VISIBLE | WS_TABSTOP, rectDummy, this, ID_CHECK_REMEMBER_FTP))
  130. {
  131. return -1;
  132. }
  133. if (!m_btnUpgrade.Create(GlobalString::g_strUpgradeBtn, WS_CHILD | WS_VISIBLE | WS_TABSTOP, rectDummy, this, ID_BTN_UPGRADE))
  134. {
  135. return -1;
  136. }
  137. m_text_notify.Create(GlobalString::g_strUpgradeNotify, SS_LEFT | WS_CHILD | WS_VISIBLE, rectDummy, this, IDC_STATIC);
  138. // 以编辑框高度为所有控件高度标准;
  139. CRect rcCtrl;
  140. m_edit_path.GetClientRect(&rcCtrl);
  141. m_nCtrlHeight = rcCtrl.Height();
  142. // 设置控件样式;
  143. SetCtrlStyle();
  144. SetStaticFont();
  145. // 设置最小窗体;
  146. SetMinSize((530, 260));
  147. // 校正布局;
  148. AdjustLayout();
  149. m_ftp_user.SetWindowText(Global::g_Config.strFtpUser.c_str());
  150. m_ftp_password.SetWindowText(Global::g_Config.strFtpPassword.c_str());
  151. return 0;
  152. }
  153. void CUpgradeWnd::OnSize(UINT nType, int cx, int cy)
  154. {
  155. CDockablePane::OnSize(nType, cx, cy);
  156. TRACE2("坐标:%d,%d\n", cx, cy);
  157. AdjustLayout();
  158. }
  159. void CUpgradeWnd::OnUpgradeBtnClick()
  160. {
  161. std::thread t([](CUpgradeWnd* p) {
  162. CString strPath = _T(""), strFtpUser = _T(""), strFtpPassword = _T("");
  163. p->m_edit_path.GetWindowText(strPath);
  164. p->m_ftp_user.GetWindowText(strFtpUser);
  165. p->m_ftp_password.GetWindowText(strFtpPassword);
  166. if (p->_bIsFtpPath) {
  167. Global::g_Config.strFtpUser = strFtpUser.GetString();
  168. Global::g_Config.strFtpPassword = strFtpPassword.GetString();
  169. WritePrivateProfileString("SATHelper", "ftp_user", strFtpUser.GetString(), Global::g_szConfig);
  170. WritePrivateProfileString("SATHelper", "ftp_password", strFtpPassword.GetString(), Global::g_szConfig);
  171. }
  172. p->m_btnUpgrade.EnableWindow(FALSE);
  173. if ( Global::g_Config.nLanguage)
  174. p->m_text_notify.SetWindowText("升级中,请稍等……!");
  175. else
  176. p->m_text_notify.SetWindowText("Upgrading, please wait!");
  177. Sleep(1000);
  178. CCallPython pycall;
  179. TCHAR szPath[MAX_PATH] = { 0 };
  180. _stprintf_s(szPath, _T("%sscripts\\TVUpgrade.py"), Global::g_szCurModuleDir);
  181. PyObject* pRet = pycall.CallPython(szPath, _T("API_TVUpgrade"), 3, _variant_t(strPath.GetString()), _variant_t(strFtpUser.GetString()), _variant_t(strFtpPassword.GetString()));
  182. if (pRet == NULL)
  183. {
  184. if (Global::g_Config.nLanguage)
  185. p->m_text_notify.SetWindowText("Running error, please check the script!");
  186. else
  187. p->m_text_notify.SetWindowText("运行出错,请检查脚本!");
  188. }
  189. else
  190. {
  191. p->m_text_notify.SetWindowText(pycall.GetUTF8String(pRet).c_str());
  192. if (pRet)
  193. Py_DECREF(pRet);
  194. }
  195. p->m_btnUpgrade.EnableWindow();
  196. }, this);
  197. t.detach();
  198. }
  199. void CUpgradeWnd::OnUpdateBtnUpgrade(CCmdUI* pCmdUI)
  200. {
  201. }
  202. void CUpgradeWnd::OnCheckRememberFtpClick()
  203. {
  204. BOOL bCheck = m_check_remember.GetCheck();
  205. m_check_remember.SetCheck(!bCheck);
  206. Global::g_Config.bRememberFTPInfo = !bCheck;
  207. WritePrivateProfileString("SATHelper", "rememberFTPInfo", Global::g_Config.bRememberFTPInfo ? "1" : "0", Global::g_szConfig);
  208. }
  209. void CUpgradeWnd::OnUpdateCheckRememberFtp(CCmdUI* pCmdUI)
  210. {
  211. pCmdUI->SetCheck(Global::g_Config.bRememberFTPInfo);
  212. }
  213. void CUpgradeWnd::OnEnChangeEdit()
  214. {
  215. UpdateData(TRUE);
  216. CString strText = _T("");
  217. m_edit_path.GetWindowText(strText);
  218. strText.TrimLeft(_T(" "));
  219. strText.MakeLower();
  220. if (strText.Find(_T("ftp://")) == 0 )
  221. _bIsFtpPath = TRUE;
  222. else
  223. _bIsFtpPath = FALSE;
  224. }
  225. void CUpgradeWnd::OnUpdateFtpUser(CCmdUI* pCmdUI)
  226. {
  227. pCmdUI->Enable(_bIsFtpPath);
  228. }
  229. void CUpgradeWnd::OnUpdateFtpPassword(CCmdUI* pCmdUI)
  230. {
  231. pCmdUI->Enable(_bIsFtpPath);
  232. }
  233. void CUpgradeWnd::SetStaticFont()
  234. {
  235. ::DeleteObject(m_fntPropList.Detach());
  236. LOGFONT lf;
  237. afxGlobalData.fontRegular.GetLogFont(&lf);
  238. NONCLIENTMETRICS info;
  239. info.cbSize = sizeof(info);
  240. afxGlobalData.GetNonClientMetrics(info);
  241. lf.lfHeight = info.lfMenuFont.lfHeight;
  242. lf.lfWeight = info.lfMenuFont.lfWeight;
  243. lf.lfItalic = info.lfMenuFont.lfItalic;
  244. m_fntPropList.CreateFontIndirect(&lf);
  245. m_text_path.SetFont(&m_fntPropList);
  246. m_text_user.SetFont(&m_fntPropList);
  247. m_text_password.SetFont(&m_fntPropList);
  248. m_check_remember.SetFont(&m_fntPropList);
  249. m_text_notify.SetFont(&m_fntPropList);
  250. m_edit_path.SetFont(&m_fntPropList);
  251. m_ftp_user.SetFont(&m_fntPropList);
  252. m_ftp_password.SetFont(&m_fntPropList);
  253. }
  254. void CUpgradeWnd::SetCtrlStyle()
  255. {
  256. // 将button设置成checkbox;
  257. m_check_remember.SetCheck(Global::g_Config.bRememberFTPInfo ? BST_CHECKED : BST_UNCHECKED);
  258. /*m_check_remember.m_bHighlightChecked = TRUE;
  259. // CMFCButton只能用图标来显示check;
  260. m_check_remember.SetImage(IDB_UNCHECKED);
  261. m_check_remember.SetCheckedImage(IDB_CHECKED);
  262. m_check_remember.m_bTransparent = FALSE;
  263. m_check_remember.m_bDrawFocus = FALSE;
  264. m_check_remember.m_nFlatStyle = CMFCButton::BUTTONSTYLE_FLAT;
  265. m_check_remember.SizeToContent();*/
  266. //m_edit_path.EnableFolderBrowseButton();
  267. m_edit_path.EnableFileBrowseButton(_T(""), _T("Image Files(*.img)|*.img"
  268. "|Zip Files(*.zip)|*.zip"
  269. "|Rar Files(*.rar)|*.rar"
  270. "|Binary Files(*.bin)|*.bin"
  271. "|All Files (*.*)|*.*||"), 0);
  272. }
  273. // 如果CDockablePane无控件,改变大小时背景会刷新失败;
  274. // 所以要重载onpaint;
  275. void CUpgradeWnd::OnPaint()
  276. {
  277. CPaintDC dc(this); // device context for painting
  278. // TODO: 在此处添加消息处理程序代码
  279. // 不为绘图消息调用 CDockablePane::OnPaint();
  280. CRect rc;
  281. GetClientRect(rc);
  282. CBrush brush;
  283. brush.CreateSolidBrush(0xFFEFD5);
  284. dc.FillRect(&rc, &brush);
  285. }
  286. void CUpgradeWnd::OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/)
  287. {
  288. // TODO: 在此处添加消息处理程序代码
  289. }
  290. HBRUSH CUpgradeWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  291. {
  292. HBRUSH hbr = CDockablePane::OnCtlColor(pDC, pWnd, nCtlColor);
  293. // TODO: 在此更改 DC 的任何特性
  294. if (nCtlColor == CTLCOLOR_STATIC ) //静态文本颜色,并且指定控件ID号    && pWnd->GetDlgCtrlID() == IDC_STATIC   
  295. {
  296. pDC->SetTextColor(RGB(255, 0, 0));
  297. //pDC->SetBkColor(RGB(191,219,255));     //静态控件的背景颜色设置,红字蓝底。          
  298. pDC->SetBkMode(TRANSPARENT); //静态文本框背景设置为透明
  299. HBRUSH brush;
  300. brush = CreateSolidBrush(0xFFEFD5);//对话框的RGB值
  301. hbr = (HBRUSH)brush;
  302. }
  303. // TODO: 如果默认的不是所需画笔,则返回另一个画笔
  304. return hbr;
  305. }