From cb4de5ef9411ce1912c6f7d6cd85506c6f619f3c Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期三, 10 十二月 2025 15:08:55 +0800
Subject: [PATCH] 1.属性页增加按钮。

---
 SourceCode/Bond/Servo/CHMPropertyPage.h   |   18 ++++-
 SourceCode/Bond/Servo/CHMPropertyDlg.cpp  |   84 ++++++++++++++++++++++++++-
 SourceCode/Bond/Servo/CPageVarialbles.cpp |    9 +++
 SourceCode/Bond/Servo/CHMPropertyDlg.h    |    2 
 SourceCode/Bond/Servo/CPageVarialbles.h   |    1 
 SourceCode/Bond/Servo/CHMPropertyPage.cpp |   62 ++++++++++++++++++++
 6 files changed, 167 insertions(+), 9 deletions(-)

diff --git a/SourceCode/Bond/Servo/CHMPropertyDlg.cpp b/SourceCode/Bond/Servo/CHMPropertyDlg.cpp
index 2542fca..e6bba3e 100644
--- a/SourceCode/Bond/Servo/CHMPropertyDlg.cpp
+++ b/SourceCode/Bond/Servo/CHMPropertyDlg.cpp
@@ -5,7 +5,6 @@
 #include "Servo.h"
 #include "CHMPropertyDlg.h"
 #include "afxdialogex.h"
-#include "HmTab.h"
 
 
 // CEquipmentDlg 瀵硅瘽妗�
@@ -19,6 +18,7 @@
 	m_hbrBkgnd = nullptr;
 	m_nWndWidth = 0;
 	m_nWndHeight = 0;
+	m_pTab = nullptr;
 }
 
 CHMPropertyDlg::CHMPropertyDlg(const char* pszTitle, int width, int height)
@@ -29,6 +29,7 @@
 	m_nWndWidth = width;
 	m_nWndHeight = height;
 	m_strTitle = pszTitle;
+	m_pTab = nullptr;
 }
 
 CHMPropertyDlg::~CHMPropertyDlg()
@@ -74,12 +75,13 @@
 
 	// Tab
 	CString strTitle;
-	CHmTab* m_pTab = CHmTab::Hook(GetDlgItem(IDC_TAB1)->m_hWnd);
+	m_pTab = CHmTab::Hook(GetDlgItem(IDC_TAB1)->m_hWnd);
 	m_pTab->SetPaddingLeft(20);
 	m_pTab->SetItemMarginLeft(18);
 	for (int i = 0; i < m_pages.size(); i++) {
 		m_pages[i]->SetParent(this);
 		m_pages[i]->GetWindowText(strTitle);
+		m_pages[i]->OnCreateBtns();
 		m_pTab->AddItem(strTitle, i == m_pages.size() - 1);
 	}
 	m_pTab->SetCurSel(0);
@@ -125,7 +127,7 @@
 void CHMPropertyDlg::OnSize(UINT nType, int cx, int cy)
 {
 	CDialogEx::OnSize(nType, cx, cy);
-	if (GetDlgItem(IDC_TAB1) == nullptr) return;
+	if (m_pTab == nullptr) return;
 	Resize();
 }
 
@@ -157,8 +159,36 @@
 	pItem->GetWindowRect(&rcItem);
 	pItem->MoveWindow(x2 - rcItem.Width(), y2 - rcItem.Height(),
 		rcItem.Width(), rcItem.Height());
-	y2 -= rcItem.Height();
-	y2 -= 12;
+	y2 -= rcItem.Height() + 12;
+
+	// 褰撳墠瀛愰〉鎸夐挳锛堝鏋滄湁锛�
+	int btnY = y2 - rcItem.Height(); // 淇濇寔涓庡簲鐢ㄦ寜閽悓楂�
+	int btnX = 12;
+	int curIndex = (m_pTab != nullptr) ? m_pTab->GetCurSel() : 0;
+	if (curIndex >= 0 && curIndex < (int)m_pages.size()) {
+		auto& btnMap = m_pages[curIndex]->getBtns();
+		// 鎸� BTN_ORDER 鎺掑簭
+		std::vector<std::pair<int, CButton*>> ordered;
+		for (auto& kv : btnMap) {
+			CButton* btn = kv.second;
+			if (btn == nullptr || !::IsWindow(btn->GetSafeHwnd())) continue;
+			int order = (int)(INT_PTR)::GetProp(btn->GetSafeHwnd(), _T("BTN_ORDER"));
+			ordered.emplace_back(order, btn);
+		}
+		std::sort(ordered.begin(), ordered.end(), [](const auto& a, const auto& b) {
+			return a.first < b.first;
+		});
+		for (auto& item : ordered) {
+			CButton* btn = item.second;
+			CRect rcBtn;
+			btn->GetWindowRect(&rcBtn);
+			if (rcBtn.Width() <= 0 || rcBtn.Height() <= 0) {
+				rcBtn.SetRect(0, 0, 80, 28);
+			}
+			btn->MoveWindow(btnX, btnY, rcBtn.Width(), rcBtn.Height());
+			btnX += rcBtn.Width() + 8;
+		}
+	}
 
 	// 鍒嗛殧绾�
 	pItem = GetDlgItem(IDC_LINE1);
