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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// CPageVarialbles.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "CPageVarialbles.h"
#include "afxdialogex.h"
#include "CVariableEditDlg2.h"
 
 
// CPageVarialbles 对话框
 
IMPLEMENT_DYNAMIC(CPageVarialbles, CHMPropertyPage)
 
CPageVarialbles::CPageVarialbles(CWnd* pParent /*=nullptr*/)
    : CHMPropertyPage(IDD_PAGE_VARIABLE, pParent)
{
 
}
 
CPageVarialbles::~CPageVarialbles()
{
}
 
void CPageVarialbles::DoDataExchange(CDataExchange* pDX)
{
    CHMPropertyPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST1, m_listCtrl);
}
 
 
BEGIN_MESSAGE_MAP(CPageVarialbles, CHMPropertyPage)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CPageVarialbles::OnLvnItemchangedList1)
END_MESSAGE_MAP()
 
 
// CPageVarialbles 消息处理程序
 
 
BOOL CPageVarialbles::OnInitDialog()
{
    CHMPropertyPage::OnInitDialog();
 
 
    // 读出列宽
    CString strIniFile, strItem;
    strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
    int width[8] = { 0, 218, 180, 180, 180, 180, 180, 180 };
    for (int i = 0; i < 8; i++) {
        strItem.Format(_T("Col_%d_Width"), i);
        width[i] = GetPrivateProfileInt("PageVariableListCtrl", strItem, width[i], strIniFile);
    }
 
 
    // 报表控件
    DWORD dwStyle = m_listCtrl.GetExtendedStyle();
    dwStyle |= LVS_EX_FULLROWSELECT;
    dwStyle |= LVS_EX_GRIDLINES;
    m_listCtrl.SetExtendedStyle(dwStyle);
 
    HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
    ListView_SetImageList(m_listCtrl.GetSafeHwnd(), imageList, LVSIL_SMALL);
    m_listCtrl.InsertColumn(0, _T(""), LVCFMT_RIGHT, width[0]);
    m_listCtrl.InsertColumn(1, _T("SV ID"), LVCFMT_LEFT, width[1]);
    m_listCtrl.InsertColumn(2, _T("SV Name"), LVCFMT_LEFT, width[2]);
    m_listCtrl.InsertColumn(3, _T("SV Format"), LVCFMT_LEFT, width[3]);
    m_listCtrl.InsertColumn(4, _T("SV Remark"), LVCFMT_LEFT, width[4]);
    loadVariables();
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}
 
 
HBRUSH CPageVarialbles::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CHMPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
 
    // TODO:  在此更改 DC 的任何特性
 
    // TODO:  如果默认的不是所需画笔,则返回另一个画笔
    return hbr;
}
 
 
void CPageVarialbles::OnDestroy()
{
    CHMPropertyPage::OnDestroy();
 
    // 保存列宽
    CString strIniFile, strItem, strTemp;
    strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
    CHeaderCtrl* pHeader = m_listCtrl.GetHeaderCtrl();
    for (int i = 0; i < pHeader->GetItemCount(); i++) {
        RECT rect;
        pHeader->GetItemRect(i, &rect);
        strItem.Format(_T("Col_%d_Width"), i);
        strTemp.Format(_T("%d"), rect.right - rect.left);
        WritePrivateProfileString("PageVariableListCtrl", strItem, strTemp, strIniFile);
    }
}
 
 
void CPageVarialbles::OnSize(UINT nType, int cx, int cy)
{
    CHMPropertyPage::OnSize(nType, cx, cy);
 
    if (GetDlgItem(IDC_LIST1) == nullptr) return;
 
    CRect rcClient, rcItem;
    GetClientRect(&rcClient);
    m_listCtrl.MoveWindow(12, 12, rcClient.Width() - 24, rcClient.Height() - 24);
}
 
void CPageVarialbles::OnApply()
{
    __super::OnApply();
}
 
void CPageVarialbles::loadVariables()
{
    auto& variables = theApp.m_model.m_hsmsPassive.getVariables();
    for (auto item : variables) {
        int index = m_listCtrl.InsertItem(m_listCtrl.GetItemCount(), _T(""));
        m_listCtrl.SetItemData(index, (DWORD_PTR)item);
        m_listCtrl.SetItemText(index, 1, std::to_string(item->getVarialbleId()).c_str());
        m_listCtrl.SetItemText(index, 2, item->getName().c_str());
        m_listCtrl.SetItemText(index, 3, SERVO::CVariable::formatToString(item->getFormat()).c_str());
        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);
}
 
