From b8a705515cd307bd46e04eeccc0bf0e158133402 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期三, 18 十二月 2024 16:27:37 +0800
Subject: [PATCH] 1. 提取对话框动态变化控件大小的函数(新建对话框基类)

---
 SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp |  188 +++-------------------------------------------
 1 files changed, 15 insertions(+), 173 deletions(-)

diff --git a/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp b/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
index bc96b29..7cf9480 100644
--- a/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
+++ b/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: 鍦ㄦ娣诲姞娑堟伅澶勭悊绋嬪簭浠g爜鍜�/鎴栬皟鐢ㄩ粯璁ゅ��
 	KillTimer(TIMER_READ_PLC_DATA);
 
-	CDialogEx::OnClose();
+	CBaseDlg::OnClose();
 }

--
Gitblit v1.9.3