From 876ad153fd25c0605f516b0f3a6407999db69131 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期二, 17 十二月 2024 16:46:37 +0800
Subject: [PATCH] 1. 完善配方列表模块,与对称轴交互 2. 双击修改配方描述 3.配方列表控件动态变化

---
 SourceCode/Bond/BondEq/View/RecipeListDlg.cpp |  325 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 310 insertions(+), 15 deletions(-)

diff --git a/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp b/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp
index b62b949..5838e70 100644
--- a/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp
+++ b/SourceCode/Bond/BondEq/View/RecipeListDlg.cpp
@@ -19,11 +19,120 @@
 CRecipeListDlg::CRecipeListDlg(CWnd* pParent /*=nullptr*/)
 	: CDialogEx(IDD_DIALOG_RECIPE_LIST, pParent)
 {
-
+	m_nInitialWidth = 0;
+	m_nInitialHeight = 0;
+	m_staticCurrRecipe = new CBLLabel();
 }
 
 CRecipeListDlg::~CRecipeListDlg()
 {
+	if (m_staticCurrRecipe != nullptr) {
+		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)
+{
+	if (label.m_hWnd == nullptr) {
+		return;
+	}
+
+	// 鑾峰彇鎺т欢鐨勭煩褰㈠尯鍩�
+	CRect rect;
+	label.GetClientRect(&rect);
+
+	// 鍔ㄦ�佽绠楀瓧浣撳ぇ灏忥紝鍩轰簬鎺т欢鐨勯珮搴�
+	int fontSize = rect.Height() / 2; // 鎺т欢楂樺害鐨勪竴鍗婁綔涓哄瓧浣撳ぇ灏�
+	if (fontSize < 8) fontSize = 8;   // 鏈�灏忓瓧浣撳ぇ灏�
+	if (fontSize > 30) fontSize = 30; // 鏈�澶у瓧浣撳ぇ灏�
+
+	// 璁剧疆瀛椾綋澶у皬
+	label.SetFontSize(fontSize);
+
+	// 鍒锋柊鎺т欢鏄剧ず
+	label.Invalidate();
+	label.UpdateWindow();
 }
 
 void CRecipeListDlg::InitRecipeLise()
@@ -56,17 +165,17 @@
 	m_grid.SetItemText(nRowIdx, nColIdx++, _T("No."));
 	m_grid.SetColumnWidth(nColIdx, 10);
 	m_grid.SetItemText(nRowIdx, nColIdx++, _T("鍚嶇О"));
-	m_grid.SetColumnWidth(nColIdx, 50);
+	m_grid.SetColumnWidth(nColIdx, 120);
 	m_grid.SetItemText(nRowIdx, nColIdx++, _T("鎻忚堪"));
 	m_grid.SetColumnWidth(nColIdx, 30);
 	m_grid.SetItemText(nRowIdx, nColIdx++, _T("鍒涘缓鏃堕棿"));
 
-	m_grid.SetFixedRowSelection(FALSE);
-	m_grid.SetFixedColumnSelection(FALSE);
-	m_grid.SetEditable(TRUE);
-	m_grid.SetRowResize(FALSE);
-	m_grid.SetColumnResize(TRUE);
-	m_grid.ExpandColumnsToFit(TRUE);
+	m_grid.SetFixedRowSelection(FALSE);     // 璁剧疆鍥哄畾琛屼笉鍙�変腑
+	m_grid.SetFixedColumnSelection(FALSE);  // 璁剧疆鍥哄畾鍒椾笉鍙�変腑
+	m_grid.SetEditable(FALSE);				// 璁剧疆鍗曞厓鏍煎彲缂栬緫
+	m_grid.SetRowResize(FALSE);				// 璁剧疆琛屼笉鍙皟鏁村ぇ灏�
+	m_grid.SetColumnResize(TRUE);			// 璁剧疆鍒楀彲璋冩暣澶у皬
+	m_grid.ExpandColumnsToFit(TRUE);		// 鑷姩璋冩暣鍒楀
 	m_grid.SetListMode(TRUE);				// 鍚敤鍒楄〃妯″紡
 	m_grid.EnableSelection(TRUE);			// 鍚敤閫夋嫨
 	m_grid.SetSingleRowSelection(TRUE);		// 鑷姩鏁磋楂樹寒锛堥檺鍒朵负鍗曡閫夋嫨锛�
@@ -78,7 +187,7 @@
 void CRecipeListDlg::FillRecipeLise()
 {
 	// 娓呴櫎鏁版嵁琛岋紝淇濈暀琛ㄥご
-	for (int i = 1; i < m_grid.GetRowCount(); i++) {
+	for (int i = m_grid.GetRowCount() - 1; i > 0; --i) {
 		m_grid.DeleteRow(i);
 	}
 
@@ -126,17 +235,77 @@
 		m_grid.SetItemText(rowIdx, 2, description);								// 閰嶆柟鎻忚堪
 		m_grid.SetItemText(rowIdx, 3, createTime);								// 鍒涘缓鏃堕棿
 
-		m_grid.SetItemState(rowIdx, 0, GVIS_READONLY);
-		m_grid.SetItemState(rowIdx, 1, GVIS_READONLY);
-		m_grid.SetItemState(rowIdx, 3, GVIS_READONLY);
-
 		++rowIdx;
 	}
 
-	m_grid.ExpandColumnsToFit(FALSE);
-	m_grid.ExpandLastColumn();
+	m_grid.ExpandColumnsToFit(FALSE);   // 鑷姩璋冩暣鍒楀
+	m_grid.ExpandLastColumn();			// 鏈�鍚庝竴鍒楀~鍏呯綉鏍�
+
+	// 鍒锋柊缃戞牸鎺т欢
 	m_grid.Invalidate();
 	m_grid.UpdateWindow();
+}
+
+void CRecipeListDlg::UpdateDataFile(const CString& strRecipeName, const CString& strNewDescription)
+{
+	CStdioFile file;
+	CFileException fe;
+
+	// 鎵撳紑鏂囦欢浠ヨ鍙栧拰鍐欏叆
+	CString strFilePath;
+	strFilePath.Format(_T("%s\\RecipeList.txt"), CToolUnits::getRecipePath().c_str());
+	if (file.Open(strFilePath, CFile::modeReadWrite | CFile::shareDenyNone, &fe)) {
+		CString strLine;
+		CStringArray arrLines;
+		BOOL bFound = FALSE;
+
+		// 璇诲彇鏂囦欢鍐呭鍒板唴瀛�
+		while (file.ReadString(strLine)) {
+			// 灏嗚繖涓�琛屾寜閫楀彿鍒嗗壊
+			CStringArray arrColumns;
+			int nPos = 0;
+			while ((nPos = strLine.Find(_T(','))) != -1) {
+				arrColumns.Add(strLine.Left(nPos));
+				strLine = strLine.Mid(nPos + 1);
+			}
+			arrColumns.Add(strLine); // 鏈�鍚庝竴鍒�
+
+			// 濡傛灉绗竴鍒楋紙閰嶆柟鍚嶇О锛夊尮閰嶏紝淇敼绗簩鍒楋紙閰嶆柟鎻忚堪锛�
+			if (arrColumns.GetSize() > 0 && arrColumns[0] == strRecipeName) {
+				arrColumns[1] = strNewDescription; // 淇敼閰嶆柟鎻忚堪鍒�
+				bFound = TRUE;
+			}
+
+			// 灏嗕慨鏀瑰悗鐨勮淇濆瓨鍥炲唴瀛�
+			CString strUpdatedLine;
+			for (int i = 0; i < arrColumns.GetSize(); ++i) {
+				if (i > 0) strUpdatedLine += _T(",");
+				strUpdatedLine += arrColumns[i];
+			}
+			arrLines.Add(strUpdatedLine);
+		}
+
+		if (bFound) {
+			// 濡傛灉鎵惧埌浜嗛厤鏂瑰悕绉帮紝淇濆瓨淇敼鍚庣殑鍐呭鍥炴枃浠�
+			file.SeekToBegin();
+			file.SetLength(0);  // 娓呯┖鏂囦欢鍐呭
+			for (int i = 0; i < arrLines.GetSize(); ++i) {
+				file.WriteString(arrLines[i] + _T("\n"));
+			}
+		}
+		else {
+			// 濡傛灉娌℃湁鎵惧埌閰嶆柟鍚嶇О锛屽垯杩藉姞鏂伴厤鏂�
+			CString strNewLine;
+			strNewLine.Format(_T("%s,%s,%s"), strRecipeName, strNewDescription, CToolUnits::getCurrentTimeString().c_str());
+			file.SeekToEnd();
+			file.WriteString(strNewLine + _T("\n"));
+		}
+
+		file.Close();
+	}
+	else {
+		AfxMessageBox(_T("鏃犳硶鎵撳紑鏂囦欢"));
+	}
 }
 
 void CRecipeListDlg::DoDataExchange(CDataExchange* pDX)
@@ -149,6 +318,9 @@
 BEGIN_MESSAGE_MAP(CRecipeListDlg, CDialogEx)
 	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)
