From f48732b2178bbaf64f39099391832a6f271fed4d Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期六, 13 九月 2025 16:15:09 +0800
Subject: [PATCH] 1. SG精度检保存界面上的参数到配置文件 2. SG精度检允许设置开机自启动 3. 修改日志保存路径

---
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h         |   61 +++++++++++++++
 SourceCode/Bond/SGMeasurement/Logger.cpp                 |   14 ++
 SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user |    2 
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp       |  153 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 226 insertions(+), 4 deletions(-)

diff --git a/SourceCode/Bond/SGMeasurement/Logger.cpp b/SourceCode/Bond/SGMeasurement/Logger.cpp
index ecc94fd..e89b795 100644
--- a/SourceCode/Bond/SGMeasurement/Logger.cpp
+++ b/SourceCode/Bond/SGMeasurement/Logger.cpp
@@ -21,8 +21,17 @@
 {
 	CSingleLock lock(&m_csLogLock, TRUE);
 
+	TCHAR szPath[MAX_PATH] = { 0 };
+	GetModuleFileName(NULL, szPath, MAX_PATH);
+
+	CString strPath = szPath;
+	int pos = strPath.ReverseFind('\\');
+	if (pos != -1) {
+		strPath = strPath.Left(pos + 1);
+	}
+
 	CTime now = CTime::GetCurrentTime();
-	CString strLogDir = _T("Log");
+	CString strLogDir = strPath + _T("Log");
 
 	if (!PathFileExists(strLogDir)) {
 		CreateDirectory(strLogDir, NULL);
@@ -37,8 +46,7 @@
 			m_logFile.Close();
 		}
 
-		if (m_logFile.Open(strNewPath,
-			CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeBinary)) {
+		if (m_logFile.Open(strNewPath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite)) {
 
 			if (m_logFile.GetLength() == 0) {
 				WCHAR bom = 0xFEFF;
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
index 110a872..647a5f7 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
@@ -10,6 +10,6 @@
     <RemoteDebuggerCommand>\\DESKTOP-IODBVIQ\SGMeasurement\$(ProjectName).exe</RemoteDebuggerCommand>
     <RemoteDebuggerWorkingDirectory>\\DESKTOP-IODBVIQ\SGMeasurement</RemoteDebuggerWorkingDirectory>
     <RemoteDebuggerServerName>DESKTOP-IODBVIQ</RemoteDebuggerServerName>
-    <DebuggerFlavor>WindowsRemoteDebugger</DebuggerFlavor>
+    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
   </PropertyGroup>
 </Project>
\ No newline at end of file
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
index ecdface..6ee6018 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
@@ -94,6 +94,7 @@
 	, m_nTrayIconID(0)
 	, m_bTrayIconCreated(FALSE)
 	, m_bExitingFromTray(FALSE)
+	, m_nAutoStart(TRUE)
 {
 	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 }
@@ -131,6 +132,9 @@
 	}
 
 	m_plcListener.Stop();
+
+	// 淇濆瓨閰嶇疆鏂囦欢
+	SaveConfig(GetConfigPath());
 
 	DestroyWindow();
 	CDialogEx::OnClose();
@@ -301,6 +305,134 @@
 			strLine.Empty();
 		}
 	}
+}
+
+CString CSGMeasurementDlg::GetAppDirectory()
+{
+	TCHAR szPath[MAX_PATH] = { 0 };
+	GetModuleFileName(NULL, szPath, MAX_PATH);
+
+	CString strPath = szPath;
+	int pos = strPath.ReverseFind('\\');
+	if (pos != -1) {
+		strPath = strPath.Left(pos + 1);
+	}
+
+	return strPath;
+}
+
+CString CSGMeasurementDlg::GetConfigPath()
+{
+	return GetAppDirectory() + _T("SGConfig.ini");
+}
+
+bool CSGMeasurementDlg::LoadConfig(const CString& strFile)
+{
+	CString strSection = _T("StorageConfig");
+	TCHAR buf[256];
+
+	GetPrivateProfileString(strSection, _T("UseTrigger"), _T("0"), buf, 256, strFile);
+	m_nUseTrigger = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("SavePointCount"), _T("100000"), buf, 256, strFile);
+	m_nSavePointCount = _ttoi(buf);
+
+	// 杈撳嚭绔彛
+	GetPrivateProfileString(strSection, _T("OutputPort"), _T("OUT1"), buf, 256, strFile);
+	{
+		int idx = m_comboOutputPort.FindStringExact(-1, buf);
+		if (idx != CB_ERR) {
+			m_comboOutputPort.SetCurSel(idx);
+		}
+		else {
+			m_comboOutputPort.SetCurSel(0);
+		}
+	}
+
+	// 璺冲彉妫�娴嬪弬鏁�
+	GetPrivateProfileString(strSection, _T("JumpThreshold"), _T("0.2"), buf, 256, strFile);
+	m_fJumpThreshold = static_cast<float>(_tstof(buf));
+
+	GetPrivateProfileString(strSection, _T("JumpWindow"), _T("3"), buf, 256, strFile);
+	m_nJumpWindow = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("ValleyMargin"), _T("0"), buf, 256, strFile);
+	m_nValleyMargin = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("MinGlass1Count"), _T("10"), buf, 256, strFile);
+	m_nMinGlass1Count = _ttoi(buf);
+
+	// 绋冲畾鍖哄煙鍙傛暟
+	GetPrivateProfileString(strSection, _T("FixedCount"), _T("5"), buf, 256, strFile);
+	m_nFixedCount = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("MaxDelta"), _T("0.05"), buf, 256, strFile);
+	m_fMaxDelta = static_cast<float>(_tstof(buf));
+
+	// 鑷惎鍔�
+	GetPrivateProfileString(strSection, _T("AutoStart"), _T("1"), buf, 256, strFile);
+	m_nAutoStart = _ttoi(buf);
+
+	return true;
+}
+
+bool CSGMeasurementDlg::SaveConfig(const CString& strFile)
+{
+	CString strSection = _T("StorageConfig");
+
+	WritePrivateProfileString(strSection, _T("UseTrigger"), std::to_wstring(m_nUseTrigger).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("SavePointCount"), std::to_wstring(m_nSavePointCount).c_str(), strFile);
+
+	// 杈撳嚭绔彛涓嬫媺妗�
+	CString strPort;
+	m_comboOutputPort.GetWindowText(strPort);
+	WritePrivateProfileString(strSection, _T("OutputPort"), strPort, strFile);
+
+	// 璺冲彉妫�娴�
+	WritePrivateProfileString(strSection, _T("JumpThreshold"), std::to_wstring(m_fJumpThreshold).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("JumpWindow"), std::to_wstring(m_nJumpWindow).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("ValleyMargin"), std::to_wstring(m_nValleyMargin).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("MinGlass1Count"), std::to_wstring(m_nMinGlass1Count).c_str(), strFile);
+
+	// 绋冲畾鍖�
+	WritePrivateProfileString(strSection, _T("FixedCount"), std::to_wstring(m_nFixedCount).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("MaxDelta"), std::to_wstring(m_fMaxDelta).c_str(), strFile);
+
+	// 鑷惎鍔�
+	WritePrivateProfileString(strSection, _T("AutoStart"), std::to_wstring(m_nAutoStart).c_str(), strFile);
+
+	return true;
+}
+
+bool CSGMeasurementDlg::SetAutoStart(bool bEnable)
+{
+	// 鑾峰彇褰撳墠绋嬪簭璺緞
+	TCHAR szPath[MAX_PATH] = { 0 };
+	GetModuleFileName(NULL, szPath, MAX_PATH);
+	CString strAppPath = szPath;
+
+	// 鑾峰彇搴旂敤绋嬪簭鍚嶇О
+	CString strAppName = ::PathFindFileName(strAppPath);
+	strAppName = strAppName.Left(strAppName.ReverseFind('.'));
+
+	HKEY hKey;
+	LONG lRet = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_WRITE, &hKey);
+
+	if (lRet != ERROR_SUCCESS) { 
+		return false;
+	}
+
+	if (bEnable) {
+		// 璁剧疆鑷惎
+		lRet = RegSetValueEx(hKey, strAppName, 0, REG_SZ, (BYTE*)(LPCTSTR)strAppPath, (strAppPath.GetLength() + 1) * sizeof(TCHAR));
+	}
+	else {
+		// 鍙栨秷鑷惎
+		lRet = RegDeleteValue(hKey, strAppName);
+	}
+
+	RegCloseKey(hKey);
+	return (lRet == ERROR_SUCCESS);
 }
 
 bool CSGMeasurementDlg::ConnectToDevice()
@@ -942,6 +1074,27 @@
 	});
 	m_plcListener.Start();
 
+	// 鍔犺浇閰嶇疆鏂囦欢
+	if (LoadConfig(GetConfigPath())) {
+		AppendLogLineRichStyled(_T("閰嶇疆宸蹭粠 SGConfig.ini 鍔犺浇鎴愬姛"), LOG_COLOR_SUCCESS);
+	}
+	else {
+		AppendLogLineRichStyled(_T("閰嶇疆鍔犺浇澶辫触锛屼娇鐢ㄩ粯璁ゅ弬鏁�"), LOG_COLOR_WARNING);
+	}
+
+	// 璁剧疆鑷姩鍚姩
+	if (SetAutoStart(m_nAutoStart)) {
+		if (m_nAutoStart) {
+			AppendLogLineRichStyled(_T("宸插惎鐢ㄥ紑鏈鸿嚜鍚姩"), LOG_COLOR_SUCCESS);
+		}
+		else {
+			AppendLogLineRichStyled(_T("宸插彇娑堝紑鏈鸿嚜鍚姩"), LOG_COLOR_WARNING);
+		}
+	}
+	else {
+		AppendLogLineRichStyled(_T("璁剧疆寮�鏈鸿嚜鍚姩澶辫触锛岃妫�鏌ユ潈闄�"), LOG_COLOR_ERROR);
+	}
+
 	// 鍒濆鍖栨棩蹇楁
 	AppendLogLineRichStyled(_T("鍑嗗灏辩华..."), LOG_COLOR_SUCCESS);
 
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
index 8b42bd2..41d41e5 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
@@ -118,6 +118,60 @@
 	void PrintSampleData(int nOutNo, const std::vector<float>& vecBuffer);
 
 	/**
+	 * @brief 鑾峰彇褰撳墠搴旂敤绋嬪簭鎵�鍦ㄧ殑鐩綍璺緞銆�
+	 *
+	 * 閫氳繃 GetModuleFileName 鑾峰彇褰撳墠鍙墽琛屾枃浠剁殑瀹屾暣璺緞锛�
+	 * 骞舵埅鍙栨帀鏂囦欢鍚嶉儴鍒嗭紝杩斿洖鐩綍璺緞锛屾湯灏捐嚜甯﹀弽鏂滄潬銆�
+	 *
+	 * @return CString 搴旂敤绋嬪簭鎵�鍦ㄧ洰褰曪紝渚嬪 "C:\\Program Files\\SGMeasurement\\"
+	 */
+	CString GetAppDirectory();
+
+	/**
+	 * @brief 鑾峰彇閰嶇疆鏂囦欢鐨勫畬鏁磋矾寰勩��
+	 *
+	 * 閰嶇疆鏂囦欢鍚嶅浐瀹氫负 "config.ini"锛屼綅浜庡簲鐢ㄧ▼搴忕洰褰曚笅銆�
+	 *
+	 * @return CString 閰嶇疆鏂囦欢瀹屾暣璺緞锛屼緥濡� "C:\\Program Files\\SGMeasurement\\config.ini"
+	 */
+	CString GetConfigPath();
+
+	/**
+	 * @brief 浠� ini 閰嶇疆鏂囦欢鍔犺浇瀛樺偍涓庡垎鏋愬弬鏁般��
+	 *
+	 * 璇诲彇鎸囧畾 ini 鏂囦欢涓殑鍙傛暟鍊硷紝骞舵洿鏂板璇濇绫讳腑鐨勬垚鍛樺彉閲忥紝
+	 * 鍖呮嫭瀛樺偍璁剧疆锛堣Е鍙戞柟寮忋�侀噰鏍风偣鏁帮級銆佽烦鍙樻娴嬪弬鏁般�佺ǔ瀹氬尯鎻愬彇鍙傛暟绛夈��
+	 * 鑻ユ枃浠朵腑缂哄皯鏌愪簺瀛楁锛屽皢浣跨敤榛樿鍊笺��
+	 *
+	 * @param strFile ini 鏂囦欢璺緞銆�
+	 * @return true 琛ㄧず鍔犺浇鎴愬姛锛宖alse 琛ㄧず鍔犺浇澶辫触锛堝鏂囦欢涓嶅瓨鍦級銆�
+	 */
+	bool LoadConfig(const CString& strFile);
+
+	/**
+	 * @brief 灏嗗綋鍓嶅瓨鍌ㄤ笌鍒嗘瀽鍙傛暟淇濆瓨鍒� ini 閰嶇疆鏂囦欢銆�
+	 *
+	 * 鎶婂璇濇绫讳腑鐨勬垚鍛樺彉閲忥紙瀛樺偍璁剧疆銆佽烦鍙樻娴嬨�佺ǔ瀹氬尯鎻愬彇绛夊弬鏁帮級
+	 * 搴忓垪鍖栧苟鍐欏叆鍒版寚瀹氱殑 ini 鏂囦欢涓紝浠ヤ究涓嬫鍚姩鏃舵仮澶嶃��
+	 *
+	 * @param strFile ini 鏂囦欢璺緞銆�
+	 * @return true 琛ㄧず淇濆瓨鎴愬姛锛宖alse 琛ㄧず淇濆瓨澶辫触銆�
+	 */
+	bool SaveConfig(const CString& strFile);
+
+	/**
+	 * @brief 璁剧疆鎴栧彇娑堝綋鍓嶇▼搴忕殑寮�鏈鸿嚜鍚姩銆�
+	 *
+	 * 璇ュ嚱鏁颁細鍦ㄦ敞鍐岃〃 `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run`
+	 * 涓嬪啓鍏ユ垨鍒犻櫎褰撳墠绋嬪簭鐨勮矾寰勶紝浠庤�屾帶鍒舵槸鍚﹂殢 Windows 鍚姩鑷姩杩愯銆�
+	 *
+	 * @param bEnable 鏄惁鍚敤鑷惎鍔ㄣ�倀rue 琛ㄧず璁剧疆寮�鏈鸿嚜鍚紝false 琛ㄧず鍙栨秷鑷惎銆�
+	 *
+	 * @return true 琛ㄧず鎿嶄綔鎴愬姛锛沠alse 琛ㄧず鎿嶄綔澶辫触锛堝娉ㄥ唽琛ㄦ潈闄愪笉瓒筹級銆�
+	 */
+	bool SetAutoStart(bool bEnable);
+
+	/**
 	 * @brief 灏濊瘯杩炴帴鍒版祴閲忚澶囥��
 	 *
 	 * @return true 琛ㄧず杩炴帴鎴愬姛锛宖alse 琛ㄧず杩炴帴澶辫触銆�
@@ -335,6 +389,13 @@
 	 */
 	BOOL m_bExitingFromTray;
 
+	// === 鑷惎鍔ㄧ浉鍏� ===
+
+	/**
+	 * @brief 鏄惁寮�鏈鸿嚜鍚姩
+	 */
+	BOOL m_nAutoStart;
+
 	// === PLC 淇″彿鐩戝惉鍣� ===
 
 	/**

--
Gitblit v1.9.3