First, use the following statement to set the style of CListCtrl:
DWORD SetExtendedStyle( DWORD dwNewStyle );
Where
LVS_EX_ CHECKBOXES means add CheckBox
LVS_EX_FULLROWSELECT means select the whole row
LVS_EX_GRIDLINES means add table line
If the LVS_EX_CHECKBOXES property is set, you can use
to get whether a line is Checked or not.
You can start by deleting what was there before with the following statement:
for(int k=2;k>=0;k--) //note that you have to delete from back to front or else you get an error
m_ ListCtrl.DeleteColumn(k);
m_ListCtrl.DeleteAllItems();
Create a new column with the following statement:
m_ListCtrl.InsertColumn(0,_T('filename'),LVCFMT_ IMAGE|LVCFMT_LEFT);
m_ListCtrl.InsertColumn(1,_T('Instrument Category'));
m_ListCtrl.InsertColumn(2,_T('Item Category'));
Where LVCFMT_IMAGE indicates that icons can be added to the first column. If you don't want the icon you can delete it.
Then set the column width:
for(j=0;j<3;j++)
m_ListCtrl.SetColumnWidth(j ,100);
The following for the list to join the icon, if you don't need the icon, you can skip this step. If you don't need the icon, you can skip this step. Note that you can only add it for the first time, if you add it more than once, you will get error!
First in the header file to add the statement:
CImageList m_ImageList;
This is necessary, if you add in a function of the cpp due to the end of the life of the CImageList is automatically released, the effect is that the list can not see the icon, only see a white square.
The following generates CImageList and binds it to CListCtrl, this is CImageList has no icon yet, it is just a container:
static int flag=2;
if(flag==2){//call SetImageList only once, or else error
m_ImageList.Create(128, 128, ILC_COLORDDB|ILC_MASK, 20, 1);
m_ListCtrl.SetImageList(&m_ImageList,LVSIL_SMALL);
< p>}flag=(flag+1)%2;
If CListCtrl has already been used, and an icon has been added to it, then the last image put into m_ImageList should be deleted
for(int kk=0;kk<M_IMAGELIST. GETIMAGECOUNT();kk++)
m_ImageList.Remove(k);
The following describes how to add rows to the CListCtrl inside, and at the same time dynamically add icons to each row:
Assuming m_listRowCount is the number of rows to be added.
CBitmap* bitmap;
bitmap=new CBitmap[m_list1rowCount];
HBITMAP hbitmap;
for(int i = 0; i < m_listRowCount; i++)
{
// Insert the appropriate thumbnail for each row
CFile f;
CFileException e;
if( !f.Open(m_FileName, CFile::modeRead, &e )){ //m_FileName is the bmp file name, up to you
hbitmap = (HBITMAP)LoadImage(NULL,path+'blank.bmp',IMAGE_BITMAP,0,0,
LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_ LOADFROMFILE);
}else{
f.Close();
hbitmap = (HBITMAP)LoadImage(NULL,bmpFile,IMAGE_BITMAP,0,0,
LR_ CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
}
bitmap[i].Attach(hbitmap);
m_ImageList.Add(&bitmap[i], RGB(0, 128, 128));
//Insert row
m_ListCtrl.InsertItem(i,m_FileName,i);
m_ListCtrl.SetItemText(i,1,type);
m_ListCtrl .SetItemText(i,2,m_Path);
}
//Remember to delete the temporary files that are no longer useful
if(m_list1rowCount!=0)
delete[] bitmap;
2. If it's the ICON type of the CListCtrl, there is a little change to be made:
Change the code to bind the icon set from
SetImageList(&m_ImageList,LVSIL_SMALL);
to
SetImageList(&m_ ImageList,LVSIL_NORMAL);
Insert rows only with
InsertItem(i,mainSet.m_FileName,i);
No need for
SetItemText(i,1,type);
and such code.
1. ListCtrl style
LVS_ICON: show big icon for each item
LVS_SMALLICON: show small icon for each item
LVS_LIST: show a column of items with small icons
LVS_REPORT: show item details
Intuitively: Windows Explorer, "View" tab, "Big Icons, Small Icons, List, Details"
---------------- ----------------------------------------------------------------
2. Set listctrl style and extended style
LONG lStyle;
lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//Get the current window style
lStyle &= ~LVS_TYPEMASK; //clear the display style bit
lStyle |= LVS_REPORT; //set the style< /p>
SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//set style
DWORD dwStyle = m_list.GetExtendedStyle();
dwStyle |= LVS_EX_ FULLROWSELECT;// select a line and highlight the whole line (only applies with report style listctrl)
dwStyle |= LVS_EX_GRIDLINES;// gridlines (only applies with report style listctrl)
dwStyle |= LVS_ EX_CHECKBOXES;//generate checkbox control before item
m_list.SetExtendedStyle(dwStyle); //set extended style
------------------------------------------ --------------------------------------
3. Insert data
m_list.InsertColumn( 0, 'ID', LVCFMT_LEFT, 40 ); //Insert column
m_list. InsertColumn( 1, 'NAME', LVCFMT_LEFT, 50 );
int nRow = m_list.InsertItem(0, "11");//insert row
m_list. SetItemText(nRow, 1, "jacky");//set the data
----------------------------------------------------------------- ---------------
4. select item always
Check Show selection always in style, or set LVS_SHOWSELALWAYS in point 2 above
--------------------------- -----------------------------------------------------
5. Check and uncheck a row
int nIndex = 0;
//Check
m_list.SetItemState( nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
//Unselected
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_ FOCUSED);
--------------------------------------------------------------------------------
6. Get the checkbox of all rows in listctrl's State
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T('Checkbox in row %d is checked'), i);
AfxMessageBox(str);; }
AfxMessageBox(str).
}
}
--------------------------------------------------------------------------------
7. Get all the selected rows in listctrl with the Serial number
Method 1:
CString str;
for(int i=0; i
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )
{ p>
str.Format(_T('Row %d selected'), i);
AfxMessageBox(str);
}
}
}
Method 2:
POSITION pos = m_list. GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0('No items were selected!\n');
else
{
while (pos)
{
int nItem = m_list.GetNextSelectedItem(pos);
TRACE1('Item %d was selected!\n', nItem);
// you could do your own processing on nItem here
}
}
--------------------------------------------------------------------------------
8. Get the info for the item
TCHAR szBuf[1024];
LVITEM lvi;
lvi.iItem = nItemIndex;
lvi.iSubItem = 0;
lvi.mask = LVIF_TEXT;< /p>
lvi.pszText = szBuf;
lvi.cchTextMax = 1024;
m_list.GetItem(&lvi);
---------------------------------------- ----------------------------------------
9. Get the header string contents of all columns of listctrl
LVCOLUMN lvcol;
char str[256];
int nColNum;
CString strColumnName[4];//if there are 4 columns
nColNum = 0;
lvcol.mask = LVCF_TEXT;
lvcol.pszText = str;
lvcol. cchTextMax = 256;
while(m_list.GetColumn(nColNum, &lvcol))
{
strColumnName[nColNum] = lvcol.pszText;
nColNum++;
}
--------------------------------------------------------------------------------
10. make an item in listctrl visible. i.e. scroll the scrollbar
m_list.EnsureVisible(i, FALSE);
--------------------------------------------------------------------------------
11. Get listctrl column count
int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();
---------------------------------------- ----------------------------------------
12. Delete all columns
Method 1:
while ( m_list.DeleteColumn (0))
Because after you delete the first column, the subsequent columns will sequentially move upwards. moving up.
Method 2:
int nColumns = 4;
for (int i=nColumns-1; i>=0; i--)
m_list.DeleteColumn (i);
---------------------- ----------------------------------------------------------
13. Get the row and column number of the listctrl that was clicked
Add the function corresponding to the NM_CLICK message of the listctrl control
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// Method 1:
/*
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
int nItem = m_list.SubItemHitTest(&lvinfo);
if(nItem ! = -1)
{
CString strtemp;
strtemp.Format('Clicked on row %d, column %d', lvinfo.iItem, lvinfo.iSubItem);
AfxMessageBox(strtemp) ;
}
*/
// Method 2:
/*
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem ! = -1)
{
CString strtemp;
strtemp.Format('Clicked on %d row, %d column',
pNMListView->iItem, pNMListView->iSubItem);
AfxMessageBox(strtemp);
}
*/
*pResult = 0;
}
------------------------------------------------------ --------------------------
14. Determine if the click is on the listctrl's checkbox
Add the corresponding function for the listctrl control's NM_CLICK message
void CTest6Dlg::OnClickList1( NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_ list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
//determine if the point is on the checkbox
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox('Point on listctrl's checkbox');
}
*pResult = 0;
}
--------------------------------------------------- -----------------------------
15. Right-click on listctrl's item to bring up the popup menu
Add the corresponding function for the listctrl control's NM_RCLICK message
void CTest6Dlg::OnRclickList1 (NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem ! = -1)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
CMenu menu;
VERIFY( menu. LoadMenu( IDR_MENU1 ) );
CMenu* popup = menu.GetSubMenu(0);
ASSERT( popup ! = NULL );
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
}
*pResult = 0;
}
< p>--------------------------------------------------------------------------------16. Some sequence of state changes when the item switches focus (including when switching items with the keyboard and mouse)
Add the function corresponding to the LVN_ITEMCHANGED message of the listctrl control
void CTest6Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_ LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
CString sTemp;
if (( pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED &&
(pNMListView->uNewState & LVIS_FOCUSED) == 0)
{
sTemp.Format('%d losted focus',pNMListView->iItem);
}
else if ((pNMListView->uOldState & LVIS_FOCUSED) == 0 & amp;&
(pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)
{
sTemp.Format('%d got focus',pNMListView-& gt;iItem);
}
if ((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED & &
(pNMListView->. uNewState & LVIS_SELECTED) == 0)
{
sTemp.Format('%d lost selected',pNMListView-> iItem);
}
else if((( pNMListView->uOldState & LVIS_SELECTED) == 0 &&
(pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
< p> {sTemp.Format('%d got selected',pNMListView->iItem);
}
*pResult = 0;
}
--------------------------- -----------------------------------------------------
17. listctrl's subitem to add an icon
Note: first use InsertItem() to insert the first column of a row, then SetItem to set the other items. Set other items
m_list.SetExtendedStyle(LVS_EX_SUBITEMIMAGES);
m_userlist.SetItem(...) ;
m_userlist. ;
--------------------------------------------------------------------------------
18. Show files in CListCtrl and display icons based on file type
< p> Code found on the web, tried and corrected myself, shareStep 1: Set list's image list to system image list
BOOL SetSystemImageList( CListCtrl & list )
{
HIMAGELIST himlSmall;
HIMAGELIST himlLarge;
SHFILEINFO sfi;
char cSysDir[MAX_PATH];
CString strBuf;
memset(cSysDir, 0, MAX_PATH);
GetWindowsDirectory(cSysDir, MAX_PATH);
strBuf = cSysDir ;
// SHGetFileInfo:
// If uFlags contains SHGFI_SYSICONINDEX, the return value is a handle to
// an image list that contains the large icon images.
// If SHGFI_SMALLICON is included with SHGFI_SYSICONINDEX, the return value
// is the handle to an image list that contains the small icon images. himlSmall = (HIMAGELIST)SHGetFileInfo ( (LPCSTR)cSysDir,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLIQUE | SHGFI_SMALLIQUE | SHGFI_SMALLIQUE | SHGFI_SYSICONINDEX | SHGFI_SMALLIQUE SHGFI_SMALLICON );
himlLarge = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
if (himlSmall && himlLarge)
{
::SendMessage(list.m_hWnd, LVM_ SETIMAGELIST,
(WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
::SendMessage(list.m_hWnd, LVM_SETIMAGELIST,
(WPARAM)LVSIL_ NORMAL, (LPARAM)himlLarge);
}
return TRUE;
}
Step 2: Add the specified file to the list, and at the same time, get the icon index of the file, add icons to the list
int GetIconIndex( LPCTSTR lpszPath, BOOL bIsDir, BOOL bSelected) ; // Declare it forward
void AddFiles(CListCtrl & list, LPCTSTR lpszFileName, BOOL bAddToDocument)
{
int nIcon = GetIconIndex(lpszFileName, FALSE, TRUE);
CString strSize;
CFileFind filefind;
// get file size
< p>if ( filefind.FindFile(lpszFileName)){
filefind.FindNextFile();
strSize.Format('%d', filefind.GetLength());
< p>}else
strSize = '0';
// split path and filename
CString strFileName = lpszFileName;
CString strPath;
int nPos = strFileName.ReverseFind('\\');
if (nPos ! = -1)
{
strPath = strFileName.Left(nPos);
strFileName = strFileName.Mid(nPos + 1);
}
// insert to list
int nItem = list.GetItemCount();
/// list.InsertItem(nItem, strFileName, nIcon);
// list.SetItemText(nItem, 1, strSize);
// /
// Here you can modify the code according to the actual need
//
}
-------------------------------------------------------------------------------- p>
19. listctrl content is updated with large amount of data to avoid flickering
m_list.SetRedraw(FALSE);
//Update content
m_list.SetRedraw(TRUE);
m_list.Invalidate Invalidate();
m_list.UpdateWindow();
20. Clear ListCtrl to re-initialize:
// Delete all rows
m_ctrllist.DeleteAllItems() ;
int iColCount = m_ctrllist.GetHeaderCtrl()->GetItemCount() ;
// Method 1
// Principle: After deleting the first column, the other columns move forward
while( m_ctrllist. DeleteColumn(0) ) ;
// Method 2
for (int i=0; i < iColCount ; i++)
{
m_ctrllist.