⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memoryeditorlist.cpp

📁 mod_RSsim
💻 CPP
📖 第 1 页 / 共 2 页
字号:
         if (pGlobalDialog->InPlantSimulation()) 
            pGlobalDialog->GetRegisterName(m_memAreaDisplayed, absItemNumber, registerName);

      if (htInfo.iSubItem)
      {
         // if U double-click on a digital, we just toggle it for U quickly
         if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
         {
            if (17 == htInfo.iSubItem)
            {
            DWORD registerValue;
            
               absItemNumber = (htInfo.iItem * (BITS_IN_WORD)) + 0;
               registerValue = pGlobalDialog->GetWordValue(m_memAreaDisplayed, 
                                                           absItemNumber, 
                                                           BITS_IN_WORD);
               descrFormat.Format("Edit %s value at %s", ViewerFormatNames[VIEWFORMAT_HEX], GetAddressFormat());
               description.Format(descrFormat, memPrefix*10000 + absItemNumber);

#ifdef _UI_DEBUGGING
      OutputDebugString(description);
#endif
               {
               // construct the "value editor" dialog to edit "16-bits" of I/O
               CEditMemoryDlg memEditor(PLCViewerFormats[VIEWFORMAT_HEX], 
                                        registerName, 
                                        registerValue, 
                                        VIEWFORMAT_HEX, 
                                        description);
         
                  if (IDOK==memEditor.DoModal())
                  {
                     // unpack the value again into bits
                     registerValue = (DWORD)memEditor.m_value;
                     pGlobalDialog->SetWordValue(m_memAreaDisplayed, 
                                                 absItemNumber, 
                                                 registerValue, 
                                                 BITS_IN_WORD);
                  }
               }
               // re-paint the row of digitals
               RedrawItems(htInfo.iItem, htInfo.iItem);
            }
            else
            {
               descrFormat.Format("Toggle digital at %s",  GetAddressFormat());
               description.Format(descrFormat, (IsAddressFormatHex()? 0 : memPrefix*10000) + absItemNumber);

#ifdef _UI_DEBUGGING
      OutputDebugString(description);
#endif
               if (PLCMemory[m_memAreaDisplayed][absItemNumber])
                  PLCMemory.SetAt(m_memAreaDisplayed, absItemNumber, 0);
               else
                  PLCMemory.SetAt(m_memAreaDisplayed, absItemNumber, 1);
               // re-paint the digital
               RedrawItems(htInfo.iItem, htInfo.iItem);
            }
         }
         else  // Edit the item in a dialog
         {
         DWORD    maximumOffset = pGlobalDialog->GetPLCMemoryLimit(m_memAreaDisplayed);

            if (absItemNumber < (LONG)maximumOffset/*MAX_MOD_MEMWORDS*/)
            {
            CEditMemoryDlg memEditor(GetViewFormatString(), 
                                     registerName, 
                                     pGlobalDialog->GetPLCMemoryValue(m_memAreaDisplayed, absItemNumber, GetViewFormat()), 
                                     GetViewFormat(), 
                                     description);
         
#ifdef _UI_DEBUGGING
      OutputDebugString(description);
#endif
               if (IDOK==memEditor.DoModal())
               {
                  // fetch the item based on it's display formatting
                  PLCMemory.SetAt(m_memAreaDisplayed,
                                  absItemNumber, 
                                  (WORD)memEditor.m_value);
                  // invalidate the item itself
                  RedrawItems(htInfo.iItem, htInfo.iItem);
               }
            }
         }
      }
      else
      {
         // User clicked in the addresses column.
         if (pGlobalDialog->IsAddressFormatHex())
            pGlobalDialog->OnViewdec();
         else
            pGlobalDialog->OnViewhex();
      }
   }
   else
      sprintf(position, "Clicked at %d, %d", rect.left, rect.top);
   //MessageBox(position);	

	*pResult = 0;
} // OnDblclk

// ---------------------------------- GetDataColumnCount ------------------------------
int CMemoryEditorList::GetDataColumnCount() 
{
int memoryViewWidth = m_displayableWidth;

   return(memoryViewWidth);
}

