Tips for using clistctrl listcontrol in VC ++

Source: Internet
Author: User

1. Set the listctrl style and extended Style
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
Set style:
Long lstyle;
Lstyle | = lvs_report; // set the style
Setwindowlong (m_list.m_hwnd, gwl_style, lstyle); // set the style
Obtain style:
Lstyle = getwindowlong (m_list.m_hwnd, gwl_style); // get the style of the current window
Lstyle & = ~ Lvs_typemask; // clear the display mode bit
Extended style:
Lvs_ex_fullrowselect; // select an exercise to highlight the entire line (only applicable to listctrl in the report style)
Lvs_ex_gridlines; // gridlines (only applicable to listctrl in the report style)
Lvs_ex_checkboxes; // the checkbox control is generated before the item.
Set the extended style:
M_list.setextendedstyle (lvs_ex_fullrowselect | lvs_ex_gridlines | lvs_ex_checkboxes); // set the extended Style

2. 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

3. 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 );

4. 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 );
}
}

5. 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
}
}

6. 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 );

7. 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 ++;
}

8. 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 );

9. 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;
}

10. 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;
}

Related Article

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.