// CProjectPageRemoteEqs.cpp: 实现文件 // #include "stdafx.h" #include "Common.h" #include "BondEq.h" #include "CProjectPageRemoteEqs.h" #include "afxdialogex.h" // CProjectPageRemoteEqs 对话框 IMPLEMENT_DYNAMIC(CProjectPageRemoteEqs, CDialogEx) CProjectPageRemoteEqs::CProjectPageRemoteEqs(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_PROJECT_PAGE_REMOTEEQS, pParent) { m_crBkgnd = PROPAGE_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_pObserver = nullptr; } CProjectPageRemoteEqs::~CProjectPageRemoteEqs() { } void CProjectPageRemoteEqs::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_TREE1, m_treeRemoteEqs); } BEGIN_MESSAGE_MAP(CProjectPageRemoteEqs, CDialogEx) ON_WM_CTLCOLOR() ON_WM_DESTROY() ON_WM_SIZE() ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CProjectPageRemoteEqs::OnTvnSelchangedTree1) ON_MESSAGE(ID_MSG_TREE_CLICK_ITEM, OnTreeClickItem) ON_WM_TIMER() END_MESSAGE_MAP() // CProjectPageRemoteEqs 消息处理程序 void CProjectPageRemoteEqs::InitRxWindows() { /* code */ // 订阅数据 IRxWindows* pRxWindows = RX_GetRxWindows(); pRxWindows->enableLog(5); if (m_pObserver == NULL) { m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void { // onNext pAny->addRef(); int code = pAny->getCode(); if (RX_CODE_EQ_STATE_CHANGED == code) { BEQ::IRemoteEquipment* pRemoteEq; if (pAny->getPtrValue("ptr", (void*&)pRemoteEq)) { HTREEITEM hItem = getTreeItem(pRemoteEq); if (hItem != nullptr) { char szBuffer[256]; pRemoteEq->getName(szBuffer, 256); m_treeRemoteEqs.SetItemText(hItem, szBuffer); m_treeRemoteEqs.SetItemIcon(hItem, pRemoteEq->isConnected() ? m_hIconRemoteEqLight : m_hIconRemoteEqDark); } } } pAny->release(); }, [&]() -> void { // onComplete }, [&](IThrowable* pThrowable) -> void { // onErrorm pThrowable->printf(); }); theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread()) ->subscribe(m_pObserver); } } BOOL CProjectPageRemoteEqs::OnInitDialog() { CDialogEx::OnInitDialog(); CString strFilepath; strFilepath.Format(_T("%s\\Res\\remote_eq_light_32.ico"), theApp.m_strAppDir); m_hIconRemoteEqLight = (HICON)::LoadImage(AfxGetInstanceHandle(), strFilepath, IMAGE_ICON, 32, 32, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); strFilepath.Format(_T("%s\\Res\\remote_eq_dark_32.ico"), theApp.m_strAppDir); m_hIconRemoteEqDark = (HICON)::LoadImage(AfxGetInstanceHandle(), strFilepath, IMAGE_ICON, 32, 32, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); m_treeRemoteEqs.SetBkColor(PROPAGE_BACKGROUND_COLOR); m_treeRemoteEqs.SetItemHeight(50); SetTimer(1, 2000, nullptr); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CProjectPageRemoteEqs::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); if (nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkColor(m_crBkgnd); } if (m_hbrBkgnd == nullptr) { m_hbrBkgnd = CreateSolidBrush(m_crBkgnd); } return m_hbrBkgnd; } void CProjectPageRemoteEqs::OnDestroy() { CDialogEx::OnDestroy(); if (m_hbrBkgnd != nullptr) { ::DeleteObject(m_hbrBkgnd); } ASSERT(m_pObserver != NULL); m_pObserver->unsubscribe(); m_pObserver = NULL; } void CProjectPageRemoteEqs::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_TREE1) == nullptr) return; CRect rcClient; CWnd* pItem; GetClientRect(&rcClient); pItem = GetDlgItem(IDC_TREE1); pItem->MoveWindow(12, 12, rcClient.Width() - 24, rcClient.Height() - 24); } void CProjectPageRemoteEqs::OnTvnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); HTREEITEM hItem = pNMTreeView->itemNew.hItem; ASSERT(hItem); BEQ::IRemoteEquipment* pEquipment = (BEQ::IRemoteEquipment*)m_treeRemoteEqs.GetItemData(hItem); ASSERT(pEquipment); theApp.m_model.notifyPtr(RX_CODE_SELECT_EQ, pEquipment); *pResult = 0; } LRESULT CProjectPageRemoteEqs::OnTreeClickItem(WPARAM wParam, LPARAM lParam) { HTREEITEM hItem = (HTREEITEM)wParam; ASSERT(hItem); BEQ::IRemoteEquipment* pEquipment = (BEQ::IRemoteEquipment*)m_treeRemoteEqs.GetItemData(hItem); ASSERT(pEquipment); theApp.m_model.notifyPtr(RX_CODE_SELECT_EQ, pEquipment); return 0; } HTREEITEM CProjectPageRemoteEqs::getTreeItem(BEQ::IRemoteEquipment* pEquipment) { HTREEITEM hItem = m_treeRemoteEqs.GetChildItem(nullptr); while (hItem != nullptr) { BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)m_treeRemoteEqs.GetItemData(hItem); if (p == pEquipment) { return hItem; } hItem = m_treeRemoteEqs.GetNextSiblingItem(hItem); } return nullptr; } void CProjectPageRemoteEqs::OnTimer(UINT_PTR nIDEvent) { if (1 == nIDEvent) { KillTimer(1); InitRxWindows(); } CDialogEx::OnTimer(nIDEvent); }