MFC control of the CListCtrl application example tutorial

  • 2020-04-02 02:39:48
  • OfStack

In this paper, the application method of CListCtrl in MFC control is described in detail in the form of an example. The specific steps are as follows:

First, one of the more unusual features of the CMFCListCtrl implementation is the ability to extend the height of each line through CImage. The instantiation code is as follows:


CMFCListCtrl m_lisTestResult;

1. Insert column (create table) :


m_lisTestResult.InsertColumn(0, _T(" The serial number "), LVCFMT_CENTER, 50);

2. Fill contents:


m_lisTestResult.InsertItem(iRow, strRow, -1);  //IRow is the row number, starting at 0; StrRow for display sequence number
m_lisTestResult.SetItemText(iRow, 1, _T(" According to the content "));

3. Get the number of rows:


int iRow = m_lisTestResult.GetItemCount();

4. Get column number:


int iList = m_lisTestResult.GetHeaderCtrl().GetItemCount();

5. Get header characters:


CString strTemp;

LVCOLUMN lvcol;
TCHAR str[32];

lvcol.mask = LVCF_TEXT;
lvcol.pszText = str;
lvcol.cchTextMax = 32;
if (m_lisTestResult.GetColumn(j, &lvcol))
{
  strTemp.Format(_T("%s,"),lvcol.pszText); 
}

6. Change the row height, which is mainly realized through CImage cooperation. The specific code is as follows:


CMFCListCtrl m_lisTestResult;
CFont m_font;
m_font.CreateFont(-12,0,0,0,100,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,_T("Arial"));
this->SetFont(&m_font);
if(!m_lisTestResult.GetSafeHwnd())
{
   CRect rect;
    this->GetDlgItem(IDC_LIST_TESTRESULT)->GetWindowRect(&rect);
    ScreenToClient(&rect);
    m_lisTestResult.Create(WS_CHILD | WS_BORDER | WS_VISIBLE | WS_VSCROLL | LVS_REPORT, rect, this, IDC_LIST_TESTRESULT);
    m_lisTestResult.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
   m_lisTestResult.SetFont(&m_font);
   CImageList images;
  images.Create(1, 24, TRUE | ILC_COLOR32, 1, 0);
  m_lisTestResult.SetImageList(&images, LVSIL_SMALL);

}

I hope the code described in this article is helpful to your Windows programming.


Related articles: