LAPTOP-SNT8I5JK\Boounion
2025-09-17 62bec5118f5e5fe750017cf2f12d4a428ab092df
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
// CRobotCmdContainerDlg.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "afxdialogex.h"
#include "CRobotCmdContainerDlg.h"
 
 
// CRobotCmdContainerDlg 对话框
 
IMPLEMENT_DYNAMIC(CRobotCmdContainerDlg, CDialogEx)
 
CRobotCmdContainerDlg::CRobotCmdContainerDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_ROBOT_CMD_CONTAINER, pParent)
{
 
}
 
CRobotCmdContainerDlg::~CRobotCmdContainerDlg()
{
}
 
std::vector<SERVO::ROBOT_CMD_PARAM>& CRobotCmdContainerDlg::GetResultCmds()
{
    return m_cmdResult;
}
 
void CRobotCmdContainerDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_CHECK_ENABLE1, m_chkEnable[0]);
    DDX_Control(pDX, IDC_CHECK_ENABLE2, m_chkEnable[1]);
    DDX_Control(pDX, IDC_CHECK_ENABLE3, m_chkEnable[2]);
    DDX_Control(pDX, IDC_CHECK_ENABLE4, m_chkEnable[3]);
    DDX_Control(pDX, IDC_STATIC_PLACE1, m_placeHolder[0]);
    DDX_Control(pDX, IDC_STATIC_PLACE2, m_placeHolder[1]);
    DDX_Control(pDX, IDC_STATIC_PLACE3, m_placeHolder[2]);
    DDX_Control(pDX, IDC_STATIC_PLACE4, m_placeHolder[3]);
}
 
 
BEGIN_MESSAGE_MAP(CRobotCmdContainerDlg, CDialogEx)
    ON_BN_CLICKED(IDC_CHECK_ENABLE1, &CRobotCmdContainerDlg::OnCheckEnable1)
    ON_BN_CLICKED(IDC_CHECK_ENABLE2, &CRobotCmdContainerDlg::OnCheckEnable2)
    ON_BN_CLICKED(IDC_CHECK_ENABLE3, &CRobotCmdContainerDlg::OnCheckEnable3)
    ON_BN_CLICKED(IDC_CHECK_ENABLE4, &CRobotCmdContainerDlg::OnCheckEnable4)
END_MESSAGE_MAP()
 
 
// CRobotCmdContainerDlg 消息处理程序
 
BOOL CRobotCmdContainerDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // TODO:  在此添加额外的初始化
    constexpr int kPageCount = 4;
    constexpr int kColumns = 2;
    constexpr int kRows = 2;
    constexpr int kMargin = 10;
    constexpr int kCheckboxHeight = 20;
    constexpr int kBorderPadding = 2;
    constexpr int kButtonHeight = 40;
 
    // 子页面尺寸(从占位静态框取)
    CSize pageSize;
    CRect rcFrame[kPageCount];
 
    for (int i = 0; i < kPageCount; ++i)
    {
        m_placeHolder[i].GetWindowRect(&rcFrame[i]);
        ScreenToClient(&rcFrame[i]);
 
        // 记录页面尺寸(假设全部一样)
        if (i == 0) {
            pageSize = rcFrame[i].Size();
        }
 
        // 放置勾选框在静态框上方
        CRect rcCheck = rcFrame[i];
        rcCheck.bottom = rcCheck.top;
        rcCheck.top -= kCheckboxHeight;
        rcCheck.bottom += 2;
        rcCheck.left += 2;
        rcCheck.right = rcCheck.left + 60;
        m_chkEnable[i].MoveWindow(rcCheck);
        m_chkEnable[i].ShowWindow(SW_SHOW);
 
        // 创建子页面,缩进去一点避免遮边框
        CRect rcShrinked = rcFrame[i];
        rcShrinked.DeflateRect(kBorderPadding, kBorderPadding);
        m_pageRobotCmd[i].Create(IDD_PAGE_ROBOT_CMD, this);
        m_pageRobotCmd[i].MoveWindow(rcShrinked);
        m_pageRobotCmd[i].ShowWindow(SW_SHOW);
        m_pageRobotCmd[i].SetSequenceNo(i + 1);
        m_pageRobotCmd[i].SetControlsEnabled(i == 0);
 
        if (i == 0) {
            m_chkEnable[0].SetCheck(BST_CHECKED);
            m_chkEnable[0].EnableWindow(FALSE);
        }
    }
 
    // 计算整体宽高
    int totalWidth = kMargin + kColumns * (pageSize.cx + kMargin);
    int totalHeight = kMargin + kRows * (pageSize.cy + kCheckboxHeight + kMargin) + kButtonHeight;
 
    // 放置按钮
    CSize btnSize(80, 28);
    int btnSpacing = 10;
    int groupWidth = btnSize.cx * 2 + btnSpacing;
    int btnStartX = (totalWidth - groupWidth) / 2;
    int btnY = totalHeight - kButtonHeight + (kButtonHeight - btnSize.cy) / 2;
 
    if (CWnd* pOK = GetDlgItem(IDOK))
        pOK->MoveWindow(btnStartX - btnSpacing, btnY + btnSpacing, btnSize.cx, btnSize.cy);
 
    if (CWnd* pCancel = GetDlgItem(IDCANCEL))
        pCancel->MoveWindow(btnStartX + btnSize.cx + btnSpacing, btnY + btnSpacing, btnSize.cx, btnSize.cy);
 
    // 重新设置窗口尺寸(客户区 -> 含边框)
    CRect rcClient(0, 0, totalWidth, totalHeight);
    DWORD dwStyle = GetStyle();
    DWORD dwExStyle = GetExStyle();
    ::AdjustWindowRectEx(&rcClient, dwStyle, TRUE, dwExStyle);
    SetWindowPos(nullptr, 0, 0, rcClient.Width(), rcClient.Height(), SWP_NOMOVE | SWP_NOZORDER);
 
    return TRUE;  // return TRUE unless you set the focus to a control
    // 异常: OCX 属性页应返回 FALSE
}
 
void CRobotCmdContainerDlg::OnOK()
{
    m_cmdResult.clear();
    for (int i = 0; i < 4; ++i) {
        if (m_chkEnable[i].GetCheck()) {
            m_cmdResult.push_back(m_pageRobotCmd[i].GetRobotCmdParam());
        }
    }
 
    CDialogEx::OnOK();
}
 
void CRobotCmdContainerDlg::OnCheckEnable1() { ToggleEnable(0); }
void CRobotCmdContainerDlg::OnCheckEnable2() { ToggleEnable(1); }
void CRobotCmdContainerDlg::OnCheckEnable3() { ToggleEnable(2); }
void CRobotCmdContainerDlg::OnCheckEnable4() { ToggleEnable(3); }
 
void CRobotCmdContainerDlg::ToggleEnable(int index)
{
    constexpr int kMaxCount = 4;
    if (index < 0 || index >= kMaxCount) {
        return;
    }
 
    BOOL bChecked = m_chkEnable[index].GetCheck();
    m_pageRobotCmd[index].SetControlsEnabled(bChecked);
 
    if (bChecked) {
        for (int i = 1; i < index; ++i) {
            m_chkEnable[i].SetCheck(BST_CHECKED);
            m_pageRobotCmd[i].SetControlsEnabled(TRUE);
        }
    }
    else {
        for (int i = index; i < kMaxCount; ++i) {
            m_chkEnable[i].SetCheck(BST_UNCHECKED);
            m_pageRobotCmd[i].SetControlsEnabled(FALSE);
        }
    }
}