Tips for using Windows listctrl
1. listctrl Style
Lvs_icon: displays a large icon for each item
Lvs_smallicon: displays small icons for each item
Lvs_list: displays a column of items with small icons
Lvs_report: displays item details
Intuitive understanding: Windows resource manager, the "Big icon, small icon, list, details" under the "View" tab"
--------------------------------------------------------------------------------
2. Set the listctrl style and extended long lstyle;
Lstyle = getwindowlong (m_list.m_hwnd, gwl_style); // get the style of the current window
Lstyle & = ~ Lvs_typemask; // clear the display mode bit
Lstyle | = lvs_report; // set the style
Setwindowlong (m_list.m_hwnd, gwl_style, lstyle); // set the style
DWORD dwstyle = m_list.getextendedstyle ();
Dwstyle | = lvs_ex_fullrowselect; // select an exercise to highlight the entire line (only applicable to listctrl in the report style)
Dwstyle | = lvs_ex_gridlines; // gridline (only applicable to listctrl with report style)
Dwstyle | = lvs_ex_checkboxes; // the checkbox control is generated before the item.
M_list.setextendedstyle (dwstyle); // you can specify the extended style.
NOTE: For the listview style, refer to msdn
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/wceshellui5/html/wce50lrflistviewstyles. asp
--------------------------------------------------------------------------------
3. insert data m_list.insertcolumn (0, "ID", lvcfmt_left, 40); // insert a column
M_list.insertcolumn (1, "name", lvcfmt_left, 50 );
Int nrow = m_list.insertitem (0, "11"); // insert a row
M_list.setitemtext (nrow, 1, "Jacky"); // set the data
--------------------------------------------------------------------------------
4. Always select Show selection always in the selected style of the item, or set lvs_showselalways in the above 2nd points.
--------------------------------------------------------------------------------
5. Select and cancel the selected int nindex = 0;
// Select
M_list.setitemstate (nindex, lvis_selected | lvis_focused, lvis_selected | lvis_focused );
// Deselect
M_list.setitemstate (nindex, 0, lvis_selected | lvis_focused );
--------------------------------------------------------------------------------
6. Get the checkbox status of all rows in listctrl m_list.setextendedstyle (lvs_ex_checkboxes );
Cstring STR;
For (INT I = 0; I <m_list.getitemcount (); I ++)
{
If (m_list.getitemstate (I, lvis_selected) = lvis_selected | m_list.getcheck (I ))
{
Str. Format (_ T ("the checkbox of row % d is selected"), I );
Afxmessagebox (STR );
}
}
--------------------------------------------------------------------------------
7. Obtain the serial numbers of all selected rows in listctrl.
Method 1:
Cstring STR;
For (INT I = 0; I <m_list.getitemcount (); I ++)
{
If (m_list.getitemstate (I, lvis_selected) = lvis_selected)
{
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 cocould do your own processing on nitem here
}
}
--------------------------------------------------------------------------------
8. Obtain the item information tchar szbuf [1024];
Lvitem LVI;
LVI. iItem = nitemindex;
LVI. isubitem = 0;
LVI. Mask = lvif_text;
LVI. psztext = szbuf;
LVI. cchtextmax = 1024;
M_list.getitem (& LVI );
For more information about how to set the item status, see the msdn article.
Q173242: use masks to set/get item states in clistctrl
Http://support.microsoft.com/kb/173242/en-us
--------------------------------------------------------------------------------
9. Obtain the header string content 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 one item in listctrl visible, that is, scroll the scroll bar m_list.ensurevisible (I, false );
--------------------------------------------------------------------------------
11. Get the number of columns in listctrl int nheadnum = m_list.getheaderctrl ()-> getitemcount ();
--------------------------------------------------------------------------------
12. method 1 of deleting all columns:
While (m_list.deletecolumn (0 ))
Because after you delete the first column, the columns that follow it will move up sequentially.
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 clicked listctrl. Add the nm_click message function 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_abve;
Int nitem = m_list.subitemhittest (& lvinfo );
If (nitem! =-1)
{
Cstring strtemp;
Strtemp. Format ("Click column % d of row % d", lvinfo. iItem, lvinfo. isubitem );
Afxmessagebox (strtemp );
}
*/
// Method 2:
/*
Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
If (pnmlistview-> iItem! =-1)
{
Cstring strtemp;
Strtemp. Format ("Click column % d of row % d ",
Pnmlistview-> iItem, pnmlistview-> isubitem );
Afxmessagebox (strtemp );
}
*/
* Presult = 0;
}
--------------------------------------------------------------------------------
14. Determine whether to add the nm_click message function of the listctrl control to the checkbox of listctrl.
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_abve;
Uint nflag;
Int nitem = m_list.hittest (point, & nflag );
// Determine whether the point is in the checkbox
If (nflag = lvht_onitemstateicon)
{
Afxmessagebox ("point in the checkbox of listctrl ");
}
* Presult = 0;
}
--------------------------------------------------------------------------------
15. Right-click the listctrl item pop-up menu to add the nm_rclick message function of the listctrl Control
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;
}
--------------------------------------------------------------------------------
16. When item focus is switched (including when item is switched with keyboard or mouse), The lvn_itemchanged message function of listctrl control is added in the order of changes in the status.
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 &&
(Pnmlistview-> unewstate & lvis_focused) = lvis_focused)
{
Stemp. Format ("% d got focus", pnmlistview-> iItem );
}
If (pnmlistview-> uoldstate & lvis_selected) = lvis_selected &&
(Pnmlistview-> unewstate & lvis_selected) = 0)
{
Stemp. Format ("% d losted selected", pnmlistview-> iItem );
}
Else if (pnmlistview-> uoldstate & lvis_selected) = 0 &&
(Pnmlistview-> unewstate & lvis_selected) = lvis_selected)
{
Stemp. Format ("% d got selected", pnmlistview-> iItem );
}
* Presult = 0;
}
--------------------------------------------------------------------------------
20. Add the icon m_list.setextendedstyle (lvs_ex_subitemimages) to the subitem of listctrl );
M_userlist.setitem (...); // For detailed parameters, see msdn
--------------------------------------------------------------------------------
21. display the file in clistctrl, and display the code found on the Internet based on the file type, share
Bool ctest6dlg: oninitdialog ()
{
Cdialog: oninitdialog ();
Himagelist himlsmall;
Himagelist himllarge;
Shfileinfo SFI;
Char csysdir [max_path];
Cstring strbuf;
Memset (csysdir, 0, max_path );
Getwindowsdirectory (csysdir, max_path );
Strbuf = csysdir;
Sprintf (csysdir, "% s", strbuf. Left (strbuf. Find ("file :///));
Himlsmall = (himagelist) shgetfileinfo (lpcstr) csysdir,
0,
& SFI,
Sizeof (shfileinfo ),
Shgfi_sysiconindex | shgfi_smallicon );
Himllarge = (himagelist) shgetfileinfo (lpcstr) csysdir,
0,
& SFI,
Sizeof (shfileinfo ),
Shgfi_sysiconindex | shgfi_largeicon );
If (himlsmall & himllarge)
{
: Sendmessage (m_list.m_hwnd, lvm_setimagelist,
(Wparam) lvsil_small, (lparam) himlsmall );
: Sendmessage (m_list.m_hwnd, lvm_setimagelist,
(Wparam) lvsil_normal, (lparam) himllarge );
}
Return true; // return true unless you set the focus to a control
}
Void ctest6dlg: addfiles (lpctstr lpszfilename, bool baddtodocument)
{
Int nicon = geticonindex (lpszfilename, false, false );
Cstring strsize;
Cfilefind FileFind;
// Get File Size
If (FileFind. findfile (lpszfilename ))
{
FileFind. findnextfile ();
Strsize. Format ("% d", FileFind. getlength ());
}
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 = m_list.getitemcount ();
M_list.insertitem (nitem, strfilename, nicon );
M_list.setitemtext (nitem, 1, strsize );
M_list.setitemtext (nitem, 2, strfilename. Right (3 ));
M_list.setitemtext (nitem, 3, strpath );
}
Int ctest6dlg: geticonindex (lpctstr lpszpath, bool bisdir, bool bselected)
{
Shfileinfo SFI;
Memset (& SFI, 0, sizeof (SFI ));
If (bisdir)
{
Shgetfileinfo (lpszpath,
File_attribute_directory,
& SFI,
Sizeof (SFI ),
Shgfi_smallicon | shgfi_sysiconindex |
Shgfi_usefileattributes | (bselected? Shgfi_openicon: 0 ));
Return SFI. iicon;
}
Else
{
Shgetfileinfo (lpszpath,
File_attribute_normal,
& SFI,
Sizeof (SFI ),
Shgfi_smallicon | shgfi_sysiconindex |
Shgfi_usefileattributes | (bselected? Shgfi_openicon: 0 ));
Return SFI. iicon;
}
Return-1;
}
--------------------------------------------------------------------------------
22. When the listctrl content is updated with a large amount of data, avoid blinking m_list.setredraw (false );
// Update content
M_list.setredraw (true );
M_list.invalidate ();
M_list.updatewindow ();