From 1318cc77e20a82b3328aa82ea6b8d8ca600de44f Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期二, 10 六月 2025 15:51:55 +0800
Subject: [PATCH] 1. 拆分数据库,每一个管理类是单独的数据库文件 2. 修复复制产生的ID错误
---
SourceCode/Bond/Servo/CBaseDlg.cpp | 341 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 341 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/Servo/CBaseDlg.cpp b/SourceCode/Bond/Servo/CBaseDlg.cpp
new file mode 100644
index 0000000..86b7926
--- /dev/null
+++ b/SourceCode/Bond/Servo/CBaseDlg.cpp
@@ -0,0 +1,341 @@
+#include "stdafx.h"
+#include "CBaseDlg.h"
+#include "GridCtrl.h"
+
+// 全局主题对象
+Theme g_lightTheme = { RGB(255, 255, 255), RGB(0, 0, 0), RGB(240, 240, 240), RGB(200, 200, 200) };
+Theme g_darkTheme = { RGB(40, 40, 40), RGB(255, 255, 255), RGB(60, 60, 60), RGB(80, 80, 80) };
+
+CFont g_defaultFont;
+Theme* g_currentTheme = &g_lightTheme;
+
+// 全局资源句柄
+HINSTANCE g_hCurrentResource = NULL;
+
+IMPLEMENT_DYNAMIC(CBaseDlg, CDialogEx)
+
+CBaseDlg::CBaseDlg(UINT nID, CWnd* pPage) : CDialogEx(nID, pPage), m_bResizing(false)
+{
+ m_nID = nID;
+ m_pParent = pPage;
+ m_nInitialWidth = 0;
+ m_nInitialHeight = 0;
+}
+
+CBaseDlg::~CBaseDlg()
+{
+ // shared_ptr会自动清理内存,不需要手动删除
+ m_mapFonts.clear();
+ m_mapCtrlLayouts.clear();
+ m_mapControls.clear();
+}
+
+void CBaseDlg::SwitchTheme(ThemeType enThemeType)
+{
+ // 使用 map 来根据 themeType 查找主题
+ static const std::unordered_map<ThemeType, Theme*> themeMap = {
+ { ThemeType::Light, &g_lightTheme },
+ { ThemeType::Dark, &g_darkTheme }
+ };
+
+ // 设置当前主题
+ auto it = themeMap.find(enThemeType);
+ if (it != themeMap.end()) {
+ g_currentTheme = it->second;
+ }
+ else {
+ g_currentTheme = &g_lightTheme;
+ }
+
+ // 更新控件的外观
+ CWnd* pWnd = GetWindow(GW_CHILD);
+ while (pWnd) {
+ pWnd->Invalidate();
+ pWnd = pWnd->GetNextWindow();
+ }
+
+ // 更新对话框背景颜色
+ SetBackgroundColor(g_currentTheme->backgroundColor);
+}
+
+void CBaseDlg::LoadResourceLibrary(const CString& strLanguage)
+{
+ // 卸载之前加载的资源库
+ UnloadResourceLibrary();
+
+ // 加载新的资源库
+ g_hCurrentResource = AfxLoadLibrary(strLanguage);
+
+ // 设置新的资源句柄
+ if (g_hCurrentResource != NULL) {
+ AfxSetResourceHandle(g_hCurrentResource);
+ }
+}
+
+void CBaseDlg::UnloadResourceLibrary()
+{
+ // 卸载之前加载的资源库
+ if (g_hCurrentResource != NULL) {
+ FreeLibrary(g_hCurrentResource); // 释放当前资源库
+ g_hCurrentResource = NULL; // 清空资源句柄
+ }
+}
+
+CFont* CBaseDlg::GetOrCreateFont(int nFontSize)
+{
+ auto it = m_mapFonts.find(nFontSize);
+ if (it != m_mapFonts.end()) {
+ return it->second.get();
+ }
+
+ // 使用 shared_ptr 来管理字体对象
+ auto font = std::make_shared<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.get();
+}
+
+void CBaseDlg::SetDefaultFont()
+{
+ CFont* defaultFont = GetOrCreateFont(12);
+
+ CWnd* pWnd = GetWindow(GW_CHILD);
+ while (pWnd) {
+ TCHAR szClassName[256];
+ GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
+ if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
+ pWnd = pWnd->GetNextWindow();
+ continue;
+ }
+
+ pWnd->SetFont(defaultFont, TRUE);
+ pWnd = pWnd->GetNextWindow();
+ }
+}
+
+BOOL CBaseDlg::AddControl(UINT nCtrlID, CWnd* pControl)
+{
+ // 确保控件不重复添加
+ if (m_mapControls.find(nCtrlID) != m_mapControls.end()) {
+ return FALSE; // 控件已经存在
+ }
+
+ m_mapControls[nCtrlID] = std::unique_ptr<CWnd>(pControl);
+ return TRUE;
+}
+
+BOOL CBaseDlg::RemoveControl(UINT nCtrlID)
+{
+ auto it = m_mapControls.find(nCtrlID);
+ if (it != m_mapControls.end()) {
+ m_mapControls.erase(it);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+BOOL CBaseDlg::UpdateControlText(UINT nCtrlID, const CString& strText)
+{
+ auto it = m_mapControls.find(nCtrlID);
+ if (it != m_mapControls.end()) {
+ CWnd* pWnd = it->second.get();
+ if (pWnd->GetSafeHwnd() != nullptr)
+ {
+ pWnd->SetWindowText(strText);
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+CWnd* CBaseDlg::GetControl(UINT nCtrlID)
+{
+ auto it = m_mapControls.find(nCtrlID);
+ if (it != m_mapControls.end()) {
+ return it->second.get();
+ }
+ return nullptr;
+}
+
+void CBaseDlg::AdjustControls(float dScaleX, float dScaleY)
+{
+ if (m_bResizing) return; // 防止在调整过程中重复调整
+
+ m_bResizing = true;
+ 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 表示所有项的高度
+ }
+
+ 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();
+ }
+ m_bResizing = false;
+}
+
+void CBaseDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
+{
+ TCHAR szClassName[256];
+ GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
+
+ if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
+ return;
+ }
+
+ int fontSize = nHeight / 2;
+ if (fontSize < 8) fontSize = 8;
+ if (fontSize > 32) fontSize = 32;
+
+ CFont* pFont = GetOrCreateFont(fontSize);
+
+ pWnd->SetFont(pFont);
+ pWnd->Invalidate(); // 刷新控件显示
+}
+
+BEGIN_MESSAGE_MAP(CBaseDlg, CDialogEx)
+ ON_WM_SIZE()
+ ON_WM_GETMINMAXINFO()
+ ON_WM_CTLCOLOR()
+END_MESSAGE_MAP()
+
+BOOL CBaseDlg::OnInitDialog()
+{
+ CDialogEx::OnInitDialog();
+
+ // 获取当前语言
+ LANGID langId = GetUserDefaultLangID();
+ if (langId == LANG_CHINESE) {
+ // 加载中文资源
+ }
+ else {
+ // 加载英文资源
+ }
+
+ // 获取对话框的工作区(屏幕可用区域)
+ 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;
+
+ // 排除不需要操作的控件(如自定义控件 GridCtrl)
+ 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);
+
+ return TRUE;
+}
+
+void CBaseDlg::OnSize(UINT nType, int cx, int cy)
+{
+ CDialogEx::OnSize(nType, cx, cy);
+
+ if (nType == SIZE_MINIMIZED || m_mapCtrlLayouts.empty()) {
+ return;
+ }
+
+ // 检查尺寸变化是否足够大,避免频繁调整
+ //static int lastWidth = 0, lastHeight = 0;
+ //if (abs(cx - lastWidth) < 10 && abs(cy - lastHeight) < 10) {
+ // return;
+ //}
+
+ //lastWidth = cx;
+ //lastHeight = cy;
+
+ // 计算比例并调整布局
+ float dScaleX = static_cast<float>(cx) / m_nInitialWidth;
+ float dScaleY = static_cast<float>(cy) / m_nInitialHeight;
+
+ AdjustControls(dScaleX, dScaleY);
+}
+
+void CBaseDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
+{
+ lpMMI->ptMinTrackSize.x = 400;
+ lpMMI->ptMinTrackSize.y = 300;
+
+ CDialogEx::OnGetMinMaxInfo(lpMMI);
+}
+
+HBRUSH CBaseDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
+{
+ if (g_currentTheme) {
+ pDC->SetBkColor(g_currentTheme->backgroundColor);
+ pDC->SetTextColor(g_currentTheme->textColor);
+
+ // 返回背景画刷
+ return CreateSolidBrush(g_currentTheme->backgroundColor);
+ }
+
+ return CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
+}
\ No newline at end of file
--
Gitblit v1.9.3