Introduction to Windows Programming (8)-Use of The ListBox control in MFC

Source: Internet
Author: User

Link: http://www.cnblogs.com/zuollblog/archive/2010/04/21/1716940.html

1. clistctrl 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, "View" under the tab "Big icon, small icon, list, details
--------------------------------------------------------------------------------
2. Set the listctrl style and extended Style
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 );

// Newly inserted
Int nrow = m_list.insertitem (0, "11"); // insert a row
M_list.setitemtext (nrow, 1, "Jacky"); // you can specify other columns.

// The newly inserted data is shown below

Int nindex = m_list.getitemcount ();
Lv_item lvitem;
Lvitem. Mask = lvif_text;
Lvitem. iItem = nindex; // number of rows
Lvitem. isubitem = 0;
Lvitem. psztext = (char *) (lpctstr) strcount; // The first column
// Insert the record value in the last row.
M_list.insertitem (& lvitem );
// Insert other columns
M_list.setitemtext (nindex, 1, strlat );
--------------------------------------------------------------------------------
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 row
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. Get 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 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. Delete all columns
Method 1:
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 click on the checkbox of listctrl.
Add the nm_click message function of the listctrl control.
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.
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. The changing order of status when item focus is switched (including when item focus is switched with keyboard or mouse)
Add the lvn_itemchanged message function 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 &&
(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 of clistctrl in clistview
Http://www.codeguru.com/cpp/controls/listview/introduction/article.php/c919/

--------------------------------------------------------------------------------
20. Add the subitem icon of listctrl
M_list.setextendedstyle (lvs_ex_subitemimages );
M_list.setitem (...); // For detailed parameters, see msdn

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.