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/RecipeListDlg.cpp | 162 +++++-------------------------------------------------
1 files changed, 15 insertions(+), 147 deletions(-)
diff --git a/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp b/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp
index 5838e70..9546dca 100644
--- a/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp
+++ b/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;
--
Gitblit v1.9.3