void CPageVarialbles::OnLvnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    int nSelCount = m_listCtrl.GetSelectedCount();
 
    // 根据选中状态启用/禁用按钮
    if (CButton* pDel = GetBtnByName("删除")) {
        pDel->EnableWindow(nSelCount > 0);
    }
    if (CButton* pEdit = GetBtnByName("编辑")) {
        pEdit->EnableWindow(nSelCount > 0);
    }
 
    *pResult = 0;
}
 
void CPageVarialbles::OnClickedBtn(const char* btnName)
{
    ASSERT(btnName);
    if (_strcmpi(btnName, "新增") == 0) {
        int rc = UX_CanExecute(L"addVarialbles");
        if (rc != 1) {
            AfxMessageBox("操作权限不足,请联系管理人员!");
            return;
        }
        unsigned int newId = theApp.m_model.m_hsmsPassive.getMaxVariableId();
        int newIdInt = static_cast<int>(newId + 1);
        CVariableEditDlg2 dlg(_T("新增变量"), newIdInt, _T("U1"), _T(""), _T(""), this);
        if (dlg.DoModal() != IDOK) return;
        CString name = dlg.GetNameText();
        CString fmt = dlg.GetTypeText();
        CString remark = dlg.GetRemark();
 
        int ret = theApp.m_model.m_hsmsPassive.addVariable(CT2A(name), CT2A(fmt), CT2A(remark), newIdInt);
        if (ret == 0) {
            UX_RecordAction(L"addVarialbles");
            m_listCtrl.DeleteAllItems();
            loadVariables();
        }
        else {
            AfxMessageBox(_T("新增变量失败,格式是否正确?(U1/U2/I2/A20/A50/L)"));
        }
    }
    else if (_strcmpi(btnName, "删除") == 0) {
        POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
        if (pos == nullptr) return;
        int nItem = m_listCtrl.GetNextSelectedItem(pos);
        auto pVar = reinterpret_cast<SERVO::CVariable*>(m_listCtrl.GetItemData(nItem));
        if (pVar == nullptr) return;
 
        int rc = UX_CanExecute(L"delVarialbles");
        if (rc != 1) {
            AfxMessageBox("操作权限不足,请联系管理人员!");
            return;
        }
        int ret = theApp.m_model.m_hsmsPassive.deleteVariable(pVar->getVarialbleId());
        if (ret == 0) {
            UX_RecordAction(L"delVarialbles");
 
            m_listCtrl.DeleteAllItems();
            loadVariables();
            if (CButton* pDel = GetBtnByName("删除")) pDel->EnableWindow(FALSE);
            if (CButton* pEdit = GetBtnByName("编辑")) pEdit->EnableWindow(FALSE);
        }
    }
    else if (_strcmpi(btnName, "编辑") == 0) {
        POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
        if (pos == nullptr) return;
        int nItem = m_listCtrl.GetNextSelectedItem(pos);
        auto pVar = reinterpret_cast<SERVO::CVariable*>(m_listCtrl.GetItemData(nItem));
        if (pVar == nullptr) return;
 
        int rc = UX_CanExecute(L"editVarialbles");
        if (rc != 1) {
            AfxMessageBox("操作权限不足,请联系管理人员!");
            return;
        }
        CVariableEditDlg2 dlg(_T("编辑变量"),
            pVar->getVarialbleId(),
            CString(CA2T(SERVO::CVariable::formatToString(pVar->getFormat()).c_str())),
            CString(CA2T(pVar->getName().c_str())),
            CString(CA2T(pVar->getRemark().c_str())),
            this);
        if (dlg.DoModal() != IDOK) return;
        CString name = dlg.GetNameText();
        CString fmt = dlg.GetTypeText();
        CString remark = dlg.GetRemark();
 
        int ret = theApp.m_model.m_hsmsPassive.updateVariable(pVar->getVarialbleId(), CT2A(name), CT2A(fmt), CT2A(remark));
        if (ret == 0) {
            UX_RecordAction(L"editVarialbles");
            m_listCtrl.DeleteAllItems();
            loadVariables();
        }
        else {
            AfxMessageBox(_T("编辑变量失败,格式是否正确?(U1/U2/I2/A20/A50/L)"));
        }
    }
}