chenluhua1980
2025-12-11 33f080ddc32f3545b685b2e0a7a5df3c35894270
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "stdafx.h"
#include "CHMPropertyPage.h"
 
 
IMPLEMENT_DYNAMIC(CHMPropertyPage, CDialogEx)
 
CHMPropertyPage::CHMPropertyPage(UINT nID, CWnd* pPage) : CDialogEx(nID, pPage)
{
 
}
 
CHMPropertyPage::~CHMPropertyPage()
{
 
}
 
void CHMPropertyPage::OnApply()
{
 
}
 
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;
}
 
void CHMPropertyPage::HandleBtnClick(HWND hBtn)
{
    for (auto& kv : m_btns) {
        if (kv.second != nullptr && kv.second->GetSafeHwnd() == hBtn) {
            OnClickedBtn(kv.first.c_str());
            break;
        }
    }
}
 
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();
}