In view of how to achieve CListView I click on a column header to re- arrange it

Category: VC/MFC -> Foundation Classes Author: liuxinwh Date: 2014-04-14 15:53:01
 
liuxinwh
2014-04-14 15:53:01
Sorted
click Sorted
hjkl9999
2014-04-14 15:58:05
4, when setting up a wireless button , you need to note is that only Group attributes a large icon for the selected state, and other wireless button state are the default values.

5, Select List Control control, select "VIEW- > ClassWizard- > Memory   Variables", and use IDC_   LISTCTRL member variables introduced its variable type :

variable name   species   variable type

m_ListCtrl   Control   ClistCtrl

while using "MESSAGES   MAP" for the radio buttons and command buttons to increase control functions.

6, and then in the include files and code files were added the following code :

(1) to increase the data structures and definitions
in VCLISTDlg.h in
typedef   struct   tagSPS   {  // define the structure

char   szPm [10];   // Name

int   Lx;   / / 0-GJ   1-XS

char   szSl [10];   // number

char   szDj [10];   // Price

char   szJe [10];   // amount

}   SPS;

int   CALLBACK   CompareFunc (LPARAM   lParam1, LPARAM   lParam2, LPARAM   lParamSort);

(2) increase the initialization data and programs at the beginning of the definition

// increase in VCLISTDlg.CPP in the beginning of the data in the file structure initialization

SPS   Sps [] = {// information

{" Plum ", 0, " 1000 ", " 30 ", " 30000 "},

{" apricot ", 0, " 1000 ", " 29 ", " 29000 "},

{" green plum ", 0, " 1000 ", " 28 ", " 28000 "},

{" Ome ", 0, " 1000 ", " 27 ", " 27000 "},

{" Hakubaicho ", 0, " 1000 ", " 31 ", " 31000 "},

{" Hongmei ", 1, " 1000 ", " 30 ", " 30000 "},

{" apricot ", 1, " 1000 ", " 29 ", " 29000 "},

{" green, plum ", 1, " 1000 ", " 28 ", " 28000 "},

{" Ome ", 1, " 1000 ", " 27 ", " 27000 "},

{" Hakubaicho ", 1, " 1000 ", " 31 ", " 31000 "}};

CImageList   Cil1, Cil2; // size of the image list

(3) increase the header , images and list controls established at program initialization code

BOOL   CVCLISTDlg :: OnInitDialog ()

{CDialog :: OnInitDialog ();

//......// other code

//   TODO:   Add   extra   initialization   here here to increase code

LV_ITEM   lvitem;

LV_COLUMN   lvcol;

int   i, iPos, iItemNum;

CVCLISTApp   * pApp = (CVCLISTApp   *) AfxGetApp (); // create an image list

Cil1.Create (32,32, TRUE, 2,2);

Cil1.Add (pApp- > LoadIcon (IDI_GJ));

Cil1 .Add (pApp- > LoadIcon (IDI_XS));

Cil2.Create (16,16, TRUE, 2,2);

Cil2.Add (pApp- > LoadIcon (IDI_GJ));

Cil2.Add (pApp- > LoadIcon (IDI_XS)); // set image list

m_ListCtrl.SetImageList (& Cil1, LVSIL_NORMAL);

m_ListCtrl.SetImageList (& Cil2, LVSIL_SMALL); // tabulated

lvcol.mask added to the list of control = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;

lvcol.fmt = LVCFMT_CENTER; // centered

i = 0;

lvcol.pszText = " goods   Name ";

lvcol.iSubItem = i;

lvcol.cx = 70;

m_ListCtrl.InsertColumn (i ++, & lvcol);

lvcol.pszText = " number   amount ";

lvcol.iSubItem = i;

lvcol.cx = 70;

m_ListCtrl.InsertColumn (i ++, & lvcol);

lvcol.pszText = " single   price ";

lvcol.iSubItem = i;

lvcol.cx = 70;

m_ListCtrl.InsertColumn (i ++, & lvcol);

lvcol.pszText = " gold   amount ";

lvcol.iSubItem = i;

lvcol.cx = 70;

m_ListCtrl.InsertColumn (i ++, & lvcol);

// add entries

iItemNum = sizeof (Sps) / sizeof (SPS) to the list control ;

for (i = 0; i < iItemNum; i ++) {

lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;

lvitem.iItem = i;

lvitem.iSubItem=0;

lvitem.pszText = Sps [i] .szPm;

lvitem.iImage = Sps [i] .Lx;

lvitem.lParam = i;

iPos = m_ListCtrl.InsertItem (& lvitem); index number

lvitem.mask // return entries after insertion = LVIF_TEXT;

lvitem.iItem = iPos;

lvitem.iSubItem = 1;

lvitem.pszText = Sps [i] .szSl;

m_ListCtrl.SetItem (& lvitem);

lvitem.iSubItem = 2;

lvitem.pszText = Sps [i] .szDj;

m_ListCtrl.SetItem (& lvitem);

lvitem.iSubItem = 3;

lvitem.pszText = Sps [i] .szJe;

m_ListCtrl.SetItem (& lvitem);

}

CheckRadioButton (IDC_STDICON, IDC_REPORT, IDC_STDICON);

return   TRUE;   //   return   TRUE   unless   you   set   the   focus   to   a   control

}

(4) complete list display mode code

after using Classwizard class wizard to create each function button to display the function , the function must be followed to improve the function of these codes , these features function as follows :

void   CVCLISTDlg :: OnStdicon () // set large icons displayed

{  //   TODO:   Add   your   control   notification   handler   code   here

LONG   lStyle;

lStyle = GetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE); // get the current window type

lStyle & = ~ LVS_TYPEMASK;   / / Clear display mode bit

lStyle | = LVS_ICON;   // set the display mode

SetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE, lStyle); // settings window type

}

void   CVCLISTDlg :: OnSmlicon ()   // small icon display mode settings

{  //   TODO:   Add   your   control   notification   handler   code   here

LONG   lStyle;

lStyle = GetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE); // get the current window Type

lStyle & = ~ LVS_TYPEMASK;   // clear display mode bit

lStyle | = LVS_SMALLICON;   // set the display mode

SetWindowLong(m_ListCtrl.m_hWnd,GWL_STYLE,lStyle);// settings window type

}

void   CVCLISTDlg :: OnList ()   // set list display way

{  //   TODO:   Add   your   control   notification   handler   code   here

LONG   lStyle;

lStyle = GetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE); // get the window type

lStyle current & = ~ LVS_TYPEMASK;   // clear the display mode bit

lStyle | = LVS_LIST;   // set the display mode

SetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE, lStyle); // settings window type

}

void   CVCLISTDlg :: OnReport ()   // detailed information display mode

{  //   TODO:   Add   your   control   notification   handler   code   here

LONG   lStyle;

lStyle = GetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE); // get the window type

lStyle current & = ~ LVS_TYPEMASK;   // clear display mode bit

lStyle | = LVS_REPORT;   // set the display mode

SetWindowLong (m_ListCtrl.m_hWnd, GWL_STYLE, lStyle); / / settings window type

}

realized

(5) delete function to achieve the delete function , must obtain a number of selected entries and table the total number of entries , and need to be forward from the rear in order to delete , the reason is that each table entry is deleted , the subsequent index number of entries is decremented changes will occur , if the method adopted to remove from front to back , it will cause unable to properly delete the selected entry , and its function code is as follows :

void   CVCLISTDlg :: OnDel ()   // delete button function

{  //   TODO:   Add   your   control   notification   handler   code   here

int   i, iState;

int   nItemSelected = m_ListCtrl.GetSelectedCount (); @ selected number of entries

int   nItemCount = m_ListCtrl.GetItemCount (); // entries total

if (nItemSelected < 1)   return;

for (i = nItemCount-1; i > = 0; i -) {

iState = m_ListCtrl.GetItemState (i, LVIS_SELECTED);

if (! iState = 0)  

}

} realized

(6) sorting function ; m_ListCtrl.DeleteItem (i)

list control has a special function, when to display detailed information , a list of the top of the header can be used as a button , a list of which can be controlled by controlling the style created. When a mouse click column header name , the list will be sent a LNV_COLUMNCLICK control message to the parent window , using a list of class -oriented messages IDC_LISTCTRL corresponding control LNV_COLUMNCLICK adding the appropriate handler , you can arrange the table column in a specific order . Its function is to use the program to see which iSort to sort table column index number , (PFNLVCOMPARE) CompareFunc for specific sort of callback function , that is, the process of sorting through a mouse click on the table header to achieve is a special sort by third-party developers function to achieve, sort function is to achieve specific entries comparison operations , and the entire sorting process is by SortItemS property by continuously calling this function to achieve. Normal sorting process is ascending , the sort function by parameter values ​​in exchange , can be achieved in descending order , is about PARAM1 and PARAM2 exchange position. The first two parameters of this callback function tables for table column index number of entries , and the third parameter is the sort of table column index number.

void   CVCLISTDlg :: OnColumnclickListctrl (NMHDR *   pNMHDR,   LRESULT *   pResult)  

{  // the left mouse button , click Table head handler

NM_LISTVIEW *   pNMListView   =   (NM_LISTVIEW *) pNMHDR;

//   TODO:   Add   your   control   notification   handler   code   here

static   int   iSorted = -1; // arrangement number

if   (pNMListView- > iSubItem == iSorted)   return;

iSorted = pNMListView- > iSubItem;

m_ListCtrl.SortItems ((PFNLVCOMPARE) CompareFunc, iSorted);

* pResult   =   0;

}

// sort callback function

compare entries int   CALLBACK   CompareFunc (LPARAM   lParam1,   LPARAM   lParam2, LPARAM   lParamSort)

{  char   * text1, * text2;

switch   (lParamSort) {

case   0L: text1 = Sps [lParam1] .szPm;

text2 = Sps [lParam2] .szPm; break;

case   1L: text1 = Sps [lParam1] .szSl ;

text2 = Sps [lParam2] .szSl; break;

case   2L: text1 = Sps [lParam1] .szDj;

text2 = Sps [lParam2] .szDj; break;

case   3L: text1 = Sps [lParam1] .szJe;

text2 = Sps [lParam2] .szJe; break;

}

return   (strcmp (text1, text2)); // results > 0   = 0   < 0

}

Similarly, you can sort through a dedicated button to implement functions such as sorting the button corresponding to the function code in this article are as follows :

void   CVCLISTDlg :: OnSort ()  

{  //   TODO:   Add   your   control   notification   handler   code   here

m_ListCtrl.SortItems ((PFNLVCOMPARE) CompareFunc, 0 );}

7, the list depending on skill drills

when using list view , which is basically the same methods and the list of control , but in the window list view is to achieve while the list control is achieved in the dialog box , depending on various features list is implemented through the menu and list control with the buttons and other ways to achieve, you need to create a list control list control in the dialog box controls and list view directly occupy the entire window , in the design process as long as the list of control buttons and menu design into the design process , and note that the increase in the class Wizard through the menu commands to operate the function , while increasing to obtain a list of visual references in front of each performance function command (  CListCtrl &   ListCtrl   =   GetListCtrl ()), while the rest of the data structures and code not need to be modified , it is easier to implement .
vazb255
2014-04-14 16:08:45
use listctrl of sortitem functions.
need custom sort callback function , give an example :
////// sorted by name callback function.
int   CALLBACK   CNodeAspect :: SortByName (LPARAM   lParam1,   LPARAM   lParam2,   LPARAM   lParamSort)
{
CNode *   pNode1   =   (CNode * ) lParam1;
CNode *   pNode2   =   (CNode *) lParam2;

int   nRes   =   strcmp (pNode1- > GetName (),   pNode2 - > GetName ());
if   (m_bSBName)
{
if   (nRes   ==   1)
nRes   =   nRes  ! -   2;
else   if   (nRes   ==   -1)
nRes   =   nRes   +   2;
return   nRes;
}

return   nRes;
}
clzcgc313
2014-04-14 16:13:27
ListView default is sorted according to the text , if you want to follow the numerical order, one is the sorted property is set to False, and write your own a sorting program to do it.
Another approach is to use Windows   LVM_SORTITEMS messaging API to specify their size a comparison function to replace the ListView default function .
mdhtss
2014-04-14 16:29:22
have an existing class implementation in codeguru in , you can free yourself
look at where you can achieve a
rockanswer
2014-04-14 16:48:48
( a ) a list of the main functions controlled

list control and visual (List   Control & View) is mainly used in various ways to display a set of data records for the user to perform various operations , Windows98 / 95 in Explorer "View " tab "Great icons | small icon | list | details resources" is a very good typical applications. The record list may include a plurality of data items may also include data indicating the size of the content icons to represent various properties of the data record.

list control provides a basic list of features to the Windows operating methods , and use the list depending on visual function can perform various operations on the list view , get embedded in the list , as the list of control , as the members of the GetListCtrl by calling references (GetListCtrl &   ctrlList   =   GetListCtrl ()), you can control various operations and list the same. A list of controls and basic operation methods Christie is: create a list of control ; list of images needed to create a list of control ; added to the list of controlled items table columns and tables ; various controls on the list , including search, sort, delete , display way , arrangement and various messaging functions ; finally revocation list control .

For a list of control, the most typical of the display control the most commonly used way: large icons mode (LVS_ICON), small icons (LVS_SMALLICON), the list display mode (LVS_LIST) and detailed information ( ie reports LVS_REPORT) display . This is done by setting its display property to achieve. To control where the window list style , function by function GetWindowLong and SetWindowLong to achieve, to control the list of icons of alignment, or by setting the style LVS_ALIGNTOP LVS_ALIGNLEFT list window to achieve ,

( two )

object structure of a control list , a list of established methods

control CListCtrl & listCtrl   structure defines a list of objects

Create   establish a list of control and tie

given object list control CListCtrl :: Create call the following format :

BOOL   Create (  DWORD   dwStyle,   const   RECT &   rect,   CWnd *   pParentWnd,   UINT   nID  );

dwStyle where the parameters used to determine the list of controlled style ; rect used to determine the size and position of the list control ; pParentWnd used to determine the list control 's parent window , usually a dialog box ; nID used to determine the identity of a list control . A list of which can be controlled by a combination of style the following values ​​:

LVS_ALIGNLEFT   used to determine the size of the icon entries displayed left-aligned ;

LVS_ALIGNTOP   used to determine the list the size of the item is displayed in the top of the alignment of icons ;

LVS_AUTOARRANGE   used to determine the size of the icon display entries in automatic arrangement ;

LVS_EDITLABELS   setting can edit text entries , the parent window must have LVN_ENDLABELEDIT style ;

LVS_ICON   used to determine the large icons are displayed ;

LVS_LIST   used to determine the list display ;

LVS_NOCOLUMNHEADER   used to determine when the head of the list does not show details of the way ;

LVS_NOLABELWRAP   used to determine a single line text entry display icons ;

LVS_NOSCROLL   used to shield the scroll bar ;

LVS_NOSORTHEADER   head can not be used to determine the list of button functions ;

LVS_OWNERDRAWFIXED   allow a detailed list of ways when self-painted window ;

LVS_REPORT   used to determine the details of that report to display ;

LVS_SHAREIMAGELISTS used to determine the list of ways to share images ;

LVS_SHOWSELALWAYS   has been used to determine the display selected entries manner ;

LVS_SINGLESEL   used to determine only have one at a time is selected ;

LVS_SMALLICON   used to determine the small icon display mode ;

LVS_SORTASCENDING   used to determine when entries are sorted in ascending order based on the text entry ;

LVS_SORTDESCENDING   used to determine when entries are sorted in descending order based on entries in the text ;

2,
attribute class
list control list control attribute class to obtain a list of controls , including the background color GetBkColor, set the background color of the list control SetBkColor, to obtain a list of image control list GetImageList, set the list control image list SetImageList, get the number of list items GetItemCount, obtaining property GetItem list control entries associated with obtaining data GetItemData, attribute entries SetItem, set the value SetItemData related entries made the next entry GetNextItem associated list of settings control text color SetTextColor, to obtain a list of control text background color GetTextBkColor, SetItemCount set the maximum number of entries and get the number of selected entries GetSelectedCount so on.

3, methods of operation
list control method of operation
control list includes inserting a new entry InsertItem, delete an entry DeleteItem, sort entries SortItems, test list position HitTest, redraw entries RedrawItems, insert a table column InsertColumn, delete a table column DeleteColumn, edit an entry text EditLabel and redraw an entry DrawItem and so on.

(3 ) data structure

list control list control contains two very important data structure LV_ITEM and LV_COLUMN. LV_ITEM definition list for an entry control , LV_COLUMN control used to define a list of columns in a table , which is defined formats are :

typedef   struct   _LV_ITEM   { 

UINT   mask;   // structure members mask bit

int   iItem;   // entry index

int   iSubItem;   / /

sub- entry index UINT   state;   // entry state

UINT   stateMask;   // status validity mask bit   <br iImage; />
LPTSTR   pszText;   //
entry name text
int   cchTextMax;   // entry name maximum length

int     //   entry index number
icons
LPARAM   lParam;   //   32 digits

associated with the entry }   LV_ITEM;  

typedef   struct   _LV_COLUMN   { 

UINT   mask;   // structure members validity mask bit

int   fmt;   // table column alignment

int   cx;   // table column width in pixels

LPTSTR   pszText;   / / table column header names

int   cchTextMax;   // table column name text length

int   iSubItem;   // associated with the table column

child table entry index }   LV_COLUMN;  

fmt which can take the following values ​​:

LVCFMT_CENTER   & nbsp tabulated centered ;

