Add Check box in second column in CListCtrl in MFC

2019-09-03 09:30发布

In my MFC application, im adding CListCtrl in my View class using OnCreate() function. I have 10 columns and 8 rows in that table. I want to include check box in second column . My code is

int CTrendView::OnCreate(LPCREATESTRUCT l)
{

m_ctLstCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT ,listRect, this, IDC_TRENDLISTCTRL);

    m_ctLstCtrl.SetExtendedStyle(m_ctLstCtrl.GetExtendedStyle()  | LVS_EX_GRIDLINES| LVS_EX_FULLROWSELECT| LVS_EX_ONECLICKACTIVATE );

    m_ctLstCtrl.SetBkColor(RGB(255,255,255));
    m_ctLstCtrl.SetTextColor(RGB(0,0,0));
    m_ctLstCtrl.SetTextBkColor(RGB(255,255,255));

    m_ctLstCtrl.InsertColumn(0,_T(""),LVCFMT_LEFT,10);
    m_ctLstCtrl.InsertColumn(1,_T("Visible"),LVCFMT_LEFT,50);
    m_ctLstCtrl.InsertColumn(2,_T("Status"),LVCFMT_LEFT,50);
    m_ctLstCtrl.InsertColumn(3,_T("Color"),LVCFMT_LEFT,50);
    m_ctLstCtrl.InsertColumn(4,_T("Object1"),LVCFMT_RIGHT,100);
    m_ctLstCtrl.InsertColumn(5,_T("Object2"),LVCFMT_RIGHT,100);
    m_ctLstCtrl.InsertColumn(6,_T("Desc"),LVCFMT_RIGHT,100);
    m_ctLstCtrl.InsertColumn(7,_T("Value"),LVCFMT_LEFT,100);
    m_ctLstCtrl.InsertColumn(8,_T("Low"),LVCFMT_LEFT,100);
    m_ctLstCtrl.InsertColumn(9,_T("High"),LVCFMT_LEFT,100);
}

Im using below function to add green color in third column and fourth column.

void CTrendView::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{

NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

   *pResult = CDRF_DODEFAULT;

   switch(pLVCD->nmcd.dwDrawStage)
   {
   case CDDS_PREPAINT:
      *pResult = CDRF_NOTIFYITEMDRAW;
      break;

   case CDDS_ITEMPREPAINT:
      *pResult = CDRF_NOTIFYSUBITEMDRAW;
      break;

   case (CDDS_ITEMPREPAINT | CDDS_SUBITEM):  
       {
         if(pLVCD->iSubItem == 2 )                    
               pLVCD->clrTextBk  = RGB(0, 255, 0);  
         else
            pLVCD->clrTextBk  = RGB(255, 255, 255);
       }

       for(int i=0;i<8;i++)
       {
           if(pLVCD->nmcd.dwItemSpec == i && pLVCD->iSubItem == 3) 
                pLVCD->clrTextBk  = PenSelect[i];              
       }    
      break;
   }

}

For adding dynamic values for other columns im using one Fucntion getting called from OnDraw().

void DrawData(CDC *pDC,int iTagPos,CListCtrl &TagListctrl,CRect rect,int pType,float pScaleLow,float pScaleHigh, int TimeCursorPos,int pFlag,CString stime)
{

    int index;   
    CString sPV, sType;     
    sType = ".SV";
        if (TrendTempSV[TimeCursorPos] != -999999)
        {               
        sPV.Format ("%3.2f",TrendTempSV[TimeCursorPos]);            
        }
        else
          sPV.Format ("%s","**.** ");       


        CString sScaleLow,sScaleHigh,indexno;
        sScaleLow.Format ("%4.1f",pScaleLow);
        sScaleHigh.Format ("%4.1f", pScaleHigh);
        indexno.Format("%d",iTagPos+1);
        TagListctrl.SetRedraw( FALSE );
        TagListctrl.DeleteItem(iTagPos);
        index = TagListctrl.InsertItem(iTagPos,indexno);

        TagListctrl.SetItemText(iTagPos,4,"Object");
        TagListctrl.SetItemText(iTagPos,5,sName);
        TagListctrl.SetItemText(iTagPos,6,sDesc);
        TagListctrl.SetItemText(iTagPos,7,sPV);
        TagListctrl.SetItemText(iTagPos,8,sScaleLow);
        TagListctrl.SetItemText(iTagPos,9,sScaleHigh);
        TagListctrl.SetRedraw( TRUE );  

}

I want check box under 'Visible' column. FOr that, i include | LVS_EX_CHECKBOXES in SetExtendedStyle.

m_ctLstCtrl.SetExtendedStyle(m_ctLstCtrl.GetExtendedStyle() | LVS_EX_CHECKBOXES | LVS_EX_GRIDLINES|
                                         LVS_EX_FULLROWSELECT|  LVS_EX_ONECLICKACTIVATE );

Im facing below problem by doing this. Checkbox appears in first column with index number. The ListCtrl start to flicker,headers are not visible,only when i click somwhere in listctrl each column header et appears one by one and the sixe of the listctrl also not same as previous. How can i avoid this?

2条回答
别忘想泡老子
2楼-- · 2019-09-03 09:54

You might want to check out XListCtrl. It features checkbox columns at arbitrary positions as well as text formatting of cell content (color).

查看更多
对你真心纯属浪费
3楼-- · 2019-09-03 10:02

Also, you can use CListCtrlExt (or CListViewExt), which are not custom draw !! that is mean you can have OS style intact and you can put on every column, any kind of control, including check box button ...

查看更多
登录 后发表回答