LAPTOP-T815PCOQ\25526
2024-12-18 b8a705515cd307bd46e04eeccc0bf0e158133402
SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
@@ -21,13 +21,11 @@
// CAxisSettingsDlg 对话框
IMPLEMENT_DYNAMIC(CAxisSettingsDlg, CDialogEx)
IMPLEMENT_DYNAMIC(CAxisSettingsDlg, CBaseDlg)
CAxisSettingsDlg::CAxisSettingsDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_DIALOG_AXIS_SETTINGS, pParent)
   : CBaseDlg(IDD_DIALOG_AXIS_SETTINGS, pParent)
{
   m_nInitialWidth = 0;
   m_nInitialHeight = 0;
   m_pPLC = nullptr;
   m_bSEV = FALSE;
@@ -63,14 +61,6 @@
CAxisSettingsDlg::~CAxisSettingsDlg()
{
   for (auto& pair : m_mapFonts) {
      if (pair.second) {
         pair.second->DeleteObject();
         delete pair.second;
      }
   }
   m_mapFonts.clear();
   for (int i = 0; i < BTN_MAX; i++) {
      delete m_pBlBtns[i];
   }
@@ -97,7 +87,7 @@
void CAxisSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   CBaseDlg::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_COMBO_AXIS_NAME, m_comboAxisNO);
   DDX_Control(pDX, IDC_STATIC_AXIS_NUMBER, m_staticAxisNO);
   DDX_Control(pDX, IDC_STATIC_AXIS_DESCRIP, m_staticAxisDescription);
@@ -130,79 +120,6 @@
   return 0;
}
CFont* CAxisSettingsDlg::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 CAxisSettingsDlg::SetDefaultFont()
{
   CFont* defaultFont = GetOrCreateFont(12);
   // 遍历所有控件,应用默认字体
   CWnd* pWnd = GetWindow(GW_CHILD);
   while (pWnd) {
      pWnd->SetFont(defaultFont, TRUE);
      pWnd = pWnd->GetNextWindow();
   }
}
void CAxisSettingsDlg::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("ComboBox")) == 0) {
            CComboBox* pComboBox = (CComboBox*)pWnd;
            pComboBox->SetItemHeight(-1, newRect.Height());  // -1 表示所有项的高度
         }
         pWnd->MoveWindow(&newRect);
         AdjustControlFont(pWnd, newRect.Width(), newRect.Height());
      }
      pWnd = pWnd->GetNextWindow();
   }
}
void CAxisSettingsDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
{
   // 根据控件高度动态调整字体大小
   int fontSize = nHeight / 2;
   if (fontSize < 8) fontSize = 8;
   if (fontSize > 24) fontSize = 24;  // 最大字体大小
   // 获取或创建字体
   CFont* pFont = GetOrCreateFont(fontSize);
   pWnd->SetFont(pFont);
   pWnd->Invalidate(); // 刷新控件显示
}
void CAxisSettingsDlg::AdjustLabelFont(CBLLabel& label)
{
   if (label.m_hWnd == nullptr) {
@@ -216,7 +133,7 @@
   // 动态计算字体大小,基于控件的高度
   int fontSize = rect.Height() / 2; // 控件高度的一半作为字体大小
   if (fontSize < 8) fontSize = 8;   // 最小字体大小
   if (fontSize > 30) fontSize = 30; // 最大字体大小
   if (fontSize > 20) fontSize = 20; // 最大字体大小
   // 设置字体大小
   label.SetFontSize(fontSize);
@@ -832,7 +749,7 @@
}
BEGIN_MESSAGE_MAP(CAxisSettingsDlg, CDialogEx)
BEGIN_MESSAGE_MAP(CAxisSettingsDlg, CBaseDlg)
   ON_BN_CLICKED(IDC_BUTTON_AXIS_LAST, &CAxisSettingsDlg::OnBnClickedButtonAxisLast)
   ON_BN_CLICKED(IDC_BUTTON_AXIS_NEXT, &CAxisSettingsDlg::OnBnClickedButtonAxisNext)
   ON_BN_CLICKED(IDC_BUTTON_AXIS_ANCHOR_POINT_GROUP1, &CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPointGroup1)
@@ -854,8 +771,6 @@
   ON_BN_CLICKED(IDC_BUTTON_AXIS_DETAIL_SETTINGS, &CAxisSettingsDlg::OnBnClickedButtonAxisDetailSettings)
   ON_MESSAGE(ID_MSG_UPDATA_DATA_TO_UI, &CAxisSettingsDlg::OnUpdateDataToUI)
   ON_WM_SIZE()
   ON_WM_CTLCOLOR()
   ON_WM_SIZING()
   ON_WM_TIMER()
   ON_WM_CLOSE()
END_MESSAGE_MAP()
@@ -866,7 +781,7 @@
BOOL CAxisSettingsDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   CBaseDlg::OnInitDialog();
   // TODO:  在此添加额外的初始化
   CString strTitle;
@@ -896,6 +811,8 @@
      pLabel->SetTextColor(RGB(255, 255, 255));
      pLabel->SetAlignment(AlignCenter);
      pLabel->SetDynamicFont(TRUE);
      AdjustLabelFont(*pLabel);
   }
   
   // 初始化编辑框
