// CEquipmentDlg.cpp: 实现文件 // #include "stdafx.h" #include "Servo.h" #include "CHMPropertyDlg.h" #include "afxdialogex.h" #include "HmTab.h" // CEquipmentDlg 对话框 IMPLEMENT_DYNAMIC(CHMPropertyDlg, CDialogEx) CHMPropertyDlg::CHMPropertyDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_DIALOG_PROPERTY, pParent) { m_crBkgnd = APPDLG_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_nWndWidth = 0; m_nWndHeight = 0; } CHMPropertyDlg::CHMPropertyDlg(const char* pszTitle, int width, int height) : CDialogEx(IDD_DIALOG_PROPERTY, nullptr) { m_crBkgnd = APPDLG_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_nWndWidth = width; m_nWndHeight = height; m_strTitle = pszTitle; } CHMPropertyDlg::~CHMPropertyDlg() { } void CHMPropertyDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CHMPropertyDlg, CDialogEx) ON_WM_CTLCOLOR() ON_WM_DESTROY() ON_WM_SIZE() ON_NOTIFY(BYHMTAB_SEL_CHANGED, IDC_TAB1, &CHMPropertyDlg::OnTabSelChanged) ON_BN_CLICKED(IDOK, &CHMPropertyDlg::OnBnClickedOk) ON_BN_CLICKED(IDC_BUTTON_APPLY, &CHMPropertyDlg::OnBnClickedButtonApply) END_MESSAGE_MAP() // CEquipmentDlg 消息处理程序 void CHMPropertyDlg::addPage(CHMPropertyPage* pPage, const char* pszName) { pPage->SetWindowTextA(pszName); m_pages.push_back(pPage); } BOOL CHMPropertyDlg::OnInitDialog() { CDialogEx::OnInitDialog(); if (m_nWndWidth != 0 && m_nWndHeight != 0) { SetWindowPos(nullptr, 0, 0, m_nWndWidth, m_nWndHeight, SWP_NOMOVE); CenterWindow(); } SetWindowText(m_strTitle); // Tab CString strTitle; CHmTab* 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_pTab->AddItem(strTitle, i == m_pages.size() - 1); } m_pTab->SetCurSel(0); ShowChildPage(0); Resize(); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CHMPropertyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); if (nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkColor(m_crBkgnd); pDC->SetTextColor(RGB(0, 0, 0)); } if (m_hbrBkgnd == nullptr) { m_hbrBkgnd = CreateSolidBrush(m_crBkgnd); } return m_hbrBkgnd; } void CHMPropertyDlg::OnDestroy() { CDialogEx::OnDestroy(); for (auto item : m_pages) { item->DestroyWindow(); delete item; } if (m_hbrBkgnd != nullptr) { ::DeleteObject(m_hbrBkgnd); } } void CHMPropertyDlg::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_TAB1) == nullptr) return; Resize(); } void CHMPropertyDlg::Resize() { CWnd* pItem; CRect rcClient, rcItem; GetClientRect(&rcClient); int x2, y, y2; x2 = rcClient.right - 12; y = 0; y2 = rcClient.bottom - 12; pItem = GetDlgItem(IDC_TAB1); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(0, y, rcClient.Width(), rcItem.Height()); y += rcItem.Height(); // [确定]按钮 pItem = GetDlgItem(IDOK); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x2 - rcItem.Width(), y2 - rcItem.Height(), rcItem.Width(), rcItem.Height()); x2 -= rcItem.Width(); x2 -= 8; // [应用]按钮 pItem = GetDlgItem(IDC_BUTTON_APPLY); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x2 - rcItem.Width(), y2 - rcItem.Height(), rcItem.Width(), rcItem.Height()); y2 -= rcItem.Height(); y2 -= 12; // 分隔线 pItem = GetDlgItem(IDC_LINE1); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(0, y2, rcClient.Width(), rcItem.Height()); for (auto item : m_pages) { item->MoveWindow(0, y, rcClient.Width(), y2 - y); } } void CHMPropertyDlg::OnTabSelChanged(NMHDR* nmhdr, LRESULT* result) { BYHMTAB_NMHDR* pNmhdrex = (BYHMTAB_NMHDR*)nmhdr; ShowChildPage((int)pNmhdrex->dwData); *result = 0; } void CHMPropertyDlg::ShowChildPage(unsigned int index) { ASSERT(index < m_pages.size()); for (int i = 0; i < m_pages.size(); i++) { m_pages[i]->ShowWindow(i == index ? SW_SHOW : SW_HIDE); } } void CHMPropertyDlg::SetWindowSize(int width, int height) { m_nWndWidth = width; m_nWndHeight = height; } void CHMPropertyDlg::OnBnClickedOk() { for (auto item : m_pages) { item->OnApply(); } CDialogEx::OnOK(); } void CHMPropertyDlg::OnBnClickedButtonApply() { for (int i = 0; i < m_pages.size(); i++) { if (m_pages[i]->IsWindowVisible()) { m_pages[i]->OnApply(); } } }