chenluhua1980
2025-12-10 cb4de5ef9411ce1912c6f7d6cd85506c6f619f3c
1.属性页增加按钮。
已修改6个文件
176 ■■■■■ 文件已修改
SourceCode/Bond/Servo/CHMPropertyDlg.cpp 84 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/CHMPropertyDlg.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/CHMPropertyPage.cpp 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/CHMPropertyPage.h 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/CPageVarialbles.cpp 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/CPageVarialbles.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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)
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;
// 对话框数据
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();
}
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()
};
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);
}
SourceCode/Bond/Servo/CPageVarialbles.h
@@ -14,6 +14,7 @@
    virtual ~CPageVarialbles();
    virtual void OnApply();
    void loadVariables();
    virtual void OnCreateBtns();
private:
    CListCtrlEx m_listCtrl;