@@ -185,6 +215,50 @@
 	for (int i = 0; i < m_pages.size(); i++) {
 		m_pages[i]->ShowWindow(i == index ? SW_SHOW : SW_HIDE);
 	}
+
+	// 闅愯棌鎵�鏈夐〉闈㈢殑鎸夐挳
+	for (auto page : m_pages) {
+		for (auto& kv : page->getBtns()) {
+			CButton* btn = kv.second;
+			if (btn != nullptr && ::IsWindow(btn->GetSafeHwnd())) {
+				btn->ShowWindow(SW_HIDE);
+			}
+		}
+	}
+
+	// 鍒涘缓骞舵樉绀哄綋鍓嶉〉闈㈢殑鎸夐挳
+	auto& btns = m_pages[index]->getBtns();
+	if (!btns.empty()) {
+		CRect rcClient;
+		GetClientRect(&rcClient);
+		const int margin = 12;
+		const int spacing = 8;
+		int x = margin;
+		int y = rcClient.bottom - 40; // 棰勭暀搴曢儴鍖哄煙
+
+		// 鎸� BTN_ORDER 鎺掑簭
+		std::vector<std::pair<int, CButton*>> ordered;
+		for (auto& kv : btns) {
+			CButton* btn = kv.second;
+			if (btn == nullptr || !::IsWindow(btn->GetSafeHwnd())) continue;
+			int order = (int)(INT_PTR)::GetProp(btn->GetSafeHwnd(), _T("BTN_ORDER"));
+			ordered.emplace_back(order, btn);
+		}
+		std::sort(ordered.begin(), ordered.end(), [](const auto& a, const auto& b) {
+			return a.first < b.first;
+		});
+		for (auto& item : ordered) {
+			CButton* btn = item.second;
+			CRect rc;
+			btn->GetWindowRect(&rc);
+			if (rc.Width() <= 0 || rc.Height() <= 0) {
+				rc.SetRect(0, 0, 80, 28);
+			}
+			btn->MoveWindow(x, y, rc.Width(), rc.Height());
+			btn->ShowWindow(SW_SHOW);
+			x += rc.Width() + spacing;
+		}
+	}
 }
 
 void CHMPropertyDlg::SetWindowSize(int width, int height)
diff --git a/SourceCode/Bond/Servo/CHMPropertyDlg.h b/SourceCode/Bond/Servo/CHMPropertyDlg.h
index 9ff65b1..243680b 100644
--- a/SourceCode/Bond/Servo/CHMPropertyDlg.h
+++ b/SourceCode/Bond/Servo/CHMPropertyDlg.h
@@ -1,6 +1,7 @@
 锘�#pragma once
 #include <vector>
 #include "CHMPropertyPage.h"
+#include "HmTab.h"
 
 
 // CEquipmentDlg 瀵硅瘽妗�
@@ -30,6 +31,7 @@
 	std::vector<CHMPropertyPage*> m_pages;
 	int m_nWndWidth;
 	int m_nWndHeight;
+	CHmTab* m_pTab;
 
 
 // 瀵硅瘽妗嗘暟鎹�
diff --git a/SourceCode/Bond/Servo/CHMPropertyPage.cpp b/SourceCode/Bond/Servo/CHMPropertyPage.cpp
index 3591ec4..d692eb8 100644
--- a/SourceCode/Bond/Servo/CHMPropertyPage.cpp
+++ b/SourceCode/Bond/Servo/CHMPropertyPage.cpp
@@ -18,3 +18,65 @@
 {
 
 }
