// CPageGraph2.cpp: 实现文件 // #include "stdafx.h" #include "Servo.h" #include "CPageGraph2.h" #include "afxdialogex.h" // CPageGraph2 对话框 IMPLEMENT_DYNAMIC(CPageGraph2, CDialogEx) CPageGraph2::CPageGraph2(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_PAGE_GRAPH2, pParent) { m_pEqsGraphWnd = nullptr; m_crBkgnd = PAGE_GRPAH2_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; } CPageGraph2::~CPageGraph2() { } void CPageGraph2::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CPageGraph2, CDialogEx) ON_WM_CTLCOLOR() ON_WM_DESTROY() ON_WM_SIZE() ON_WM_TIMER() END_MESSAGE_MAP() // CPageGraph2 消息处理程序 BOOL CPageGraph2::OnInitDialog() { CDialogEx::OnInitDialog(); SetTimer(1, 2000, nullptr); // filter graph wnd EqsGraphListener listener; listener.onCheckConnectPin = [](PIN* pPin1, PIN* pPin2) -> bool { ASSERT(pPin1); ASSERT(pPin2); ASSERT(pPin1->pData); ASSERT(pPin2->pData); //int nRet = ((IPin*)pPin1->pData)->checkConnectPin((IPin*)pPin2->pData); //if (nRet >= 0) { // return true; //} return false; }; listener.onConnectPin = [](PIN* pPin1, PIN* pPin2) -> bool { ASSERT(pPin1); ASSERT(pPin2); ASSERT(pPin1->pData); ASSERT(pPin2->pData); //int nRet = ((IPin*)pPin1->pData)->connectPin((IPin*)pPin2->pData); //if (nRet >= 0) { // return true; //} return false; }; listener.onDisconnectPin = [](PIN* pPin) -> bool { ASSERT(pPin); ASSERT(pPin->pData); //int nRet = ((IPin*)pPin->pData)->disconnect(); //if (nRet >= 0) { // return true; //} return false; }; listener.onDeleteEqItem = [&](EQITEM* pItem) -> bool { ASSERT(pItem); ASSERT(pItem->pData); return true; // return _filterManager.unload((CFilter*)pFilter->pData) >= 0; }; listener.onEqItemPosChanged = [&](EQITEM* pItem, int x, int y) -> void { ASSERT(pItem); }; listener.onDblckEqItem = [&](EQITEM* pItem) -> bool { ASSERT(pItem); return true; }; listener.onRclickEqItem = [&](EQITEM* pItem) -> bool { ASSERT(pItem); return true; }; m_pEqsGraphWnd = CEqsGraphWnd::FromHandle(GetDlgItem(IDC_EQSGRAPHWND1)->m_hWnd); m_pEqsGraphWnd->SetBkgndColor(m_crBkgnd); m_pEqsGraphWnd->SetOnListener(listener); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CPageGraph2::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 CPageGraph2::OnDestroy() { CDialogEx::OnDestroy(); SaveEqsGraphData(); if (m_hbrBkgnd != nullptr) { ::DeleteObject(m_hbrBkgnd); } } void CPageGraph2::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_EQSGRAPHWND1) == nullptr) return; CRect rcClient; GetClientRect(&rcClient); GetDlgItem(IDC_EQSGRAPHWND1)->MoveWindow(0, 0, rcClient.Width(), rcClient.Height()); } #define INPIN 1 #define OUTPIN 2 void CPageGraph2::AddEqToGraphWnd(SERVO::CEquipment* pEquipment) { int x, y; GetItemDataFormIni(pEquipment->getName().c_str(), x, y); EQITEM* pItem = m_pEqsGraphWnd->AddItem(0, pEquipment->getName().c_str(), (DWORD_PTR)pEquipment); m_pEqsGraphWnd->SetItemType(pItem, ITEM_SMALL); if (x > 0 && y > 0) { m_pEqsGraphWnd->SetItemPos(pItem, x, y); } std::vector& inPins = pEquipment->getInputPins(); for (auto inPin : inPins) { m_pEqsGraphWnd->AddPin(pItem, INPIN, inPin->getName().c_str(), (DWORD_PTR)inPin); } std::vector& outPins = pEquipment->getOutputPins(); for (auto outPin : outPins) { m_pEqsGraphWnd->AddPin(pItem, OUTPIN, outPin->getName().c_str(), (DWORD_PTR)outPin); } } void CPageGraph2::OnTimer(UINT_PTR nIDEvent) { if (1 == nIDEvent) { KillTimer(1); std::list& eqs = theApp.m_model.m_master.getEquipmentList(); for (auto eq : eqs) { AddEqToGraphWnd(eq); } for (auto eq : eqs) { auto& pins = eq->getOutputPins(); for (auto pin : pins) { SERVO::CPin* pPin2 = pin->getConnectedPin(); if (pPin2 != nullptr) { m_pEqsGraphWnd->ConnectPin((DWORD_PTR)eq, (DWORD_PTR)pin, (DWORD_PTR)pPin2->getEquipment(), (DWORD_PTR)pPin2); } } } } CDialogEx::OnTimer(nIDEvent); } void CPageGraph2::SaveEqsGraphData() { char szFilepath[MAX_PATH]; CString strValue; sprintf_s(szFilepath, MAX_PATH, "%s\\EqsGraph.ini", (LPTSTR)(LPCTSTR)theApp.m_strAppDir); std::list& eqs = theApp.m_model.m_master.getEquipmentList(); for (auto item : eqs) { EQITEM* pEqItem = m_pEqsGraphWnd->GetItem((DWORD_PTR)item); strValue.Format(_T("%d"), pEqItem->rect.left); WritePrivateProfileString(item->getName().c_str(), _T("Left"), (LPTSTR)(LPCTSTR)strValue, szFilepath); strValue.Format(_T("%d"), pEqItem->rect.top); WritePrivateProfileString(item->getName().c_str(), _T("Top"), (LPTSTR)(LPCTSTR)strValue, szFilepath); } } void CPageGraph2::GetItemDataFormIni(const char* pszItemName, int& left, int& top) { char szFilepath[MAX_PATH]; CString strValue; sprintf_s(szFilepath, MAX_PATH, "%s\\EqsGraph.ini", (LPTSTR)(LPCTSTR)theApp.m_strAppDir); left = GetPrivateProfileInt(pszItemName, _T("Left"), 0, szFilepath); top = GetPrivateProfileInt(pszItemName, _T("Top"), 0, szFilepath); }