// InsertDeviceDlg.cpp : 实现文件 // #include "stdafx.h" #include "RunTest.h" #include "IniReader.h" #include "InsertDeviceDlg.h" #include ".\insertdevicedlg.h" // CInsertDeviceDlg 对话框 IMPLEMENT_DYNAMIC(CInsertDeviceDlg, CDialog) CInsertDeviceDlg::CInsertDeviceDlg(CWnd* pParent /*=NULL*/) : CDialog(CInsertDeviceDlg::IDD, pParent) { m_bModify = FALSE; m_strAddr = ""; m_strName = ""; m_strPort = ""; m_nStarAddr = -1; m_nEndAddr = -1; m_bMulti = FALSE; } CInsertDeviceDlg::~CInsertDeviceDlg() { } void CInsertDeviceDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_TREE1, m_CtrlTree); DDX_Control(pDX, IDCANCLE, m_btnClose); DDX_Control(pDX, IDOK, m_btnAdd); } BEGIN_MESSAGE_MAP(CInsertDeviceDlg, CDialog) ON_BN_CLICKED(IDOK, OnBnClickedOk) ON_BN_CLICKED(IDCANCLE, OnBnClickedCancle) ON_BN_CLICKED(BTN_MODIFY, OnBnClickedModify) // ON_NOTIFY(TVN_ITEMEXPANDED, IDC_TREE1, &CInsertDeviceDlg::OnTvnItemexpandedTree1) END_MESSAGE_MAP() // CInsertDeviceDlg 消息处理程序 BOOL CInsertDeviceDlg::OnInitDialog() { CDialog::OnInitDialog(); InitSTButtons(); ////////////////// CString strPath; TCHAR szFull[_MAX_PATH]; TCHAR szDrive[_MAX_DRIVE]; TCHAR szDir[_MAX_DIR]; ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR)); _tsplitpath(szFull, szDrive, szDir, NULL, NULL); _tcscpy(szFull, szDrive); _tcscat(szFull, szDir); strPath = CString(szFull); CString strIni; strIni.Format("%s\\config.ini",strPath); CIniReader IniReader(strIni); //m_strDepotPath = IniReader.GetKeyValueText("name","DeviceLibrary"); //m_strRutDepotPath = IniReader.GetKeyValueText("name","RunUnitLibrary"); m_strDepotPath.Format("%s\\%s",strPath,IniReader.GetKeyValueText("name","DeviceLibrary")); m_strRutDepotPath.Format("%s\\%s",strPath,IniReader.GetKeyValueText("name","RunUnitLibrary")); // TODO: 在此添加额外的初始化 // [3/29/2013 Zero.t] m_imgList.Create(IDB_BITMAP1,16, 6, RGB(255,255,255)); m_CtrlTree.SetImageList(&m_imgList,TVSIL_NORMAL); hRunRootItem = m_CtrlTree.InsertItem("当前运行时设备库设备",2,2); InitTreeCtrlInfo(); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } void CInsertDeviceDlg::InitSTButtons() { short shBtnColor = 30; m_btnClose.SetIcon(IDI_ICON4); m_btnClose.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor); m_btnClose.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0)); m_btnAdd.SetIcon(IDI_ICON5); m_btnAdd.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor); m_btnAdd.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0)); /*m_btnRunTest.SetIcon(IDI_ICON3); m_btnRunTest.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor); m_btnRunTest.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));*/ } void CInsertDeviceDlg::InitTreeCtrlInfo() { CMarkup xml; if ( !xml.Load(m_strDepotPath) ) { LOG4C((LOG_NOTICE,"设备库不存在")); return; } xml.FindElem(); xml.IntoElem(); int nCount = 0; BOOL bNoDevice = TRUE; BOOL bNoFactory = TRUE; BOOL bNoType = TRUE; while( xml.FindElem("object_depot")) { CString strDevice = xml.GetAttrib("name"); xml.FindChildElem("document"); CString strFactory = xml.GetChildAttrib("Factory"); CString strType = xml.GetChildAttrib("deviceType"); STTreeInfo tTreeInfo = {0}; vector ::iterator it; for ( it = vtSTTreeInfo.begin(); it != vtSTTreeInfo.end(); it++ ) { tTreeInfo = *it; if ( tTreeInfo.strType == strType && tTreeInfo.hType != NULL ) { bNoType = FALSE; STFactory tFactory = {0}; vector ::iterator itFactory; for ( itFactory = tTreeInfo.vtFactory.begin(); itFactory != tTreeInfo.vtFactory.end(); itFactory++) { tFactory = *itFactory; if ( tFactory.strFactory == strFactory && tFactory.hFactory != NULL ) { bNoFactory = FALSE; STDevice tDevice = {0}; vector ::iterator itDevice; for( itDevice = tFactory.vtDevice.begin(); itDevice != tFactory.vtDevice.end(); itDevice++) { tDevice = *itDevice; if ( tDevice.strDevice == strDevice && tDevice.hDevice != NULL ) { bNoDevice = FALSE; break; } } if ( bNoDevice ) { tDevice.strDevice = strDevice; tDevice.hDevice = m_CtrlTree.InsertItem(strDevice,3,3,tFactory.hFactory); tFactory.vtDevice.push_back(tDevice); *itFactory = tFactory; } else { bNoDevice = TRUE; } break; } } if ( bNoFactory ) { STFactory st_Factory = {0}; st_Factory.strFactory = strFactory; st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,0,1,tTreeInfo.hType); //st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,tTreeInfo.hType); STDevice stDevice = {0}; stDevice.strDevice = strDevice; stDevice.hDevice = m_CtrlTree.InsertItem(strDevice,3,3,st_Factory.hFactory); st_Factory.vtDevice.push_back(stDevice); tTreeInfo.vtFactory.push_back(st_Factory); }else { bNoFactory = TRUE; } *it = tTreeInfo; break; } } if ( bNoType ) { STTreeInfo st_TreeInfo={0}; st_TreeInfo.strType = strType; st_TreeInfo.hType = m_CtrlTree.InsertItem(strType,0,1,hRunRootItem); STFactory st_Factory = {0}; st_Factory.strFactory = strFactory; st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,0,1,st_TreeInfo.hType); //st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,st_TreeInfo.hType); STDevice st_Device = {0}; st_Device.strDevice = strDevice; st_Device.hDevice = m_CtrlTree.InsertItem(strDevice,3,3,st_Factory.hFactory); st_Factory.vtDevice.push_back(st_Device); st_TreeInfo.vtFactory.push_back(st_Factory); vtSTTreeInfo.push_back(st_TreeInfo); } else { bNoType = TRUE; } nCount++; } CString strCount; strCount.Format("当前可使用设备总数:%d",nCount); SetDlgItemText(TX_COUNT,strCount); xml.OutOfElem(); } void CInsertDeviceDlg::OnBnClickedOk()//添加设备; { int iPort,iAddr,iRate,iDataBit, iStopBit,iParity, iRespTime,iInterval; iPort = GetDlgItemInt(IDC_EDIT2); iRate = GetDlgItemInt(IDC_EDIT4); iDataBit = GetDlgItemInt(IDC_EDIT5); iStopBit = GetDlgItemInt(IDC_EDIT6); iParity = GetDlgItemInt(IDC_EDIT7); iRespTime = GetDlgItemInt(IDC_EDIT8); iInterval = GetDlgItemInt(IDC_EDIT9); //GetDlgItemText(IDC_EDIT2,m_strPort); m_strPort.Format("%d",iPort); CString strAddr ; GetDlgItemText(IDC_EDIT3,strAddr); int nLenth = strAddr.GetLength(); int nIndex = strAddr.Find("-"); CObjectdepot tag; tag.SetDevicesDepotPath(m_strDepotPath); tag.SetRuntimeDevicesDepotPath(m_strRutDepotPath); if ( nIndex == -1 ) { iAddr = GetDlgItemInt(IDC_EDIT3); if ( iPort == 0 || iAddr == 0 ) { MessageBox("请完整填写串口属性"); m_bModify = FALSE; return; } if( !tag.InsertSubElement(iPort,iAddr,m_strName.GetBuffer()) ) { m_strName.ReleaseBuffer(); MessageBox("已存或库错误"); m_bModify = FALSE; return; } m_strAddr.Format("%d",iAddr); } else { if ( iPort == 0 || strAddr == "" ) { MessageBox("请完整填写串口属性"); m_bModify = FALSE; return; } CString str = strAddr.Left(nIndex); m_nStarAddr = atoi(str.GetBuffer()); str.ReleaseBuffer(); str = strAddr.Right(nLenth - nIndex -1); m_nEndAddr = atoi(str.GetBuffer()); str.ReleaseBuffer(); m_bMulti = TRUE; //int nCount = iAddr2 - iAddr; for ( int i = m_nStarAddr; i <= m_nEndAddr; i++) { if( !tag.InsertSubElement(iPort,i,m_strName.GetBuffer()) ) { m_strName.ReleaseBuffer(); MessageBox("已存在"); m_bModify = FALSE; m_nEndAddr = i-1; break; } Sleep(50); } } if ( m_bModify ) { tag.ModifySubPhysical(iPort,iRate,iDataBit,iStopBit,iParity,iRespTime,iInterval); } OnOK(); } void CInsertDeviceDlg::OnBnClickedCancle() { // TODO: 在此添加控件通知处理程序代码 OnCancel(); } void CInsertDeviceDlg::OnBnClickedModify() { CString strBName = ""; GetDlgItemText(BTN_MODIFY,strBName); if ( strBName == "修改属性" ) { ((CWnd*)(GetDlgItem(IDC_EDIT4)))->EnableWindow(); ((CWnd*)(GetDlgItem(IDC_EDIT5)))->EnableWindow(); ((CWnd*)(GetDlgItem(IDC_EDIT6)))->EnableWindow(); ((CWnd*)(GetDlgItem(IDC_EDIT7)))->EnableWindow(); ((CWnd*)(GetDlgItem(IDC_EDIT8)))->EnableWindow(); ((CWnd*)(GetDlgItem(IDC_EDIT9)))->EnableWindow(); SetDlgItemText(BTN_MODIFY,"应用属性"); GetDlgItemText(BTN_MODIFY,strBName); }else if( strBName == "应用属性" ) { ((CWnd*)(GetDlgItem(IDC_EDIT4)))->EnableWindow(FALSE); ((CWnd*)(GetDlgItem(IDC_EDIT5)))->EnableWindow(FALSE); ((CWnd*)(GetDlgItem(IDC_EDIT6)))->EnableWindow(FALSE); ((CWnd*)(GetDlgItem(IDC_EDIT7)))->EnableWindow(FALSE); ((CWnd*)(GetDlgItem(IDC_EDIT8)))->EnableWindow(FALSE); ((CWnd*)(GetDlgItem(IDC_EDIT9)))->EnableWindow(FALSE); SetDlgItemText(BTN_MODIFY,"修改属性"); m_bModify = TRUE; } } BOOL CInsertDeviceDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { if ( !::IsWindow(m_CtrlTree)) { return CDialog::OnNotify(wParam, lParam, pResult); } LPNMHDR pNmhdr = (LPNMHDR)lParam; int idCtrl = LOWORD(wParam); // 获取控件ID; // 如果消息对象是该资源(树); if( idCtrl == IDC_TREE1 ) { CPoint point; ::GetCursorPos(&point); m_CtrlTree.ScreenToClient(&point); switch( pNmhdr->code ) { case NM_CLICK: // 单击; { // Get Click item's UINT uFlags; HTREEITEM hItem = m_CtrlTree.HitTest(point, &uFlags); if((hItem != NULL) && (TVHT_ONITEM & uFlags)) { m_CtrlTree.SelectItem(hItem); } // [3/27/2013 Zero.t] SetDlgItemText(IDC_EDIT2,""); SetDlgItemText(IDC_EDIT3,""); SetDlgItemText(IDC_EDIT4,""); SetDlgItemText(IDC_EDIT5,""); SetDlgItemText(IDC_EDIT6,""); SetDlgItemText(IDC_EDIT7,""); SetDlgItemText(IDC_EDIT8,""); SetDlgItemText(IDC_EDIT9,""); GetDlgItem(BTN_MODIFY)->EnableWindow(FALSE);//修改 GetDlgItem(IDOK)->EnableWindow(FALSE);//添加 return true;//Already process the notify } case NM_RCLICK: // 右击; { // Get RightClick item's UINT uFlags; HTREEITEM hItem = m_CtrlTree.HitTest(point, &uFlags); m_CtrlTree.SelectItem(hItem); if((hItem != NULL) && (TVHT_ONITEM & uFlags)) { GetCursorPos(&m_mousePoint); // 保存当前鼠标位置; HTREEITEM hSelectedItem = m_CtrlTree.GetSelectedItem(); if ( !m_CtrlTree.ItemHasChildren(hSelectedItem)) { m_strName = m_CtrlTree.GetItemText(hSelectedItem); #if 1 CObjectdepot tObjectdepot; tObjectdepot.SetDevicesDepotPath(m_strDepotPath); tObjectdepot.SetRuntimeDevicesDepotPath(m_strRutDepotPath); STDepotObject *pDepotObject = tObjectdepot.LookupElement(m_strName) ; if ( pDepotObject == NULL) return FALSE; SetDlgItemInt(IDC_EDIT4,pDepotObject->st_depotphy.iRate); SetDlgItemInt(IDC_EDIT5,pDepotObject->st_depotphy.iDBit); SetDlgItemInt(IDC_EDIT6,pDepotObject->st_depotphy.iSBit); SetDlgItemInt(IDC_EDIT7,pDepotObject->st_depotphy.iPaty); SetDlgItemInt(IDC_EDIT8,pDepotObject->st_depotphy.iRsTm); SetDlgItemInt(IDC_EDIT9,pDepotObject->st_depotphy.iIntl); Sleep(50); if ( pDepotObject ) { delete pDepotObject; pDepotObject = NULL; } #endif } } return true;//Already process the notify } case NM_DBLCLK: // 左键 双击; { UINT uFlags; HTREEITEM hItem = m_CtrlTree.HitTest(point, &uFlags); m_CtrlTree.SelectItem(hItem); if((hItem != NULL) && (TVHT_ONITEM & uFlags)) { GetCursorPos(&m_mousePoint); // 保存当前鼠标位置; HTREEITEM hSelectedItem = m_CtrlTree.GetSelectedItem(); if ( !m_CtrlTree.ItemHasChildren(hSelectedItem)) { m_strName = m_CtrlTree.GetItemText(hSelectedItem); CObjectdepot tObjectdepot; tObjectdepot.SetDevicesDepotPath(m_strDepotPath); tObjectdepot.SetRuntimeDevicesDepotPath(m_strRutDepotPath); STDepotObject *pDepotObject = tObjectdepot.LookupElement(m_strName); if ( pDepotObject == NULL) return FALSE; SetDlgItemInt(IDC_EDIT4,pDepotObject->st_depotphy.iRate); SetDlgItemInt(IDC_EDIT5,pDepotObject->st_depotphy.iDBit); SetDlgItemInt(IDC_EDIT6,pDepotObject->st_depotphy.iSBit); SetDlgItemInt(IDC_EDIT7,pDepotObject->st_depotphy.iPaty); SetDlgItemInt(IDC_EDIT8,pDepotObject->st_depotphy.iRsTm); SetDlgItemInt(IDC_EDIT9,pDepotObject->st_depotphy.iIntl); UpdateData(); if ( pDepotObject ) { //ZeroMemory(pDepotObject,sizeof(STDepotObject));// ★★暂时未知什么原因,要在delete前ZeroMemory; delete pDepotObject; pDepotObject = NULL; } //(CWnd*)GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);//停止 GetDlgItem(BTN_MODIFY)->EnableWindow(TRUE);//修改 GetDlgItem(IDOK)->EnableWindow();//添加 } } return true; } } return false; // no match } return CDialog::OnNotify(wParam, lParam, pResult); }