+	ON_NOTIFY(NM_DBLCLK, IDC_CUSTOM_RECIPE_LIST, &CRecipeListDlg::OnGridCellEditFinished)
+	ON_WM_SIZE()
 END_MESSAGE_MAP()
 
 
@@ -160,11 +332,87 @@
 	CDialogEx::OnInitDialog();
 
 	// TODO:  鍦ㄦ娣诲姞棰濆鐨勫垵濮嬪寲
+	m_staticCurrRecipe->SubclassDlgItem(IDC_STATIC_CURR_RECIPE, this);
+	m_staticCurrRecipe->SetBkColor(RGB(0, 110, 110));
+	m_staticCurrRecipe->ModifyStyle(0, SS_NOTIFY);
+	m_staticCurrRecipe->SetTextColor(RGB(255, 255, 255));
+	m_staticCurrRecipe->SetAlignment(AlignCenter);
+	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);
+
 	InitRecipeLise();
 
 	return TRUE;  // return TRUE unless you set the focus to a control
 	// 寮傚父: OCX 灞炴�ч〉搴旇繑鍥� FALSE
 }
+
+void CRecipeListDlg::OnSize(UINT nType, int cx, int cy)
+{
+	CDialogEx::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()
 {
@@ -298,3 +546,50 @@
 	// 鍒锋柊缃戞牸鎺т欢
 	FillRecipeLise();
 }
+
+void CRecipeListDlg::OnBnClickedButtonSelectRecipe()
+{
+	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
+	int nSelect = -1;
+	for (int i = 1; i < m_grid.GetRowCount(); i++) {
+		if (m_grid.IsCellSelected(i, 1)) {
+			nSelect = i;
+			break;
+		}
+	}
+
+	if (nSelect < 0) {
+		AfxMessageBox(_T("璇烽�夋嫨閰嶆柟锛�"));
+		return;
+	}
+
+	CString strSelectedRecipe = m_grid.GetItemText(nSelect, 1);
+	if (RecipeManager::getInstance().loadRecipe(std::string(CT2A(strSelectedRecipe)))) {
+		m_staticCurrRecipe->SetWindowText(strSelectedRecipe);
+	}
+	else {
+		AfxMessageBox(_T("鍔犺浇閰嶆柟澶辫触锛�"));
+	}
+}
+
+void CRecipeListDlg::OnGridCellEditFinished(NMHDR* pNotifyStruct, LRESULT* pResult)
+{
+	// 鑾峰彇淇敼鐨勮鍒椾俊鎭�
+	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*)pNotifyStruct;
+	int nRow = pItem->iRow;
+	int nCol = pItem->iColumn;
+
+	// 鍙鐞嗏�滈厤鏂规弿杩扳�濆垪鐨勪慨鏀�
+	if (nCol == 2) {
+		CString strRecipeName = m_grid.GetItemText(nRow, 1);
+		CInputDialog inputDialog(_T("閰嶆柟鎻忚堪"), _T("璇疯緭鍏ラ厤鏂规弿杩帮細"));
+		if (inputDialog.DoModal() != IDOK) {
+			*pResult = 0;
+			return;
+		}
+		CString strText = inputDialog.GetInputText();
+		UpdateDataFile(strRecipeName, strText);
+	}
+
+	*pResult = 0;
+}

--
Gitblit v1.9.3