双击更改

时间:2018-10-06 07:24:31

标签: c++ visual-c++ mfc clistctrl

我有一个名为CListCtrl的列表控件m_listCtrl,在创建对话框时显示IDFirstnameLastname。在下面的代码中,我通过单击项目的double click并更改该行第一列的值来处理position

//This is in the message map and IDC_LISTNAMES is the list control
ON_NOTIFY(NM_DBLCLK, IDC_LISTNAMES,  OnDoubleClick)


void CSqlMfcTestDlg::OnDoubleClick(NMHDR* pNMHDR, LRESULT* pResult) 
{

  POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
  if (pos == NULL)
  {
    TRACE(_T("No items were selected!\n"));
  }
  else
  {
    while (pos)
    {
        int nItem = m_listCtrl.GetNextSelectedItem(pos);

        itemClicked = nItem;
        CString idString = m_listCtrl.GetItemText(nItem, 0);
        CString textString = m_listCtrl.GetItemText(nItem, 1);
        MessageBox(L"Item Clicked is : " + textString);
        m_listCtrl.SetItemText(nItem,0, idString+L"(Clicked)");


    }
  }

}

我想添加一条语句来更改该行的颜色,而不是m_listCtrl.SetItemText(nItem,0, idString+L"(Clicked)");

1 个答案:

答案 0 :(得分:0)

查看以下代码段:

class MyListCtrl: public CListCtrl
{
   private:
      int m_row;
   public:
      afx_msg void OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult); // write your custom code in the definition
      afx void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
}; 

class CSqlMfcTestDlg: public CDialogEx 
{
   private:
      MyListCtrl m_listCtrl;    
};

定义(未经测试):

void MyListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
  LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
  int iRow = lplvcd->nmcd.dwItemSpec;

  switch(lplvcd->nmcd.dwDrawStage)
  {
     case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
    {
         if(iRow == m_row)
         lplvcd->clrTextBk =  RGB(0,0,255);
    }
  }
}
void MyListCtrl::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    m_row = GetSelectionMark();
    RedrawItems(m_row, m_row);
}