MFC clistctrl usage

Source: Internet
Author: User
The default view style of listctrl is report1. clistctrl lvs_icon, which 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: display item details intuitive understanding: Windows resource manager, "View" tab under "Big icon, small icon, list, details" 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 Columns
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 data 4. Always select item

Select Show selection always in the style, or set lvs_showselalways in the above 2nd

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 {
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 {
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 details about how to get the item status, refer to 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 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 of listctrl visible, that is, scroll the scroll bar.

M_list.ensurevisible (I, false );

11. Obtain 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 corresponding function of adding the nm_click message of the listctrl control to the row and column NUMBER OF THE clicked 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 item pop-up menu of listctrl and 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 and 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;
} 17. Get the item content of the listctrl control in another process.

Http://www.codeproject.com/threads/int64_memsteal.asp

18. Select the item in listview.

Q131284: How to Select a listview item programmatically
Http://support.microsoft.com/kb/131284/en-us

19. How to Use the derived class http://www.codeguru.com/cpp/controls/listview/introduction/article.php/c919/ of clistctrl in clistview
20. Add the icon m_list.setextendedstyle (lvs_ex_subitemimages) to the subitem of listctrl );
M_list.setitem (...); // For detailed parameters, see msdn

21. Determine which line of the clistctrl record is selected
1. Int n =-1;
2. Position Pos = m_pctrl-> getfirstselecteditemposition (); // return the position of the first selected row
3. If (Pos! = NULL)
4 .{
5. While (POS)
6 .{
7. n = m_pctrl-> getnextselecteditem (POS); // return the number of the next selected rows (the return value starts from 0)
8. // perform the corresponding operation
9 .}
10 .}

22. Respond to the click clistctrl event
1. // response to the = num_click message of cmylistview
2. // after being directly generated using the VC Class Wizard, you do not need to add the Declaration and message ing again.
3.
4. // Add a function declaration
5. afx_msg void onclick (nmhdr * pnmhdr, lresult * presult );
6. // Add message ing
7. on_policy_reflect (nm_click, onclick)
8.
9. Void cmylistview: onclick (nmhdr * pnmhdr, lresult * presult)
10 .{
11. // todo: add your control notification handler code here
12. nm_listview * pnmlistview = (nm_listview *) pnmhdr;
13. If (pnmlistview-> iItem! =-1)
14 .{
15 ./*
16. cstring strtemp;
17. strtemp. Format ("Click column % d of row % d ",
18. pnmlistview-> iItem, pnmlistview-> isubitem );
19. afxmessagebox (strtemp );
20 .*/
21. // The value starts from 0, and no data zone does not correspond to the time
22. // corresponding operation
23 .}
24. * presult = 0;
25 .}

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.