+
+void CHMPropertyPage::OnCreateBtns()
+{
+
+}
+
+CButton* CHMPropertyPage::CreateBtn(const char* name, int w, int h, const UINT id)
+{
+	std::string key = std::string(name);
+	auto it = m_btns.find(key);
+	if (it != m_btns.end()) {
+		return it->second;
+	}
+
+	CButton* pBtn = new CButton();
+	pBtn->Create(name, WS_CHILD, CRect(0, 0, w, h), GetParent(), id);
+	// 浣跨敤榛樿GUI瀛椾綋
+	HFONT hFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
+	if (hFont != nullptr) {
+		pBtn->SetFont(CFont::FromHandle(hFont), FALSE);
+	}
+	::SetProp(pBtn->GetSafeHwnd(), _T("BTN_ORDER"), (HANDLE)(INT_PTR)m_btnOrderSeq++);
+	m_btns[key] = pBtn;
+	return pBtn;
+}
+
+CButton* CHMPropertyPage::GetBtnByName(const char* name)
+{
+	auto it = m_btns.find(std::string(name));
+	if (it != m_btns.end()) {
+		return it->second;
+	}
+	return nullptr;
+}
+
+std::map<std::string, CButton*>& CHMPropertyPage::getBtns()
+{
+	return m_btns;
+}
+
+BEGIN_MESSAGE_MAP(CHMPropertyPage, CDialogEx)
+	ON_WM_DESTROY()
+END_MESSAGE_MAP()
+
+void CHMPropertyPage::OnDestroy()
+{
+	CDialogEx::OnDestroy();
+
+	for (auto& kv : m_btns) {
+		CButton* btn = kv.second;
+		if (btn != nullptr) {
+			if (::IsWindow(btn->GetSafeHwnd())) {
+				::RemoveProp(btn->GetSafeHwnd(), _T("BTN_ORDER"));
+			}
+			if (::IsWindow(btn->GetSafeHwnd())) {
+				btn->DestroyWindow();
+			}
+			delete btn;
+		}
+	}
+	m_btns.clear();
+}
diff --git a/SourceCode/Bond/Servo/CHMPropertyPage.h b/SourceCode/Bond/Servo/CHMPropertyPage.h
index 8f90c5b..446215d 100644
--- a/SourceCode/Bond/Servo/CHMPropertyPage.h
+++ b/SourceCode/Bond/Servo/CHMPropertyPage.h
@@ -1,14 +1,24 @@
-#pragma once
+锘�#pragma once
+#include <map>
+#include <string>
 
 class CHMPropertyPage : public CDialogEx
 {
 	DECLARE_DYNAMIC(CHMPropertyPage)
 
 public:
-	CHMPropertyPage(UINT nID, CWnd* pPage);			// 标准构造函数
-	virtual ~CHMPropertyPage();						// 析构函数
+	CHMPropertyPage(UINT nID, CWnd* pPage);			// 鏍囧噯鏋勯�犲嚱鏁�
+	virtual ~CHMPropertyPage();						// 鏋愭瀯鍑芥暟
 	virtual void OnApply();
+	virtual void OnCreateBtns();
+	afx_msg void OnDestroy();
+	std::map<std::string, CButton*>& getBtns();
+	CButton* GetBtnByName(const char* name);
 
+protected:
+	CButton* CreateBtn(const char* name, int w, int h, const UINT id);
+	std::map<std::string, CButton*> m_btns;
+	int m_btnOrderSeq{ 0 };
 
+	DECLARE_MESSAGE_MAP()
 };
-
diff --git a/SourceCode/Bond/Servo/CPageVarialbles.cpp b/SourceCode/Bond/Servo/CPageVarialbles.cpp
index 5f4f5cc..7a6b34f 100644
--- a/SourceCode/Bond/Servo/CPageVarialbles.cpp
+++ b/SourceCode/Bond/Servo/CPageVarialbles.cpp
@@ -130,3 +130,12 @@
 		m_listCtrl.SetItemText(index, 4, item->getRemark().c_str());
 	}
 }
+
+void CPageVarialbles::OnCreateBtns()
+{
+	const int BTN_W = 80;
+	const int BTN_H = 28;
+	CreateBtn(_T("鏂板"), BTN_W, BTN_H, 1001);
+	CreateBtn(_T("鍒犻櫎"), BTN_W, BTN_H, 1002)->EnableWindow(FALSE);
+	CreateBtn(_T("缂栬緫"), BTN_W, BTN_H, 1003)->EnableWindow(FALSE);
+}
diff --git a/SourceCode/Bond/Servo/CPageVarialbles.h b/SourceCode/Bond/Servo/CPageVarialbles.h
index 3ccbdc0..6055dc6 100644
--- a/SourceCode/Bond/Servo/CPageVarialbles.h
+++ b/SourceCode/Bond/Servo/CPageVarialbles.h
@@ -14,6 +14,7 @@
 	virtual ~CPageVarialbles();
 	virtual void OnApply();
 	void loadVariables();
+	virtual void OnCreateBtns();
 
 private:
 	CListCtrlEx m_listCtrl;

--
Gitblit v1.9.3