Clistctrl Terms of Use

Source: Internet
Author: User
Listctrl is often used at work. I often see people posting and asking how to use this control, so I have summarized my experience for reference.
Note: Here, m_listctrl is used to represent a clistctrl Class Object. Here, listctrl is in the report format. For other large icons, we will not talk about small icons for the time being, after all, report is widely used. Secondly, we use Clause 1 and Clause 2 to describe the first and second points. This is a reference to the definition of Objective C ++. I think it is cool.

Clause 1: Set the listctrl style.

On csdn, we often see people asking how to set the style. Their listctrl looks like a list with horizontal and vertical lines. Then, select a row and select all rows, instead of selecting a column, we will provide a 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 );

Where
Lvs_ex_fullrowselect is the entire row selected previously.
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. Remember the Windows Task Manager. If you want to do that, you must add this attribute, I will talk about this later ~

Clause 2: Add a column header

This is a more substantive thing. Separate the list box with a column header.CodeTalking, coming

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 the practice of inserting 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 at the bottom.

Clause 4: insert an icon to the list

You can also insert icons in the report format. Continue with the Code. m_image is a cimagelist object.

M_image.create (16, 16, true | ilc_color24, 3, 1 );
M_listctrl.setimagelist (& m_image, lvsil_small );

Call the cimagelist member function int cimagelist: add (hicon); insert the icon to imagelist, and insert the record

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 is added as follows:

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 );
}
This is a comprehensive example, with icons inserted And Param used.

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 special introduction to the nm_click of listview.
Void cmydlg: onitemclick (nmhdr * pnmhdr, lresult * presult)
{
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 that 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.

Go directly to the Code:
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 the additional information (window handle). For example, I want to obtain the window handle, you can use hwnd = (hwnd) lvitem. lparam; mask is used to specify the information you want to obtain. For details, refer to the definition of lvitem structure in msdn and clistctrl: getitem.

Clause 8: UseProgramSelect a row to select

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)

I am too lazy to copy the msdn Code. It is 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. I deleted it:
M_listctrl.deleteitem (0)
M_listctrl.deleteitem (1)

Congratulations! It's wrong. I'm so happy :) because after you Delete row 0th, the row below will move up, so the original row 1st will become row 0th, then you delete m_listctrl.deleteitem (1). It is really troublesome to delete the original 2nd rows. Therefore, it is safe to delete only the rows from the bottom up, the following operations will not be affected:
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 have to make a mistake! At this time, we have to despise Microsoft. For Xiami, there are getlastselecteditemposition and getprevselecteditem.
Writing one more member function will die: (no way, you can think of it 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 ())
{
// Well, 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. The test also passed. No problem. Of course, there is a way to first get the index of all selected rows through getfirstselecteditemposition and getnextselecteditem, then place these indexes in an array and 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 open them on the stack, a vector will always need it. it's troublesome! So I am using the above method for deletion for your reference and hope to find a better solution.

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.