LAPTOP-T815PCOQ\25526
2024-12-18 b8a705515cd307bd46e04eeccc0bf0e158133402
SourceCode/Bond/BondEq/View/RecipeListDlg.cpp
@@ -14,13 +14,11 @@
// CRecipeListDlg 对话框
IMPLEMENT_DYNAMIC(CRecipeListDlg, CDialogEx)
IMPLEMENT_DYNAMIC(CRecipeListDlg, CBaseDlg)
CRecipeListDlg::CRecipeListDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_DIALOG_RECIPE_LIST, pParent)
   : CBaseDlg(IDD_DIALOG_RECIPE_LIST, pParent)
{
   m_nInitialWidth = 0;
   m_nInitialHeight = 0;
   m_staticCurrRecipe = new CBLLabel();
}
@@ -30,86 +28,6 @@
      delete m_staticCurrRecipe;
      m_staticCurrRecipe = nullptr;
   }
   for (auto& pair : m_mapFonts) {
      if (pair.second) {
         pair.second->DeleteObject();
         delete pair.second;
      }
   }
   m_mapFonts.clear();
}
CFont* CRecipeListDlg::GetOrCreateFont(int nFontSize)
{
   auto it = m_mapFonts.find(nFontSize);
   if (it != m_mapFonts.end()) {
      return it->second;
   }
   CFont* font = new CFont();
   LOGFONT logFont = { 0 };
   _tcscpy_s(logFont.lfFaceName, _T("Segoe UI"));
   logFont.lfHeight = -nFontSize;
   logFont.lfQuality = CLEARTYPE_QUALITY;
   font->CreateFontIndirect(&logFont);
   m_mapFonts[nFontSize] = font;
   return font;
}
void CRecipeListDlg::AdjustControls(float dScaleX, float dScaleY)
{
   CWnd* pWnd = GetWindow(GW_CHILD);
   while (pWnd) {
      int nCtrlID = pWnd->GetDlgCtrlID();
      if (nCtrlID != -1 && m_mapCtrlLayouts.find(nCtrlID) != m_mapCtrlLayouts.end()) {
         CRect originalRect = m_mapCtrlLayouts[nCtrlID];
         CRect newRect(
            static_cast<int>(originalRect.left * dScaleX),
            static_cast<int>(originalRect.top * dScaleY),
            static_cast<int>(originalRect.right * dScaleX),
            static_cast<int>(originalRect.bottom * dScaleY));
         TCHAR szClassName[256];
         GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
         if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
            CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd;
            pGridCtrl->SetDefCellHeight(newRect.Height() / 21);
            pGridCtrl->ExpandColumnsToFit(TRUE);
            pGridCtrl->ExpandLastColumn();
            pGridCtrl->Invalidate();
            pGridCtrl->UpdateWindow();
         }
         pWnd->MoveWindow(&newRect);
         AdjustControlFont(pWnd, newRect.Width(), newRect.Height());
      }
      pWnd = pWnd->GetNextWindow();
   }
}
void CRecipeListDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
{
   TCHAR szClassName[256];
   GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
   // 跳过特殊控件(如 MFCGridCtrl)
   if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
      return;
   }
   // 根据控件高度动态调整字体大小
   int fontSize = nHeight / 2;
   if (fontSize < 8) fontSize = 8;
   if (fontSize < 24) fontSize = 24;
   // 获取或创建字体
   CFont* pFont = GetOrCreateFont(fontSize);
   pWnd->SetFont(pFont);
   pWnd->Invalidate(); // 刷新控件显示
}
void CRecipeListDlg::AdjustLabelFont(CBLLabel& label)
@@ -310,12 +228,12 @@
void CRecipeListDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   CBaseDlg::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_CUSTOM_RECIPE_LIST, m_grid);
}
BEGIN_MESSAGE_MAP(CRecipeListDlg, CDialogEx)
BEGIN_MESSAGE_MAP(CRecipeListDlg, CBaseDlg)
   ON_BN_CLICKED(IDC_BUTTON_CREATE_RECIPE, &CRecipeListDlg::OnBnClickedButtonCreateRecipe)
   ON_BN_CLICKED(IDC_BUTTON_DELETE_RECIPE, &CRecipeListDlg::OnBnClickedButtonDeleteRecipe)
   ON_BN_CLICKED(IDC_BUTTON_SELECT_RECIPE, &CRecipeListDlg::OnBnClickedButtonSelectRecipe)
