From 07b2cefe6e0dfe8bc530aab3b58d4159502ceba8 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期四, 21 十一月 2024 11:34:54 +0800
Subject: [PATCH] 1. 解决控件字体修改以后出现锯齿

---
 SourceCode/Bond/BondEq/View/UserManagerDlg.cpp |   90 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/SourceCode/Bond/BondEq/View/UserManagerDlg.cpp b/SourceCode/Bond/BondEq/View/UserManagerDlg.cpp
index 512eb5e..50136c1 100644
--- a/SourceCode/Bond/BondEq/View/UserManagerDlg.cpp
+++ b/SourceCode/Bond/BondEq/View/UserManagerDlg.cpp
@@ -25,6 +25,13 @@
 
 CUserManagerDlg::~CUserManagerDlg()
 {
+	for (auto& pair : m_mapFonts) {
+		if (pair.second) {
+			pair.second->DeleteObject();
+			delete pair.second;
+		}
+	}
+	m_mapFonts.clear();
 }
 
 void CUserManagerDlg::DoDataExchange(CDataExchange* pDX)
@@ -342,6 +349,42 @@
 	return false;
 }
 
+void CUserManagerDlg::SetDefaultFont()
+{
+	CFont* defaultFont = nullptr;
+
+	// 濡傛灉瀛椾綋绠$悊瀹瑰櫒涓湁榛樿澶у皬锛堝 12锛夌殑瀛椾綋锛岀洿鎺ヤ娇鐢�
+	auto it = m_mapFonts.find(12);
+	if (it != m_mapFonts.end()) {
+		defaultFont = it->second;
+	}
+	else {
+		// 鍒涘缓榛樿瀛椾綋
+		defaultFont = new CFont();
+		LOGFONT logFont = { 0 };
+		_tcscpy_s(logFont.lfFaceName, _T("Segoe UI"));
+		logFont.lfHeight = -12;
+		logFont.lfQuality = CLEARTYPE_QUALITY;
+		defaultFont->CreateFontIndirect(&logFont);
+		m_mapFonts[12] = defaultFont; // 瀛樺偍鍒板瓧浣撶鐞嗗鍣�
+	}
+
+	// 閬嶅巻鎵�鏈夋帶浠讹紝搴旂敤榛樿瀛椾綋
+	CWnd* pWnd = GetWindow(GW_CHILD);
+	while (pWnd) {
+		// 璺宠繃鐗规畩鎺т欢锛堝 MFCGridCtrl锛�
+		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();
+	}
+}
+
 void CUserManagerDlg::AdjustControls(int nWidth, int nHeight)
 {
 	CWnd* pWnd = GetWindow(GW_CHILD);
@@ -361,10 +404,14 @@
 		TCHAR szClassName[256];
 		GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
 
+		if (_tcsicmp(szClassName, _T("ComboBox")) == 0) {
+			CComboBox* pComboBox = (CComboBox*)pWnd;
+			pComboBox->SetItemHeight(-1, nHeight);  // -1 琛ㄧず鎵�鏈夐」鐨勯珮搴�
+		}
 
 		if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
 			CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd;
-			pGridCtrl->SetDefCellHeight(newHeight / 20);
+			pGridCtrl->SetDefCellHeight(newHeight / 21);
 			pGridCtrl->ExpandColumnsToFit(TRUE);
 			pGridCtrl->Invalidate();
 		}
@@ -382,27 +429,33 @@
 	TCHAR szClassName[256];
 	GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
 
-	if (_tcsicmp(szClassName, _T("Static")) == 0) {
-		CStatic* pStatic = (CStatic*)pWnd;
-		pStatic->ModifyStyle(0, SS_CENTER | SS_CENTERIMAGE);
-		return;
-	}
-
+	// 璺宠繃鐗规畩鎺т欢锛堝 MFCGridCtrl锛�
 	if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
 		return;
 	}
 
-	int fontSize = nHeight - 10;
-	CFont* pCurrentFont = pWnd->GetFont();
-	LOGFONT logFont;
-	pCurrentFont->GetLogFont(&logFont);
-	logFont.lfHeight = -fontSize;
+	// 鏍规嵁鎺т欢楂樺害鍔ㄦ�佽皟鏁村瓧浣撳ぇ灏�
+	int fontSize = nHeight / 2;
+	if (fontSize < 8) fontSize = 8;
 
-	CFont newFont;
-	newFont.CreateFontIndirect(&logFont);
+	// 妫�鏌ュ瓧浣撴槸鍚﹀凡缁忓瓨鍦�
+	auto it = m_mapFonts.find(fontSize);
+	if (it == m_mapFonts.end()) {
+		// 鍔ㄦ�佸垱寤烘柊瀛椾綋
+		CFont* newFont = new CFont();
+		LOGFONT logFont = { 0 };
+		_tcscpy_s(logFont.lfFaceName, _T("Segoe UI"));
+		logFont.lfHeight = -fontSize;
+		logFont.lfQuality = CLEARTYPE_QUALITY; // 鍚敤 ClearType 鎶楅敮榻�
+		newFont->CreateFontIndirect(&logFont);
 
-	pWnd->SetFont(&newFont);
-	pWnd->Invalidate();
+		// 瀛樺偍鍒板瓧浣撶鐞嗗鍣ㄤ腑
+		m_mapFonts[fontSize] = newFont;
+		it = m_mapFonts.find(fontSize);
+	}
+
+	pWnd->SetFont(it->second);
+	pWnd->Invalidate(); // 鍒锋柊鎺т欢鏄剧ず
 }
 
 BEGIN_MESSAGE_MAP(CUserManagerDlg, CDialogEx)
@@ -427,6 +480,9 @@
 	SetWindowText(_T("鐢ㄦ埛绠$悊"));
 	SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0);
 
+	// 璁剧疆榛樿瀛椾綋
+	SetDefaultFont();
+
 	GetClientRect(&clientRect);
 	m_nInitialWidth = clientRect.Width();
 	m_nInitialHeight = clientRect.Height();
@@ -436,7 +492,7 @@
 	int dlgHeight = dlgRect.Height() * 3;
 
 	if (dlgWidth > screenRect.Width()) {
-		dlgWidth = screenRect.Width();
+		dlgWidth = screenRect.Width(); 
 	}
 	if (dlgHeight > screenRect.Height()) {
 		dlgHeight = screenRect.Height();

--
Gitblit v1.9.3