123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
- // (“Fluent UI”)。该示例仅供参考,
- // 用以补充《Microsoft 基础类参考》和
- // MFC C++ 库软件随附的相关电子文档。
- // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
- // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
- // https://go.microsoft.com/fwlink/?LinkId=238214.
- //
- // 版权所有(C) Microsoft Corporation
- // 保留所有权利。
- #include "stdafx.h"
- #include "framework.h"
- #include "UpgradeWnd.h"
- #include "Resource.h"
- #include "MainFrm.h"
- #include "SATHelper.h"
- #include <filehelper.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #define new DEBUG_NEW
- #endif
- extern CMainFrame* g_pMainFrame;
- #define ID_BTN_UPGRADE 7
- #define ID_EDIT_IMAGE_PATH 8
- #define ID_EIDT_FTP_USER 9
- #define ID_EDIT_FTP_PASSWORD 10
- #define ID_CHECK_REMEMBER_FTP 11
- /////////////////////////////////////////////////////////////////////////////
- // CResourceViewBar
- CUpgradeWnd::CUpgradeWnd() noexcept
- {
- m_nCtrlHeight = 0;
- }
- CUpgradeWnd::~CUpgradeWnd()
- {
- RedRat::UnloadLibrary();
- }
- BEGIN_MESSAGE_MAP(CUpgradeWnd, CDockablePane)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_SETFOCUS()
- ON_WM_SETTINGCHANGE()
- ON_WM_PAINT()
- ON_COMMAND(ID_BTN_UPGRADE, OnUpgradeBtnClick)
- ON_UPDATE_COMMAND_UI(ID_BTN_UPGRADE, OnUpdateBtnUpgrade)
- ON_COMMAND(ID_CHECK_REMEMBER_FTP, OnCheckRememberFtpClick)
- ON_UPDATE_COMMAND_UI(ID_CHECK_REMEMBER_FTP, OnUpdateCheckRememberFtp)
- ON_EN_CHANGE(ID_EDIT_IMAGE_PATH, OnEnChangeEdit)
- ON_UPDATE_COMMAND_UI(ID_EIDT_FTP_USER, OnUpdateFtpUser)
- ON_UPDATE_COMMAND_UI(ID_EDIT_FTP_PASSWORD, OnUpdateFtpPassword)
- ON_WM_CONTEXTMENU()
- ON_WM_CTLCOLOR()
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CResourceViewBar 消息处理程序
- void CUpgradeWnd::AdjustLayout()
- {
- if (GetSafeHwnd() == nullptr || (AfxGetMainWnd() != nullptr && AfxGetMainWnd()->IsIconic()))
- {
- return;
- }
- const int margin = 6;
- const int top = 10; // 离顶部距离;
- const int dr = 3; // 行间距
- const int dc = 5; // 列间距;
- int nHeight = top;
- CRect rcCtrl;
- CRect rectClient;
- GetClientRect(rectClient);
- // 1、编辑框;
- m_text_path.GetWindowRect(rcCtrl);
- ScreenToClient(rcCtrl);
- m_text_path.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + top, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
- m_edit_path.SetWindowPos(nullptr, rcCtrl.right + dr, rectClient.top + top, rectClient.Width() - rcCtrl.Width() - margin*2 - dr, m_nCtrlHeight, SWP_NOACTIVATE | SWP_NOZORDER);
- nHeight += m_nCtrlHeight + dr;
- // 2、ftp用户;
- m_text_user.GetWindowRect(rcCtrl);
- ScreenToClient(rcCtrl);
- m_text_user.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
- m_ftp_user.SetWindowPos(nullptr, rcCtrl.right + dr, rectClient.top + nHeight, rectClient.Width() - rcCtrl.Width() - margin * 2 - dr, m_nCtrlHeight, SWP_NOACTIVATE | SWP_NOZORDER);
- nHeight += m_nCtrlHeight + dr;
- // 3、ftp密码;
- m_text_password.GetWindowRect(rcCtrl);
- ScreenToClient(rcCtrl);
- m_text_password.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
- m_ftp_password.SetWindowPos(nullptr, rcCtrl.right + dr, rectClient.top + nHeight, rectClient.Width() - rcCtrl.Width() - margin * 2 - dr, m_nCtrlHeight, SWP_NOACTIVATE | SWP_NOZORDER);
- nHeight += m_nCtrlHeight + dr;
- // 4、是否记住ftp信息;
- m_check_remember.GetWindowRect(rcCtrl);
- ScreenToClient(rcCtrl);
- m_check_remember.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rectClient.Width() - margin * 2, rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
- nHeight += rcCtrl.Height() + dr;
- // 5、升级;
- m_btnUpgrade.GetWindowRect(rcCtrl);
- ScreenToClient(rcCtrl);
- m_btnUpgrade.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight, rcCtrl.Width(), rcCtrl.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
- nHeight += rcCtrl.Height() + dr;
- // 提示语
- m_text_notify.GetWindowRect(rcCtrl);
- ScreenToClient(rcCtrl);
- m_text_notify.SetWindowPos(nullptr, rectClient.left + margin, rectClient.top + nHeight + dr, rectClient.Width() - margin * 2 , m_nCtrlHeight * 2, SWP_NOACTIVATE | SWP_NOZORDER);
- m_text_notify.Invalidate();
- }
- int CUpgradeWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDockablePane::OnCreate(lpCreateStruct) == -1)
- return -1;
- CRect rectDummy(0,0,60,23);
- // 创建组合:
- const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP;
- // 编辑框;
- m_text_path.Create(GlobalString::g_strUpgradePath, SS_CENTER | SS_CENTERIMAGE | WS_CHILD | WS_VISIBLE , rectDummy, this, IDC_STATIC);
- //if (!m_edit_path.Create( WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS| WS_TABSTOP, rectDummy, this, ID_EDIT_IMAGE_PATH))
- if (!m_edit_path.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |ES_AUTOHSCROLL | WS_TABSTOP, rectDummy, this, ID_EDIT_IMAGE_PATH))
- {
- return -1;
- }
- m_text_user.Create(GlobalString::g_strUpgradeFTPUser, SS_CENTER | SS_CENTERIMAGE | WS_CHILD | WS_VISIBLE, rectDummy, this, IDC_STATIC);
- if (!m_ftp_user.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP, rectDummy, this, ID_EIDT_FTP_USER))
- {
- return -1;
- }
- m_text_password.Create(GlobalString::g_strUpgradeFTPPassword, SS_CENTER | SS_CENTERIMAGE | WS_CHILD | WS_VISIBLE, rectDummy, this, IDC_STATIC);
- if (!m_ftp_password.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD | ES_AUTOHSCROLL | WS_TABSTOP, rectDummy, this, ID_EDIT_FTP_PASSWORD))
- {
- //m_ftp_password.SetPasswordChar(‘*');
- return -1;
- }
- if (!m_check_remember.Create(GlobalString::g_strUpgradeRememberFTP, WS_CHILD | BS_CHECKBOX| WS_VISIBLE | WS_TABSTOP, rectDummy, this, ID_CHECK_REMEMBER_FTP))
- {
- return -1;
- }
- if (!m_btnUpgrade.Create(GlobalString::g_strUpgradeBtn, WS_CHILD | WS_VISIBLE | WS_TABSTOP, rectDummy, this, ID_BTN_UPGRADE))
- {
- return -1;
- }
- // ES_WANTRETURN;
- m_text_notify.Create(GlobalString::g_strUpgradeNotify, SS_LEFT | WS_CHILD | WS_VISIBLE , rectDummy, this, IDC_STATIC);
- // 以编辑框高度为所有控件高度标准;
- CRect rcCtrl;
- m_edit_path.GetClientRect(&rcCtrl);
- m_nCtrlHeight = rcCtrl.Height();
- // 设置控件样式;
- SetCtrlStyle();
- SetStaticFont();
- // 设置最小窗体;
- SetMinSize((530, 260));
- // 校正布局;
- AdjustLayout();
- m_ftp_user.SetWindowText(Global::g_Config.strFtpUser.c_str());
- m_ftp_password.SetWindowText(Global::g_Config.strFtpPassword.c_str());
- return 0;
- }
- void CUpgradeWnd::OnSize(UINT nType, int cx, int cy)
- {
- CDockablePane::OnSize(nType, cx, cy);
- TRACE2("坐标:%d,%d\n", cx, cy);
- AdjustLayout();
- }
- void CUpgradeWnd::OnUpgradeBtnClick()
- {
- std::thread t([](CUpgradeWnd* p) {
- CString strPath = _T(""), strFtpUser = _T(""), strFtpPassword = _T("");
- p->m_edit_path.GetWindowText(strPath);
- p->m_ftp_user.GetWindowText(strFtpUser);
- p->m_ftp_password.GetWindowText(strFtpPassword);
- if (p->_bIsFtpPath) {
- Global::g_Config.strFtpUser = strFtpUser.GetString();
- Global::g_Config.strFtpPassword = strFtpPassword.GetString();
- WritePrivateProfileString("SATHelper", "ftp_user", strFtpUser.GetString(), Global::g_szConfig);
- WritePrivateProfileString("SATHelper", "ftp_password", strFtpPassword.GetString(), Global::g_szConfig);
- }
- p->m_btnUpgrade.EnableWindow(FALSE);
- if ( Global::g_Config.nLanguage)
- p->m_text_notify.SetWindowText("升级中,请稍等……!");
- else
- p->m_text_notify.SetWindowText("Upgrading, please wait!");
- Sleep(1000);
-
- CCallPython pycall;
- TCHAR szPath[MAX_PATH] = { 0 };
- _stprintf_s(szPath, _T("%sscripts\\TVUpgrade.py"), Global::g_szCurModuleDir);
- PyObject* pRet = pycall.CallPython(szPath, _T("API_TVUpgrade"), 3, _variant_t(strPath.GetString()), _variant_t(strFtpUser.GetString()), _variant_t(strFtpPassword.GetString()));
- if (pRet == NULL)
- {
- if (Global::g_Config.nLanguage)
- p->m_text_notify.SetWindowText("Running error, please check the script!");
- else
- p->m_text_notify.SetWindowText("运行出错,请检查脚本!");
- }
- else
- {
- p->m_text_notify.SetWindowText(pycall.GetUTF8String(pRet).c_str());
- if (pRet)
- Py_DECREF(pRet);
- }
- p->m_btnUpgrade.EnableWindow();
- }, this);
- t.detach();
- }
- void CUpgradeWnd::OnUpdateBtnUpgrade(CCmdUI* pCmdUI)
- {
- }
- void CUpgradeWnd::OnCheckRememberFtpClick()
- {
- BOOL bCheck = m_check_remember.GetCheck();
- m_check_remember.SetCheck(!bCheck);
- Global::g_Config.bRememberFTPInfo = !bCheck;
- WritePrivateProfileString("SATHelper", "rememberFTPInfo", Global::g_Config.bRememberFTPInfo ? "1" : "0", Global::g_szConfig);
- }
- void CUpgradeWnd::OnUpdateCheckRememberFtp(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(Global::g_Config.bRememberFTPInfo);
- }
- void CUpgradeWnd::OnEnChangeEdit()
- {
- UpdateData(TRUE);
- CString strText = _T("");
- m_edit_path.GetWindowText(strText);
- strText.TrimLeft(_T(" "));
- strText.MakeLower();
- if (strText.Find(_T("ftp://")) == 0 )
- _bIsFtpPath = TRUE;
- else
- _bIsFtpPath = FALSE;
- }
- void CUpgradeWnd::OnUpdateFtpUser(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(_bIsFtpPath);
- }
- void CUpgradeWnd::OnUpdateFtpPassword(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(_bIsFtpPath);
- }
- void CUpgradeWnd::SetStaticFont()
- {
- ::DeleteObject(m_fntPropList.Detach());
- LOGFONT lf;
- afxGlobalData.fontRegular.GetLogFont(&lf);
- NONCLIENTMETRICS info;
- info.cbSize = sizeof(info);
- afxGlobalData.GetNonClientMetrics(info);
- lf.lfHeight = info.lfMenuFont.lfHeight;
- lf.lfWeight = info.lfMenuFont.lfWeight;
- lf.lfItalic = info.lfMenuFont.lfItalic;
- m_fntPropList.CreateFontIndirect(&lf);
- m_text_path.SetFont(&m_fntPropList);
- m_text_user.SetFont(&m_fntPropList);
- m_text_password.SetFont(&m_fntPropList);
- m_check_remember.SetFont(&m_fntPropList);
- m_text_notify.SetFont(&m_fntPropList);
- m_edit_path.SetFont(&m_fntPropList);
- m_ftp_user.SetFont(&m_fntPropList);
- m_ftp_password.SetFont(&m_fntPropList);
- }
- void CUpgradeWnd::SetCtrlStyle()
- {
- // 将button设置成checkbox;
- m_check_remember.SetCheck(Global::g_Config.bRememberFTPInfo ? BST_CHECKED : BST_UNCHECKED);
- /*m_check_remember.m_bHighlightChecked = TRUE;
- // CMFCButton只能用图标来显示check;
- m_check_remember.SetImage(IDB_UNCHECKED);
- m_check_remember.SetCheckedImage(IDB_CHECKED);
- m_check_remember.m_bTransparent = FALSE;
- m_check_remember.m_bDrawFocus = FALSE;
- m_check_remember.m_nFlatStyle = CMFCButton::BUTTONSTYLE_FLAT;
- m_check_remember.SizeToContent();*/
- //m_edit_path.EnableFolderBrowseButton();
- m_edit_path.EnableFileBrowseButton(_T(""), _T("Image Files(*.img)|*.img"
- "|Zip Files(*.zip)|*.zip"
- "|Rar Files(*.rar)|*.rar"
- "|Binary Files(*.bin)|*.bin"
- "|All Files (*.*)|*.*||"), 0);
- }
- // 如果CDockablePane无控件,改变大小时背景会刷新失败;
- // 所以要重载onpaint;
- void CUpgradeWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: 在此处添加消息处理程序代码
- // 不为绘图消息调用 CDockablePane::OnPaint();
- CRect rc;
- GetClientRect(rc);
- CBrush brush;
- brush.CreateSolidBrush(0xFFEFD5);
- dc.FillRect(&rc, &brush);
- }
- void CUpgradeWnd::OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/)
- {
- // TODO: 在此处添加消息处理程序代码
- }
- HBRUSH CUpgradeWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDockablePane::OnCtlColor(pDC, pWnd, nCtlColor);
- // TODO: 在此更改 DC 的任何特性
- if (nCtlColor == CTLCOLOR_STATIC ) //静态文本颜色,并且指定控件ID号 && pWnd->GetDlgCtrlID() == IDC_STATIC
- {
- //pDC->SetTextColor(RGB(255, 0, 0));
- //pDC->SetBkColor(RGB(191,219,255)); //静态控件的背景颜色设置,红字蓝底。
- pDC->SetBkMode(TRANSPARENT); //静态文本框背景设置为透明
- HBRUSH brush;
- brush = CreateSolidBrush(0xFFEFD5);//控件背景色
- hbr = (HBRUSH)brush;
- }
- // TODO: 如果默认的不是所需画笔,则返回另一个画笔
- return hbr;
- }
|