// ------------------------------------ OnGetdispinfo ---------------------------------
void CMemoryEditorList::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
	// TODO: Add your control notification handler code here
	
   // call my internal function
	*pResult = GetListViewDISPINFO((LPARAM)pNMHDR);
} // OnGetdispinfo


// --------------------------- GetListViewDISPINFO -------------------------
//
LRESULT CMemoryEditorList::GetListViewDISPINFO(LPARAM lParam)
{
LPNMHDR  lpnmh = (LPNMHDR) lParam;
HWND     hwndListView = m_hWnd;
DWORD    absItemNumber;
DWORD    memoryViewWidth;
DWORD    registerValue = 0;
CString  valueFormat;
float    *pFloat32value;
int      viewDataFormat = GetViewFormat();  // fetch user selection
DWORD    maximumOffset = pGlobalDialog->GetPLCMemoryLimit(m_memAreaDisplayed);

LV_DISPINFO *lpdi = (LV_DISPINFO *)lParam;
TCHAR szString[MAX_PATH];
   

   if(lpdi->item.iSubItem)
   {
      // return a sub-item (the data values)
      if(lpdi->item.mask & LVIF_TEXT)
      {
         memoryViewWidth = m_displayableWidth;
         if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
         {
            memoryViewWidth--;
            // determine the bit #
            if (17 == lpdi->item.iSubItem)
            {  // the totals column
               viewDataFormat = VIEWFORMAT_WORD;// always display in HEX

               absItemNumber = (lpdi->item.iItem * (BITS_IN_WORD)) + 0;
               registerValue = pGlobalDialog->GetWordValue(m_memAreaDisplayed, 
                                                           absItemNumber, 
                                                           BITS_IN_WORD);
               valueFormat = PLCViewerFormats[VIEWFORMAT_HEX];       // BIT totals in HEX
            }
            else
            { // individual bit #
               absItemNumber = (lpdi->item.iItem * BITS_IN_WORD) + lpdi->item.iSubItem-1;
               if (absItemNumber < maximumOffset)
               {
                  registerValue = PLCMemory[m_memAreaDisplayed][absItemNumber];
                  valueFormat = PLCViewerFormats[VIEWFORMAT_DECIMAL];   // bits are 0 or 1
               }
            }
         }
         else
         {
            valueFormat = GetViewFormatString();
            // determine the register #
            absItemNumber = (lpdi->item.iItem * (memoryViewWidth)) + lpdi->item.iSubItem-1;
            if (absItemNumber < maximumOffset/*MAX_MOD_MEMWORDS*/)
               registerValue = pGlobalDialog->GetPLCMemoryValue(m_memAreaDisplayed, absItemNumber, GetViewFormat());
            if ((m_cloneAddressing)&&(GetViewFormat() == VIEWFORMAT_FLOAT))
               SwopWords(&registerValue);
         }
         
         if (absItemNumber < maximumOffset/*MAX_MOD_MEMWORDS*/)
         {
            
            // print the contents
            switch (viewDataFormat)
            {
            case VIEWFORMAT_DECIMAL:
               sprintf(szString, valueFormat, (short)registerValue);
               break;
            case VIEWFORMAT_HEX: 
            case VIEWFORMAT_WORD: 
               sprintf(szString, valueFormat, registerValue);
               break;
            case VIEWFORMAT_DWORD:
               sprintf(szString, valueFormat, registerValue);
               break;
            case VIEWFORMAT_LONG:
               sprintf(szString, valueFormat, (LONG)registerValue);
               break;
            case VIEWFORMAT_FLOAT:
               pFloat32value = (float*)&registerValue;
               sprintf(szString, valueFormat, *pFloat32value);
               break;
            default:
               ASSERT(0);
               break;
            }
            lstrcpyn(lpdi->item.pszText, szString, lpdi->item.cchTextMax);
         }
         else
         {
            lstrcpyn(lpdi->item.pszText, "--", lpdi->item.cchTextMax);
         }
      }
   }
   else
   {
   CString addrFormat;
      
      // return address column values
      if(lpdi->item.mask & LVIF_TEXT)
      {
      DWORD addrEndItem, offset = 0;

         absItemNumber = lpdi->item.iItem * (m_displayableWidth) +1 ;//modbus addresses start at 1 not zero
         if ((PROTOCOL_SELABMASTER232 == pGlobalDialog->m_selectedProtocol) ||
             (PROTOCOL_SELAB232 == pGlobalDialog->m_selectedProtocol))
         { // Allen-Bradley
            offset = 0;
         }
         else
         { // modbus
            if (!IsAddressFormatHex()) // decimal address format
            {
               if (absItemNumber>9999) //5-digit offset
                  offset = PLCMemPrefixes[m_memAreaDisplayed]*100000;
               else
                  offset = PLCMemPrefixes[m_memAreaDisplayed]*10000;
            }
         }
         addrFormat.Format("%s-%s", GetAddressFormat(), GetAddressFormat());

         // work out the last address on this line/row
         addrEndItem = absItemNumber + m_displayableWidth-1;
         if (addrEndItem > maximumOffset/*MAX_MOD_MEMWORDS*/)
             addrEndItem = maximumOffset/*MAX_MOD_MEMWORDS*/; //range check it
         
         sprintf(szString, addrFormat, offset+absItemNumber, offset+addrEndItem);
         lstrcpyn(lpdi->item.pszText, szString, lpdi->item.cchTextMax);
      }

      if(lpdi->item.mask & LVIF_IMAGE)
      {
         lpdi->item.iImage = 0;
      }

      if(lpdi->item.mask & LVIF_INDENT)
      {
         lpdi->item.iIndent = 0;
      }
   }
   
   return 0;
} // GetListViewDISPINFO

