VC + + Listctrl Experience Summary _c language

Source: Internet
Author: User

First of all, here, we use M_listctrl to represent a CListCtrl class object, and then here our Listctrl are the statement form, as for other such as what big icon, small icon of the temporary don't speak, after all, the story is the use of public words. Secondly, we use clause one, terms and conditions to describe the 1th, 2nd, this is referred to the "effective C + +" as the name, I think it is more cool:)

Article one: Set the Listctrl style

CSDN often see people ask how to set style, they listctrl look is a list, there is a bar and vertical line, and then select a row, to select the whole line, not only a column is selected, and so on, here to a more comprehensive method of setting.

Get the original style
DWORD dwstyle =:: GetWindowLong (M_listctrl.m_hwnd, gwl_style); 
Dwstyle &= ~ (lvs_typemask);
Dwstyle &= ~ (lvs_editlabels);
Set new style
SetWindowLong (M_listctrl.m_hwnd, Gwl_style,dwstyle, | Lvs_report | Lvs_nolabelwrap | Lvs_showselalways);
Set extension 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 whole line selected
Lvs_ex_gridlines Grid line (only applicable with the Listctrl of the style)
Add a checkbox to the front of the lvs_ex_checkboxes
Plistctrl->setextendedstyle (M_listctrl. GetExtendedStyle () | Lvs_ex_subitemimages);
This is also a very important attribute, so that you can add icon in the list, remember the Windows Task Manager, you want to do so, this attribute to add oh, this I will speak of ~

Clause II: Adding column headers

This is a more substantial thing, to the list box, and then add column header, the code to speak, came

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?? Whatever you do, dots ~ ~
Lvcolumn.mask The mask can have a variety of attributes, specifically to see MSDN Bar,

Article three: Put the record, insert 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 ("Fluffy 1");
if (M_listctrl. InsertItem (&lvitemadd)!=-1)
{ 
  Lv_item Lvitem = {0};
  Lvitem.mask = Lvif_text;
  Lvitem.iitem = nindex;
  Lvitem.isubitem = 1;
  Lvitem.psztext =_t ("Fluffy 2");
  M_listctrl. SetItem (&lvitem);  
}

Nindex is the current number of rows, and then the new line is inserted at the bottom,

Item Four: Insert an icon into the list

You can also insert an icon in the format of the

Continue the code to speak

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 hicon);
Insert the icon into the ImageList,
And then when you insert the record,

Lvitemadd.mask = Lvif_text;
Lvitemadd.mask = lvif_text| Lvif_image

Then add a lvitemadd.iimage = N;
This n is the number in the ImageList, which is the specific icon, list Mody, hehe

Clause V: Use additional information when inserting records, use of LParam

Sometimes you want to add some extra information to a particular line, so you can use this lparam
MSDN is so described specifies the 32-bit value of the item
I was last to add a message to a line, a window handle, and then add,

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 ("Fluffy 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 ("Fluffy 2");
  M_listctrl. SetItem (&lvitem);  
}

OK, this is a relatively full example, and insert icon, and the use of Param

Clause VI: Click on the list box to get the selected line information

Response to Nm_click message, if you have MSDN, you can see that there is a special introduction to ListView's Nm_click

void Cmydlg::onitemclick (nmhdr* pnmhdr, lresult* pResult) 
{
  //Todo:add your control notification handler code he Re
  int nitem =-1;
  Lpnmitemactivate lpnmitemactivate = (lpnmitemactivate) pnmhdr;
  if (lpnmitemactivate!= NULL)
  {
   nitem = lpnmitemactivate->iitem;
  }
}

Now Nitem is click to select the row of index, with the index, get that line of information still difficult?
Lazy said: "Difficult, because you have not said, dizzy, then continue to say

Clause VII: Gets the information for the row based on the index of the row

Just go 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 nindex, the first column of information out, including the icon we added just now, and that extra information (window handle), such as I want to get the window handle, you can HWND = (HWND) Lvitem.lparam;
Mask used to indicate that you want to get that information.
The definition and clistctrl::getitem of Lvitem structure in MSDN can be found in detail

Clause VIII: Select a line with the program to select it

Select the
M_listctrl. SetItemState (nindex,lvis_selected| lvis_focused,lvis_selected| lvis_focused);
Unchecked, uncheck
M_listctrl. SetItemState (nindex,0,lvis_selected| lvis_focused);

Clause IX: Gets all the currently selected rows (multiple selections)

This, I am more lazy, copy the MSDN code, it's very simple anyway

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 are could do your own processing in nitem here
  }
}

Article ten: Delete clause nine of the selected line

This is more troublesome than the previous nine terms, because if you want to delete more than one line, it is often an error. For example, I'm now going to delete lines No. 0 and 1th (the list's row sequence starts at 0)
So good. I'm going to delete it.

M_listctrl. DeleteItem (0)
M_listctrl. DeleteItem (1)

Congratulations, wrong, I am very happy ah:
Because after you delete line No. 0, the following row moves up, then the original 1th line becomes the No. 0 line, then you M_listctrl. DeleteItem (1), then delete is the original line 2nd, really troublesome,
Therefore, only from the bottom to delete, is safe, first deleted, will not affect the subsequent operation,

M_listctrl. DeleteItem (1)
M_listctrl. DeleteItem (0)

But sometimes, we don't know which rows to delete, only the rows that we want to delete, like those in clause nine.
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'll be waiting for the corpse.
At this time we will B4 Microsoft, for the shrimp wood has getlastselecteditemposition and Getprevselecteditem, write a pair of member function will die AH:(
There's a stupid way to do it.

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 our dd
  }

Getnextselecteditem this function,

Look at the use of MSDN, in fact, is to return the first index, and then go to the next selected line, so this is also safe, in practice, I did so, the test also passed, no problem

Of course, there is a way, first through getfirstselecteditemposition and Getnextselecteditem to get all the selected rows of the index, and then put the index into an array, and then from the bottom to delete

Alas really trouble ah, but also indefinite array, do not say with new in the heap open it, then a vector always want it, trouble ah, so I temporarily use the above method to delete, also for everyone's reference, hope to find a better way.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.