// CPanelMaster.cpp: 实现文件 // #include "stdafx.h" #include "Servo.h" #include "CPanelMaster.h" #include "afxdialogex.h" #include "Common.h" #include "VerticalLine.h" // CPanelMaster 对话框 IMPLEMENT_DYNAMIC(CPanelMaster, CDialogEx) CPanelMaster::CPanelMaster(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_PANEL_MASTER, pParent) { m_crBkgnd = PANEL_MASTER_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_nPanelWidth = 388; } CPanelMaster::~CPanelMaster() { } void CPanelMaster::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_TREE1, m_treeCtrl); } BEGIN_MESSAGE_MAP(CPanelMaster, CDialogEx) ON_WM_CTLCOLOR() ON_WM_DESTROY() ON_WM_SIZE() ON_NOTIFY(BYVERTICALLINE_MOVEX, IDC_LINE1, &CPanelMaster::OnVLineMoveX) ON_WM_TIMER() ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CPanelMaster::OnTvnSelchangedTree1) END_MESSAGE_MAP() // CPanelMaster 消息处理程序 int CPanelMaster::getPanelWidth() { return m_nPanelWidth; } BOOL CPanelMaster::OnInitDialog() { CDialogEx::OnInitDialog(); CVerticalLine* pLine1 = CVerticalLine::Hook(GetDlgItem(IDC_LINE1)->GetSafeHwnd()); pLine1->SetBkgndColor(RGB(225, 225, 225)); pLine1->SetLineColor(RGB(198, 198, 198)); pLine1->EnableResize(); // 读取面板宽 CString strIniFile; strIniFile.Format(_T("%s\\%s.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir, (LPTSTR)(LPCTSTR)theApp.m_strAppFile); m_nPanelWidth = GetPrivateProfileInt(_T("App"), _T("MasterPanelWidth"), int((double)GetSystemMetrics(SM_CXSCREEN) * 0.25), (LPTSTR)(LPCTSTR)strIniFile); // treectrl m_treeCtrl.SetBkColor(PANEL_MASTER_BACKGROUND_COLOR); m_treeCtrl.SetItemHeight(28); SetTimer(1, 2000, nullptr); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CPanelMaster::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 CPanelMaster::OnDestroy() { CDialogEx::OnDestroy(); if (m_hbrBkgnd != nullptr) { ::DeleteObject(m_hbrBkgnd); } } void CPanelMaster::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_LINE1) == nullptr) return; CWnd* pItem; CRect rcClient; GetClientRect(&rcClient); pItem = GetDlgItem(IDC_LINE1); pItem->MoveWindow(rcClient.right - 3, 0, 3, rcClient.Height()); m_treeCtrl.MoveWindow(5, 5, rcClient.Width() - 13, rcClient.Height() - 10); } #define MASTER_PANEL_MIN_WIDTH 88 #define MASTER_PANEL_MAX_WIDTH 588 void CPanelMaster::OnVLineMoveX(NMHDR* nmhdr, LRESULT* result) { BYVERTICALLINE_NMHDR* pNmhdrex = (BYVERTICALLINE_NMHDR*)nmhdr; int x = pNmhdrex->dwData; m_nPanelWidth += x; m_nPanelWidth = max(m_nPanelWidth, MASTER_PANEL_MIN_WIDTH); m_nPanelWidth = min(m_nPanelWidth, MASTER_PANEL_MAX_WIDTH); GetParent()->SendMessage(ID_MSG_PANEL_RESIZE, m_nPanelWidth, 0); CString strIniFile, strValue; strIniFile.Format(_T("%s\\%s.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir, (LPTSTR)(LPCTSTR)theApp.m_strAppFile); strValue.Format(_T("%d"), m_nPanelWidth); WritePrivateProfileString(_T("App"), _T("MasterPanelWidth"), (LPTSTR)(LPCTSTR)strValue, (LPTSTR)(LPCTSTR)strIniFile); OnSize(0, 0, 0); * result = 0; } void CPanelMaster::OnTimer(UINT_PTR nIDEvent) { if (1 == nIDEvent) { KillTimer(1); loadEquipmentList(); } CDialogEx::OnTimer(nIDEvent); } void CPanelMaster::loadEquipmentList() { HTREEITEM hItemMaster = m_treeCtrl.InsertItem("Master"); std::list& eqs = theApp.m_model.m_master.getEquipmentList(); for (auto item : eqs) { HTREEITEM hItemEq = m_treeCtrl.InsertItem(item->getName().c_str(), hItemMaster); m_treeCtrl.SetItemData(hItemEq, (DWORD_PTR)item); loadSteps(item, hItemEq); m_treeCtrl.Expand(hItemEq, TVE_EXPAND); } m_treeCtrl.Expand(hItemMaster, TVE_EXPAND); } void CPanelMaster::loadSteps(SERVO::CEquipment* pEquipment, HTREEITEM hItemEq) { std::map& steps = pEquipment->getSteps(); for (auto item : steps) { HTREEITEM hStep = m_treeCtrl.InsertItem(item.second->getName().c_str(), hItemEq); m_treeCtrl.SetItemData(hStep, (DWORD_PTR)item.second); } } void CPanelMaster::OnTvnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); HTREEITEM hItem = pNMTreeView->itemNew.hItem; int nLevel = GetTreeItemLevel(hItem); if (nLevel == 2) { SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)m_treeCtrl.GetItemData(hItem); theApp.m_model.notifyPtr(RX_CODE_SELECT_EQUIPMENT, pEquipment); } else if (nLevel == 3) { SERVO::CStep* pStep = (SERVO::CStep*)m_treeCtrl.GetItemData(hItem); theApp.m_model.notifyPtr(RX_CODE_SELECT_STEP, pStep); } *pResult = 0; } int CPanelMaster::GetTreeItemLevel(HTREEITEM hItem) { int nLevel = 0; HTREEITEM hTemp = hItem; while (hTemp != nullptr) { hTemp = m_treeCtrl.GetParentItem(hTemp); nLevel++; } return nLevel; }