Listctrl is often used at work and is often used to post and ask how to use this control,
So I summarized my usage experience for reference.
First Note: Here, m_listctrl is used to represent a clistctrl class object,
Here, listctrl is in the report format. For other large icons and small icons
After all, report is widely used. Second, we use Clause 1 and Clause 2.
To describe the first and second points. refer to the definition of Objective C ++.
Cool
Clause 1: Set the listctrl style.
On csdn, we often see people asking how to set the style. Their listctrl is like a list.
, There is a line between the horizontal and vertical bars, and then select a row. You must select the entire row instead of only one column.
Selected, and so on. Here is a more comprehensive setting method.
// Obtain the original style
DWORD dwstyle =: getwindowlong (m_listctrl.m_hwnd, gwl_style );
Dwstyle & = ~ (Lvs_typemask );
Dwstyle & = ~ (Lvs_editlabels );
// Set a new style
Setwindowlong (m_listctrl.m_hwnd, gwl_style,
Dwstyle, | lvs_report | lvs_nolabelwrap | lvs_showselalways );
// Set the extended Style
DWORD styles =
Lvs_ex_fullrowselect | lvs_ex_gridlines | lvs_ex_checkboxes;
Listview_setextendedlistviewstyleex (m_listctrl.m_hwnd, styles,
Styles );
Lvs_ex_fullrowselect indicates the entire row selected.
Lvs_ex_gridlines (only applicable to listctrl in report style)
Add a checkbox before lvs_ex_checkboxes
Plistctrl-> setextendedstyle (m_listctrl.getextendedstyle ()
| Lvs_ex_subitemimages );
This is also a very important attribute. In this way, you can add the icon in the list and remember
Task Manager? If you want to do that, you need to add this attribute. What I will talk about later ~
Clause 2: Add a column header
This is a more substantive thing. Separate the list box with a column header.
The code is talking.
Tchar rgtsz [2] [10] = {_ T ("column header 1"), _ T ("column header 2 ")};
Lv_column lvcolumn;
Crect rect;
M_listctrl.getwindowrect (& rect );
For (INT I = 0; I <2; I ++)
{
Lvcolumn. Mask = lvcf_fmt | lvcf_subitem | lvcf_text
| Lvcf_width | lvcf_order;
Lvcolumn. FMT = lvcfmt_left;
Lvcolumn. psztext = rgtsz [I];
Lvcolumn. isubitem = I;
Lvcolumn. iorder = I;
If (I = 0)
{
Lvcolumn. Cx = rect. Width () * 3/5;
}
Else
Lvcolumn. Cx = rect. Width () * 2/5;
M_listctrl.insertcolumn (I, & lvcolumn );
}
This is how to insert two columns. Do you want to insert 20 columns ?? Draw a gourd as you like ~~
The mask in lvcolumn. Mask can have various attributes. Let's take a look at msdn,
Clause 3: insert records into the list box
Int nindex = m_listctrl.getitemcount ();
Lv_item lvitemadd = {0 };
Lvitemadd. Mask = lvif_text;
Lvitemadd. iItem = nindex;
Lvitemadd. isubitem = 0;
Lvitemadd. psztext = _ T ("Mao 1 ");;
If (m_listctrl.insertitem (& lvitemadd )! =-1)
{
Lv_item lvitem = {0 };
Lvitem. Mask = lvif_text;
Lvitem. iItem = nindex;
Lvitem. isubitem = 1;
Lvitem. psztext = _ T ("Mao 2 ");
M_listctrl.setitem (& lvitem );
}
Nindex indicates the current number of rows, and inserts a new row into the bottom of the column,
Clause 4: insert an icon to the list
You can also insert icons in report format.
Continue speaking code
M_image is a cimagelist object.
M_image.create (16, 16, true | ilc_color24, 3, 1 );
M_listctrl.setimagelist (& m_image, lvsil_small );
Then call the cimagelist member function int cimagelist: add (hicon );
Insert the icon to imagelist,
Then, when the record is inserted
Lvitemadd. Mask = lvif_text;-"lvitemadd. Mask =
Lvif_text | lvif_image
Then add a lvitemadd. iimage = N;
N is the serial number in imagelist, which indicates the specific icon, list?
Clause 5: Use additional information when inserting a record. Use of lparam
Sometimes, if you want to add additional information to a row, you can use this
Lparam
The specifies the 32-bit value of the item described in msdn
The last time I added a window handle to a row,
Int nindex = m_listctrl.getitemcount ();
Lv_item lvitemadd = {0 };
Lvitemadd. Mask = lvif_text | lvif_image | lvif_param;
Lvitemadd. iItem = nindex;
Lvitemadd. isubitem = 0;
Lvitemadd. psztext = _ T ("Mao 1 ");;
Lvitemadd. iimage = N;
Lvitemadd. lparam = (lparam) hwnd; (window handle of a window)
If (m_listctrl.insertitem (& lvitemadd )! =-1)
{
Lv_item lvitem = {0 };
Lvitem. Mask = lvif_text;
Lvitem. iItem = nindex;
Lvitem. isubitem = 1;
Lvitem. psztext = _ T ("Mao 2 ");
M_listctrl.setitem (& lvitem );
}
OK. This is a comprehensive example. You can insert the icon and use the param
Clause 6: Click the list box to obtain information about the selected row.
In response to the nm_click message, if you have msdn, you can see that there is a specific
Introduction to nm_click
Void cmydlg: onitemclick (nmhdr * pnmhdr, lresult * presult)
{
// Todo: add your control notification handler code here
Int nitem =-1;
Lpnmitemactivate = (lpnmitemactivate) pnmhdr;
If (lpnmitemactivate! = NULL)
{
Nitem = lpnmitemactivate-> iItem;
}
}
Now nitem is to click the index of the selected row. With index, is it still difficult to obtain the information of the row?
?
Lazy said: It's hard, because you haven't talked about it yet. If you're dizzy, continue.
Clause 7: obtain the information of a row based on its index.
Run the Code directly.
Lv_item lvitem = {0 };
Lvitem. iItem = nindex;
Lvitem. isubitem = 0;
Lvitem. Mask = lvif_text | lvif_image | lvif_param;
M_listctrl.getitem (& lvitem)
In this way, the information in the first column of nindex is taken out, including the icon we just added, and
Additional information (window handle ),
For example, if you want to obtain the window handle, you can use hwnd = (hwnd) lvitem. lparam;
The mask is used to specify the information you want to obtain.
For details, see the definition of lvitem structure in msdn and clistctrl: getitem.
Clause 8: Use a program to select a line
Selected
M_listctrl.setitemstate
(Nindex, lvis_selected | lvis_focused, lvis_selected | lvis_focused );
Deselected. deselect
M_listctrl.setitemstate (nindex, 0, lvis_selected | lvis_focused );
Clause 9: obtain all currently selected rows (multiple rows)
This is a bit lazy. Copy the msdn code. It's very simple.
Example
// Clistctrl * plistctrl = (clistctrl *) getdlgitem
(Idc_yourlistcontrol );
Assert (plistctrl! = NULL );
Position Pos = plist-> getfirstselecteditemposition ();
If (Pos = NULL)
Trace0 ("no items were selected! /N ");
Else
{
While (POS)
{
Int nitem = plist-> getnextselecteditem (POS );
Trace1 ("item % d was selected! /N ", nitem );
// You cocould do your own processing on nitem here
}
}
Clause 10: Delete the row selected in Clause 9
This is more troublesome than the first nine terms, because if you want to delete multiple lines. Errors often occur.
For example, I want to delete 0th rows and 1st rows now (the row sequence of the list starts from 0)
That's good. I deleted it.
M_listctrl.deleteitem (0)
M_listctrl.deleteitem (1)
Congratulations! It's wrong. I'm so happy :)
Because after you delete 0th rows, the row below will move up, so the original 1st rows will become 0th rows, then you m_listctrl.deleteitem (1 ), it is troublesome to delete the original 2nd rows,
Therefore, it is safe to delete data from the bottom up. deleting data first does not affect subsequent operations,
M_listctrl.deleteitem (1)
M_listctrl.deleteitem (0)
However, sometimes we do not know which rows to delete. We only know which rows to delete, such as those in Clause 9.
If we still use
Position Pos = m_listctrl.getfirstselecteditemposition ();
If (Pos = NULL)
Trace0 ("no items were selected! /N ");
Else
{
While (POS)
{
Int nitem = m_listctrl.getnextselecteditem (POS );
M_listctrl.deleteitem (nitem );
}
}
You are waiting for the corpse to be removed.
At this time, we are about B4 Microsoft. For Xiami, there are getlastselecteditemposition and getprevselecteditem.
Writing one or more member functions will die :(
No way. You can think for yourself. Here is a stupid way.
Position sselpos = NULL;
While (sselpos = m_listctrl.getfirstselecteditemposition ())
{
Int nselitem =-1;
Nselitem = m_listctrl.getnextselecteditem (sselpos );
If (nselitem> = 0 & nselitem <m_listctrl.getitemcount ())
{
Okay, this nselitem is the DD we want.
}
}
Getnextselecteditem: According to the usage of msdn, it returns the first index and then goes to the next selected row. Therefore, it is safe to do so. In practice, I did the same, and the test passed. No problem.
Of course, there is another way to first use getfirstselecteditemposition and getnextselecteditem
To obtain the indexes of all selected rows, put these indexes into an array, and then delete them from the bottom up.
Alas, it's really troublesome. I have to worry about arrays. If we don't want to use new to stack them, a vector will always be needed.
So I am using the above method for deletion for your reference and hope to find a better solution.