@@ -970,47 +887,6 @@
   initializeAxisIDCombo();
   refreshAxisDetails(1);
   refreshPositionDetails(1, m_currentPage);
   CRect screenRect, dlgRect, clientRect;
   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;
         // 设置默认字体
         pWnd->SetFont(pDefaultFont);
      }
      pWnd = pWnd->GetNextWindow();
   }
   GetWindowRect(&dlgRect);
   int dlgWidth = dlgRect.Width() * 2;
   int dlgHeight = dlgRect.Height() * 2;
   SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0);
   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);
   
   SetTimer(TIMER_READ_PLC_DATA, 500, nullptr);
@@ -1056,7 +932,7 @@
         }
         if (currentIndex == -1) {
            return CDialogEx::PreTranslateMessage(pMsg);
            return CBaseDlg::PreTranslateMessage(pMsg);
         }
         CString descriptionCtrlName, positionCtrlName;
@@ -1070,7 +946,7 @@
         CWnd* pPositionCtrl = GetDlgItem(positionCtrlId);
         if (pDescriptionCtrl == nullptr || pPositionCtrl == nullptr) {
            return CDialogEx::PreTranslateMessage(pMsg);
            return CBaseDlg::PreTranslateMessage(pMsg);
         }
         PositionRange& position = recipeManager.getPositionByIndex(getCurrentSelectedAxisID(), m_currentPage, AXIS_PAGE_SIZE, currentIndex);
@@ -1082,7 +958,7 @@
            CString strText;
            GetDlgItem(IDC_EDIT_AXIS_CURR_POS)->GetWindowText(strText);
            if (strText.IsEmpty()) {
               return CDialogEx::PreTranslateMessage(pMsg);;
               return CBaseDlg::PreTranslateMessage(pMsg);;
            }
            double enteredValue = _ttof(strText);
@@ -1138,24 +1014,14 @@
      }
   }
   return CDialogEx::PreTranslateMessage(pMsg);
   return CBaseDlg::PreTranslateMessage(pMsg);
}
void CAxisSettingsDlg::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);
   // 动态调整各个 CBLLabel 的字体大小
   for (auto pLabel : m_pBlLabels) {
      AdjustLabelFont(*pLabel);
@@ -1182,30 +1048,6 @@
      pComboBox->MoveWindow(&rectComboBox);
      pComboBox->SetItemHeight(-1, rectButton.Height() - 6);
   }
}
void CAxisSettingsDlg::OnSizing(UINT fwSide, LPRECT pRect)
{
   if (fwSide == WMSZ_BOTTOMRIGHT) {
      if (pRect->right - pRect->left < 200) {
         pRect->right = pRect->left + 200;
      }
      if (pRect->bottom - pRect->top < 150) {
         pRect->bottom = pRect->top + 150;
      }
   }
   CDialogEx::OnSizing(fwSide, pRect);
}
HBRUSH CAxisSettingsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
   // TODO:  在此更改 DC 的任何特性
   // TODO:  如果默认的不是所需画笔,则返回另一个画笔
   return hbr;
}
void CAxisSettingsDlg::OnBnClickedButtonAxisLast()
@@ -1516,7 +1358,7 @@
      readPLCDataToUI(nAxisId);
   }   
   CDialogEx::OnTimer(nIDEvent);
   CBaseDlg::OnTimer(nIDEvent);
}
void CAxisSettingsDlg::OnClose()
@@ -1524,5 +1366,5 @@
   // TODO: 在此添加消息处理程序代码和/或调用默认值
   KillTimer(TIMER_READ_PLC_DATA);
   CDialogEx::OnClose();
   CBaseDlg::OnClose();
}