LVCFMT_LEFT   table column left-aligned

( four ) application skills list

control sample demonstrates a list of specific examples given in this article and the previous control application skills header control and image lists. Steps are as follows :

1, by "FILE- > NEW- > PROJECTS- > MFC   AppWizard (EXE)" establishment named VCLIST project, based in the establishment process of selection dialog ( dialog   based) applications ; will delete the default controls in the dialog box , and all of the properties in the Language dialog field is set to Chinese (PRC), in order to make the application supports Chinese ;

establish two icons IDI_GJ and IDI_XS, used to indicate the selected and non- selected state icon , the icon should be established for each 32X32 and 16X16 two sizes to ensure the needs of the program ;  
3, in the dialog window design combo box (Group   Box), set up four wireless button (Radio) combo box " large icons | small icon | | Data " , and set the sort , delete, and close three control buttons (Button), and in dialogue set the size of the box to the right of the list control (List   Ctrl), its corresponding identification are as follows :

---------------------- -------------------------------------------------- --------

control name   title name   identification symbol

---------------- -------------------------------------------------- --------------

list Control   IDC_LISTCTRL

combo box   way   IDC_STATIC

Wireless button   large icons   IDC_STDICON

small icons   IDC_SMLICON

column   table   IDC_LIST

owned   material   IDC_REPORT

button   Pai   sequence   IDC_SORT

delete   except   IDC_DEL

off   closed   IDOK

tracy147852369
2014-04-14 17:04:56

HOWTO:   Sort   a   ListView   Control   by   Date
liuguangyao2010
2014-04-14 17:09:52
cf421 () brother with your code how wrong :
error   C2440:   'type   cast'  :   can not   convert   from   ''   to   'int   (__ stdcall   *) (long, long, long ) '
                None   of   the   functions   with   this   name   in   scope   match   the   target   type
parameter how to set
Thank you , something I have not tried
book_maddevil
2014-04-14 17:12:40
Use   Windows   Form   sorting by columns   ListView   project
caoxiang1992
2014-04-14 17:29:57
how is .net
I use vc ++ 6.0 of
sence6
2014-04-14 17:45:49
can you tell me what it is
codeguru where
luming6009
2014-04-14 18:03:33
on the first floor of 960 379 (Heritage ), CNode what is wrong is how not defined what needs to head into the file?
hnfncls
2014-04-14 18:17:36
codeguru technology website
begegnen
2014-04-14 18:32:32
I'm going to stand up who can help me to write about
int   CALLBACK   CompareFunc (LPARAM   lParam1,   LPARAM   lParam2 , LPARAM   lParamSort) function to sort the contents of ah !
z742848007
2014-04-14 18:39:39
static   int   CALLBACK   CompareFunc (LPARAM   lParam1,   LPARAM   lParam2,   LPARAM   lParamSort)
{
      CListCtrl *   pListCtrl   =   (CListCtrl *)   lParamSort;
      CString         strItem1   =   pListCtrl- > GetItemText (lParam2,   0);
      CString         strItem2   =   pListCtrl- > GetItemText (lParam1,   0);

      return   strcmp (strItem2,   strItem1);
}
cnlong19
2014-04-14 18:50:23
I write should be how to change ah ! Really have no clue
void   CMyListView :: OnColumnclick (NMHDR *   pNMHDR,   LRESULT *   pResult)  
{
NM_LISTVIEW *   pNMListView   =   (NM_LISTVIEW *) pNMHDR;
static   int   iSorted = -1; // arrangement number
CListCtrl &   listCtrl   =   GetListCtrl ();

if   (pNMListView- > iSubItem == iSorted)   return;

iSorted = pNMListView- > iSubItem;

listCtrl.SortItems ((PFNLVCOMPARE) CompareFunc, iSorted) ;

* pResult   =   0;
}

int   CMyListView :: CompareFunc (LPARAM   lParam1,   LPARAM   lParam2,   LPARAM   lParamSort)
{
CListCtrl *   pListCtrl   =   (CListCtrl *)   lParamSort;
      CString         strItem1   =   pListCtrl- > GetItemText (lParam2,   0);
      CString         strItem2   =   pListCtrl- > GetItemText (lParam1,   0) ;

      return   strcmp (strItem2,   strItem1);
}
wishernzl23
2014-04-14 19:04:22
listCtrl.SortItems ((PFNLVCOMPARE) CompareFunc, (DWORD) & listCtrl);
xuechjbj
2014-04-14 19:17:03
how did I click sorting ah !