@@ -329,7 +247,7 @@
BOOL CRecipeListDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   CBaseDlg::OnInitDialog();
   // TODO:  在此添加额外的初始化
   m_staticCurrRecipe->SubclassDlgItem(IDC_STATIC_CURR_RECIPE, this);
@@ -340,55 +258,7 @@
   m_staticCurrRecipe->SetDynamicFont(TRUE);
   m_staticCurrRecipe->SetWindowText(RecipeManager::getInstance().getCurrentRecipeName().c_str());
   CRect screenRect, dlgRect, clientRect;
   SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0);
   GetClientRect(&clientRect);
   m_nInitialWidth = clientRect.Width();
   m_nInitialHeight = clientRect.Height();
   // 初始化默认字体
   CFont* pDefaultFont = GetOrCreateFont(12);
   // 遍历所有子控件,记录初始位置并设置默认字体
   CWnd* pWnd = GetWindow(GW_CHILD);
   while (pWnd) {
      int nCtrlID = pWnd->GetDlgCtrlID();
      if (nCtrlID != -1) {
         // 记录控件初始布局
         CRect ctrlRect;
         pWnd->GetWindowRect(&ctrlRect);
         ScreenToClient(&ctrlRect);
         m_mapCtrlLayouts[nCtrlID] = ctrlRect;
         // 跳过特殊控件(如 MFCGridCtrl)
         TCHAR szClassName[256];
         GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
         if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
            pWnd = pWnd->GetNextWindow();
            continue;
         }
         // 设置默认字体
         pWnd->SetFont(pDefaultFont);
      }
      pWnd = pWnd->GetNextWindow();
   }
   GetWindowRect(&dlgRect);
   int dlgWidth = dlgRect.Width() * 2;
   int dlgHeight = dlgRect.Height() * 2;
   if (dlgWidth > screenRect.Width()) {
      dlgWidth = screenRect.Width();
   }
   if (dlgHeight > screenRect.Height()) {
      dlgHeight = screenRect.Height();
   }
   int centerX = screenRect.left + (screenRect.Width() - dlgWidth) / 2;
   int centerY = screenRect.top + (screenRect.Height() - dlgHeight) / 2;
   MoveWindow(centerX, centerY, dlgWidth, dlgHeight);
   AdjustLabelFont(*m_staticCurrRecipe);
   InitRecipeLise();
@@ -398,21 +268,11 @@
void CRecipeListDlg::OnSize(UINT nType, int cx, int cy)
{
   CDialogEx::OnSize(nType, cx, cy);
   CBaseDlg::OnSize(nType, cx, cy);
   // TODO: 在此处添加消息处理程序代码
   if (nType == SIZE_MINIMIZED || m_mapCtrlLayouts.empty()) {
      return;
   }
   float dScaleX = static_cast<float>(cx) / m_nInitialWidth;
   float dScaleY = static_cast<float>(cy) / m_nInitialHeight;
   // 遍历对话框中的所有控件
   AdjustControls(dScaleX, dScaleY);
   AdjustLabelFont(*m_staticCurrRecipe);
}
void CRecipeListDlg::OnBnClickedButtonCreateRecipe()
{
@@ -588,7 +448,15 @@
         return;
      }
      CString strText = inputDialog.GetInputText();
      m_grid.SetItemText(nRow, nCol, strText);
      UpdateDataFile(strRecipeName, strText);
      m_grid.ExpandColumnsToFit(FALSE);   // 自动调整列宽
      m_grid.ExpandLastColumn();         // 最后一列填充网格
      // 刷新网格控件
      m_grid.Invalidate();
      m_grid.UpdateWindow();
   }
   *pResult = 0;