Add Check box in second column in CListCtrl in MFC

旧巷老猫 提交于 2019-12-11 18:39:59

问题


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?


回答1:


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




回答2:


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 ...



来源:https://stackoverflow.com/questions/54074381/add-check-box-in-second-column-in-clistctrl-in-mfc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!