// ------------------------------- OnCustomdrawMyList ---------------------
// owner-draw code
void CMemoryEditorList::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
//int      viewDataFormat = GetViewFormat();


    // Take the default processing unless we set this to something else below.
    *pResult = CDRF_DODEFAULT;

    // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.

    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        *pResult = CDRF_NOTIFYITEMDRAW;
        }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        // This is the notification message for an item.  We'll request
        // notifications before each subitem's prepaint stage.

        *pResult = CDRF_NOTIFYSUBITEMDRAW;
        }
    else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
        {
        // This is the prepaint stage for a subitem. Here's where we set the
        // item's text and background colors. Our return value will tell 
        // Windows to draw the subitem itself, but it will use the new colors
        // we set here.
        // The text color will cycle through red, green, and light blue.
        // The background color will be light blue for column 0, red for
        // column 1, and black for column 2.
    
        COLORREF crText, crBkgnd;
        
        if (( 0 == pLVCD->iSubItem ) || (!pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed)) || (17 == pLVCD->iSubItem))
            {
            crText = RGB(0,0,0);
            crBkgnd = m_backSysColor;//RGB(255,255,255);
            }
        else 
        {
         LV_DISPINFO di;
         CHAR chText[80];
         int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

            di.item.iItem = nItem;
            di.item.iSubItem = pLVCD->iSubItem;
            di.item.mask = LVIF_TEXT;
            di.item.pszText = chText;
            di.item.cchTextMax = sizeof(chText);

            GetListViewDISPINFO((LPARAM)&di);
            if (di.item.pszText[0] == '1')
            {
               //crText = RGB(255,50,50);
               //crBkgnd = m_backSysColor;
               crText = RGB(0,0,0);
               crBkgnd = m_backSysColorFade;//RGB(205,205,205);//m_backSysColor;
            }
            else
            {
               crText = RGB(0,0,0);
               crBkgnd = m_backSysColor;
            }
        }
        /*
           if ( 1 == pLVCD->iSubItem )
            {
            crText = RGB(0,255,0);
            crBkgnd = m_backSysColor;//RGB(255,0,0);
            }
        else
            {
            crText = RGB(128,128,255);
            crBkgnd = m_backSysColor;//RGB(0,0,0);
            }
        */
        // Store the colors back in the NMLVCUSTOMDRAW struct.
        pLVCD->clrText = crText;
        pLVCD->clrTextBk = crBkgnd;

        // Tell Windows to paint the control itself.
        *pResult = CDRF_DODEFAULT;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -