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/AxisSettingsDlg.cpp |  178 ++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 116 insertions(+), 62 deletions(-)

diff --git a/SourceCode/Bond/BondEq/AxisSettingsDlg.cpp b/SourceCode/Bond/BondEq/AxisSettingsDlg.cpp
index e92cb8c..867e469 100644
--- a/SourceCode/Bond/BondEq/AxisSettingsDlg.cpp
+++ b/SourceCode/Bond/BondEq/AxisSettingsDlg.cpp
@@ -20,11 +20,104 @@
 
 CAxisSettingsDlg::~CAxisSettingsDlg()
 {
+	for (auto& pair : m_mapFonts) {
+		if (pair.second) {
+			pair.second->DeleteObject();
+			delete pair.second;
+		}
+	}
+	m_mapFonts.clear();
 }
 
 void CAxisSettingsDlg::DoDataExchange(CDataExchange* pDX)
 {
 	CDialogEx::DoDataExchange(pDX);
+}
+
+void CAxisSettingsDlg::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) {
+		pWnd->SetFont(defaultFont, TRUE);
+		pWnd = pWnd->GetNextWindow();
+	}
+}
+
+void CAxisSettingsDlg::AdjustControls(int nWidth, int nHeight)
+{
+	CWnd* pWnd = GetWindow(GW_CHILD);
+	while (pWnd) {
+		UINT nCtrlID = pWnd->GetDlgCtrlID();
+
+		CRect ctrlRect;
+		pWnd->GetWindowRect(&ctrlRect);
+		ScreenToClient(&ctrlRect);
+
+		// 璁$畻鎺т欢鐨勬柊浣嶇疆鍜屽ぇ灏忥紝鎸夋瘮渚嬭皟鏁�
+		int newX = (int)(ctrlRect.left * (nWidth / (float)m_nInitialWidth));
+		int newY = (int)(ctrlRect.top * (nHeight / (float)m_nInitialHeight));
+		int newWidth = (int)(ctrlRect.Width() * (nWidth / (float)m_nInitialWidth));
+		int newHeight = (int)(ctrlRect.Height() * (nHeight / (float)m_nInitialHeight));
+
+		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 琛ㄧず鎵�鏈夐」鐨勯珮搴�
+		}
+
+		pWnd->MoveWindow(newX, newY, newWidth, newHeight);
+		AdjustControlFont(pWnd, newWidth, newHeight);
+
+		// 鑾峰彇涓嬩竴涓帶浠�
+		pWnd = pWnd->GetNextWindow();
+	}
+}
+
+void CAxisSettingsDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
+{
+	// 鏍规嵁鎺т欢楂樺害鍔ㄦ�佽皟鏁村瓧浣撳ぇ灏�
+	int fontSize = nHeight / 2;
+	if (fontSize < 8) fontSize = 8;
+
+	// 妫�鏌ュ瓧浣撴槸鍚﹀凡缁忓瓨鍦�
+	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);
+
+		// 瀛樺偍鍒板瓧浣撶鐞嗗鍣ㄤ腑
+		m_mapFonts[fontSize] = newFont;
+		it = m_mapFonts.find(fontSize);
+	}
+
+	pWnd->SetFont(it->second);
+	pWnd->Invalidate(); // 鍒锋柊鎺т欢鏄剧ず
 }
 
 
@@ -61,15 +154,29 @@
 	// TODO:  鍦ㄦ娣诲姞棰濆鐨勫垵濮嬪寲
 	//ModifyStyle(0, WS_THICKFRAME | WS_SIZEBOX);
 
-	CRect rect;
-	GetClientRect(&rect);
-	m_nInitialWidth = rect.Width();
-	m_nInitialHeight = rect.Height();
+	// 璁剧疆榛樿瀛椾綋
+	SetDefaultFont();
 
-	rect.right *= 1.5;
-	rect.bottom *= 1.5;
-	// 璋冩暣瀵硅瘽妗嗗ぇ灏�
-	MoveWindow(rect);
+	CRect screenRect, dlgRect, clientRect;
+	GetClientRect(&clientRect);
+	m_nInitialWidth = clientRect.Width();
+	m_nInitialHeight = clientRect.Height();
+
+	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);
 
 	return TRUE;  // return TRUE unless you set the focus to a control
 	// 寮傚父: OCX 灞炴�ч〉搴旇繑鍥� FALSE
@@ -189,57 +296,4 @@
 void CAxisSettingsDlg::OnBnClickedButtonAxisTestStop()
 {
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
-}
-
-void CAxisSettingsDlg::AdjustControls(int nWidth, int nHeight)
-{
-	CWnd* pWnd = GetWindow(GW_CHILD);
-	while (pWnd) {
-		UINT nCtrlID = pWnd->GetDlgCtrlID();
-
-		CRect ctrlRect;
-		pWnd->GetWindowRect(&ctrlRect);
-		ScreenToClient(&ctrlRect);
-
-		// 璁$畻鎺т欢鐨勬柊浣嶇疆鍜屽ぇ灏忥紝鎸夋瘮渚嬭皟鏁�
-		int newX = (int)(ctrlRect.left * (nWidth / (float)m_nInitialWidth));
-		int newY = (int)(ctrlRect.top * (nHeight / (float)m_nInitialHeight));
-		int newWidth = (int)(ctrlRect.Width() * (nWidth / (float)m_nInitialWidth));
-		int newHeight = (int)(ctrlRect.Height() * (nHeight / (float)m_nInitialHeight));
-
-		pWnd->MoveWindow(newX, newY, newWidth, newHeight);
-		AdjustControlFont(pWnd, newWidth, newHeight);
-
-		// 鑾峰彇涓嬩竴涓帶浠�
-		pWnd = pWnd->GetNextWindow();
-	}
-}
-
-void CAxisSettingsDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
-{
-	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;
-	}
-
-	if (_tcsicmp(szClassName, _T("ComboBox")) == 0) {
-		CComboBox* pComboBox = (CComboBox*)pWnd;
-		pComboBox->SetItemHeight(-1, nHeight);  // -1 琛ㄧず鎵�鏈夐」鐨勯珮搴�
-	}
-
-	int fontSize = nHeight - 10;
-	CFont* pCurrentFont = pWnd->GetFont();
-	LOGFONT logFont;
-	pCurrentFont->GetLogFont(&logFont);
-	logFont.lfHeight = -fontSize;
-
-	CFont newFont;
-	newFont.CreateFontIndirect(&logFont);
-
-	pWnd->SetFont(&newFont);
-	pWnd->Invalidate();
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3