MFC clistctrl usage. style/insertion, deletion, selected data and sorting problems, etc.

Source: Internet
Author: User
MFC clistctrl usage. style/insert, delete, selected data and sorting problems, etc. Excerpted from: http://hi.baidu.com/fclshark/blog/item/650cbaa731b7619fd14358dc.html
Powerful clistctrl: for network notes, you also need to add and modify clistctrl to change the line color, column color, font color, and so on.
There is a sort in the resource attribute to set the sorting. If you accidentally select ascending, then the order of your insertitem will be, 20, 21 .... so pay attention. first, use the following statement to set the clistctrl style: DWORD setextendedstyle (DWORD dwnewstyle). lvs_ex_checkboxes indicates adding checkboxlvs_ex_fullrowselect indicates selecting the entire line lvs_ex_gridlines to add table lines.
If the lvs_ex_checkboxes attribute is set, you can use bool getcheck (INT nitem) const; to check whether a row is checked.
You can use the following statement to delete the previous items: For (int K = 2; k> = 0; k --) // delete them from the back, otherwise, the error m_listctrl.deletecolumn (k); m_listctrl.deleteallitems ();
Use the following statement to create a column: m_listctrl.insertcolumn (0, _ T ("file name"), lvcfmt_image | lvcfmt_left); m_listctrl.insertcolumn (1, _ T ("instrument category ")); m_listctrl.insertcolumn (2, _ T ("project category "));
Lvcfmt_image indicates that the icon can be added to the first column. If no icon is required, delete it.
Then set the column width: For (j = 0; j <3; j ++) m_listctrl.setcolumnwidth (J, 100 );
Add the icon to the list as follows. skip this step if you do not need the icon. Note: Only join is performed for the first time. If join is performed for multiple times, an error occurs! First add the declaration in the header file: cimagelist m_imagelist; this is necessary. If cimagelist is automatically released due to the end of the life cycle in a CPP function, the effect is that the icon is not displayed in the list, only one white square is displayed. The following code generates a cimagelist and binds it to clistctrl. This is a container without an icon in cimagelist: static int flag = 2; If (flag = 2) {// only setimagelist is called once; otherwise, m_imagelist.create (128,128, ilc_colorddb | ilc_mask, 20, 1); then (& m_imagelist, lvsil_small);} flag = (flag + 1) % 2; If clistctrl has been used and an icon has been added, the imagefor (int kk = 0; KK <m_imagelist.getimagecount (); KK ++) in m_imagelist will be deleted) m_imagelist.remove (k );
The following describes how to add rows to clistctrl and dynamically add icons to each row: Assume m_listrowcount is the number of rows to be added. Cbitmap * bitmap; bitmap = new cbitmap [m_list1rowcount]; hbitmap;
For (INT I = 0; I <m_listrowcount; I ++) {// Insert the corresponding thumbnail cfile F for each row; cfileexception E; If (! F. open (m_filename, cfile: moderead, & E) {// m_filename is the BMP file name. You can determine hbitmap = (hbitmap) LoadImage (null, path + "blank.bmp ", image_bitmap, 0, 0, lr_createdibsection | lr_defaultsize | lr_loadfromfile);} else {f. close (); hbitmap = (hbitmap) LoadImage (null, BMP file, image_bitmap, 0, 0, lr_createdibsection | lr_defaultsize | lr_loadfromfile);} bitmap [I]. attach (hbitmap); m_imagelist.add (& bitmap [I], RGB (0,128,128 ));
// Insert the 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 useless temporary file if (m_list1rowcount! = 0) Delete [] bitmap;
2. For the clistctrl of the icon type, make a few changes: bind Code Changed from setimagelist (& m_imagelist, lvsil_small); To setimagelist (& m_imagelist, lvsil_normal );
When inserting a row, only insertitem (I, mainset. m_filename, I) is used; setitemtext (I, 1, type) is not used.
1. listctrl style lvs_icon: displays the large icon lvs_smallicon for each item: displays the small icon lvs_list for each item: displays a column of item lvs_report with a small icon: 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 style long lstyle; lstyle = getwindowlong (m_list.m_hwnd, gwl_style); // obtain the current window style lstyle & = ~ Lvs_typemask; // clear the lstyle | = lvs_report; // set style setwindowlong (m_list.m_hwnd, gwl_style, lstyle); // set Style
DWORD dwstyle = m_list.getextendedstyle (); dwstyle | = lvs_ex_fullrowselect; // select a whole row highlighted (only applicable to listctrl in the report style) dwstyle | = lvs_ex_gridlines; // gridlines (only applicable to listctrl in the report style) dwstyle | = lvs_ex_checkboxes; // the checkbox control m_list.setextendedstyle (dwstyle) is generated before the item; // set the extended Style
--------------------------------------------------------------------------------
3. insert data m_list.insertcolumn (0, "ID", lvcfmt_left, 40); // insert columns m_list.insertcolumn (1, "name", lvcfmt_left, 50); int nrow = m_list.insertitem (0, "11"); // insert the 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 deselect 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. the checkbox status of all rows in listctrl is 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 );
--------------------------------------------------------------------------------
9. Obtain the header string content of all columns of listctrl lvcolumn lvcol; char STR [256]; int ncolnum; cstring strcolumnname [4]; // if there are four 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 will move up in sequence.
Method 2: int ncolumns = 4; for (INT I = nColumns-1; I> = 0; I --) m_list.deletecolumn (I );
--------------------------------------------------------------------------------
13. obtain the row and column number of the clicked listctrl control and add the nm_click message function 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 ("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 click and 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_above; uint nflag; int nitem = m_list.hittest (point, & nflag); // you can determine whether a point is located on the checkbox if (nflag = lvht_onitemstateicon) {afxmessagebox ("point in the checkbox of listctrl");} * presult = 0 ;}
--------------------------------------------------------------------------------
15. right-click the listctrl item pop-up menu and add the nm_rclick message function of the listctrl control. Void ctest6dlg: onrclicklist1 (nmhdr * pnmhdr, lresult * presult) {nm_listview * pnmlistview =; 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 switches focus (including when item is switched with keyboard and mouse), the 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 ;}
--------------------------------------------------------------------------------
17. Add the subitem icon of listctrl
Note: you must first use insertitem () to insert the first column of a row, and then use setitem to set other items m_list.setextendedstyle (lvs_ex_subitemimages); m_userlist.setitem (..);
--------------------------------------------------------------------------------
18. display the file in clistctrl and display the code found on the Internet by the icon Based on the file type. Try it and correct it by yourself. Share
Step 1: Set the list of images to the 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 wrongly ded 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_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, obtain the icon index of the file, and add the icon to the list.
Int geticonindex (lpctstr lpszpath, bool bisdir, bool bselected); // forward void addfiles (clistctrl & list, lpctstr values, bool baddtodocument) {int nicon = geticonindex (limit, false, true); cstring strsize; cfilefind FileFind;
// Get file sizeif (FileFind. findfile (lpszfilename) {FileFind. findnextfile (); strsize. format ("% d", FileFind. getlength ();} else strsize = "0 ";
// Split path and filenamecstring strfilename = lpszfilename; cstring strpath;
Int NPOs = strfilename. reversefind ('\'); If (NPOs! =-1) {strpath = strfilename. Left (NPOs); strfilename = strfilename. mid (NPOs + 1 );}
// Insert to listint nitem = List. getitemcount ();

// List. insertitem (nitem, strfilename, nicon); // list. setitemtext (nitem, 1, strsize); // you can modify the code as needed //}

--------------------------------------------------------------------------------
19. When the listctrl content is updated with a large amount of data, avoid blinking m_list.setredraw (false); // update the content m_list.setredraw (true); m_list.invalidate (); m_list.updatewindow ();
20. Clear listctrl to reinitialize:
// Delete all rows m_ctrllist.deleteallitems (); // delete all columns int icolcount = m_ctrllist.getheaderctrl ()-> getitemcount ();
// Method 1 // principle: after the first column is deleted, other columns move forward while (m_ctrllist.deletecolumn (0); // method 2For (INT I = 0; I <icolcount; I ++) {m_ctrllist.deletecolumn (0 );}
// Method 3for (INT I = iColCount-1; I> = 0; I --) {m_ctrllist.deletecolumn (I );
 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/Bilter/archive/2010/07/29/5773415.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.