// 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& 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); } } }