Clist Control for VC universal control programming

Source: Internet
Author: User
Article title: clist Control for VC universal control programming
Original Author: programunion
Original output: yesky
Publisher: loose_went
release type: Reprinted
Release Date:
today's browser: 17
Overview: 6684

skin ++ software skin replacement kit
· supports multiple development languages
· completely separated interface and business logic, saving development and maintenance time
· completely skin-over, including standard controls and general dialogs
· skin supports the winblinds format, and thousands of skin free downloads
· provides convenient design tools and allows you to design your own skin

(1) Main Functions of list control
List Control and view are mainly used to display a set of data records in various ways for users to perform various operations, the "big icon | small icon | list | detailed resource" under the "View" tab in the resource manager in Windows 98/95 is a typical application. The record in the list can contain multiple data items or the size icon indicating the data content, which is used to indicate various attributes of the data record.

List Control provides basic methods for operating the Windows list function. You can use the list view function to perform various operations on the List View, you can call getlistctrl to obtain the reference (getlistctrl & ctrllist = getlistctrl () embedded in the List View to perform various operations like list control. The basic method for operating a list control and view is: Create a list control, create a list of images required for the list control, add table columns and table items to the list control, and perform various control on the list, it mainly includes searching, sorting, deletion, display mode, arrangement mode, and various message processing functions. Finally, it revokes list control.

For a list control, the most typical and common display control modes are: Large icon mode (lvs_icon), small icon mode (lvs_smallicon), and list display mode (lvs_list) and detailed information (that is, reports lvs_report) display mode. This can be achieved by setting its display mode attribute. To control the style of the window in which the list is located, you can use the getwindowlong and setwindowlong functions. To control the aligntop of the list icon, you can set the style lvs_aligntop or lvs_alignleft of the List window,

(2) object structure of list control

1. Create list controlCubeMethod

Clistctrl & listctrl defines the structure of the list object

Create creates a list control and binds objects.

The call format of list control clistctrl: Create is as follows:

Bool create (DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint NID );

The dwstyle parameter is used to determine the style of list control; rect is used to determine the size and position of list control; pparentwnd is used to determine the parent window of list control, which is usually a dialog box; NID is used to identify the list control. The style of list control can be a combination of the following values:

The lvs_alignleft icon is used to determine the table item size and is displayed in the left-aligned manner;

The lvs_aligntop icon is used to determine the table item size and is displayed in the top aligntop mode;

Lvs_autoarrange is used to determine the table item size icons and display them in an automatic arrangement;

Lvs_editlabels: You can edit the table item text. The parent window must have the lvn_endlabeledit style;

Lvs_icon is used to determine the display mode of the big image;

Lvs_list is used to determine how the list is displayed;

Lvs_nocolumnheader is used to determine that the List header is not displayed in details;

Lvs_nolabelwrap is used to determine the text items for displaying icons in a single row;

Lvs_noscroll is used to block the scroll bar;

Lvs_nosortheader is used to determine that the List header cannot be used as a button;

Lvs_ownerdrawfixed allows you to customize the window when listing details;

Lvs_report is used to determine whether to display details as reports;

Lvs_shareimagelists is used to determine the way an image is shared;

Lvs_showselalways is used to determine how the selected table items are always displayed;

Lvs_singlesel is used to determine that only one item can be selected at a time;

Lvs_smallicon is used to determine how small icons are displayed;

Lvs_sortascending is used to sort table items in ascending order based on the table item text;

When lvs_sortdescending is used to determine the sort of table items, it is based on the descending order of table item text;

2. List control attributes

The list control attribute classes include getbkcolor, setbkcolor, getimagelist, setimagelist, and number of retrieved list items. getitemcount: Get the list control attribute getitem, get the data getitemdata related to the table item, set the attribute setitem of the table item, set the value setitemdata related to the table item, and get the associated next table getnextitem, settextcolor, gettextbkcolor, setitemcount, and getselectedcount.

3. List Control Operations

List control operations include inserting a new table item insertitem, deleting a table item deleteitem, sorting table item sortitems, test list location hittest, redrawing table item redrawitems, and inserting a table column insertcolumn, delete a table column deletecolumn, edit a table item text editlabel, and redraw a table item drawitem.

(3) List-controlled Data Structure

List Control contains two very important data structures: lv_item and lv_column. Lv_item is used to define a table item for list control. lv_column is used to define a table column for list control. The definition formats are as follows:

Typedef struct _ lv_item {

Uint mask; // structure member blocking bit

Int iItem; // Index Number of the table item

Int isubitem; // Index Number of the sub-Table item

Uint state; // table item state

Uint statemask; // status validity blocking bit

Lptstr psztext; // table Item Name text

Int cchtextmax; // maximum Table Name Length

Int iimage; // Index Number of the table item icon

Lparam; // 32-digit table item-related

} Lv_item;

Typedef struct _ lv_column {

Uint mask; // The validity mask of the structure member.

Int FMT; // table column alignment

Int CX; // The pixel width of the table column

Lptstr psztext; // table header name

Int cchtextmax; // The text length of the table column name

Int isubitem; // Index Number of the sub-Table item associated with the table column

} Lv_column;

FMT can take the following values:

Align lvcfmt_center columns in the center

Left alignment of the lvcfmt_left table column

(4) Example of list control

This article provides an example to demonstrate how to control the list and how to apply the header control and image list. The procedure is as follows:

1. Use "file-> New-> projects-> MFC Appwizard (exe)" to create a project named vclist. during creation, select an application based on the dialog box (dialog based; delete the default control in the dialog box, and set the language field in all dialog box properties to Chinese (P. r. c .), to make the applicationProgramSupports Chinese characters;

Two icons, idi_gj and idi_xs, are created to indicate the selected and unselected icons. For each icon, a 32x32 and 16x16 size should be created to ensure the needs of the program;

3. Design a group box in the dialog box window, and set four wireless buttons (radio) to "Big icon | small icon | list | document ", at the same time, set the three buttons for sorting, deletion, and closure, and set the list control (list CTRL) with proper size in the dialog box. The corresponding IDs are as follows:

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

Control name title name identifier

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

List Control idc_listctrl

Idc_static

Wireless button big icon idc_stdicon

Small icon idc_smlicon

Column table idc_list

Resource idc_report

Button sorting idc_sort

Delete idc_del

Turn off the idok

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

4. When setting the wireless button, note that only the group attribute of the big chart is selected, and the status of other wireless buttons is the default value.

5. Select the list control, select "View-> classwizard-> memory variables", and use IDC _ listctrl to introduce member variables. The variable type is:

Variable name type variable type

M_listctrl control clistctrl

"Messages map" is also used to add control functions for wireless buttons and command buttons.

6. ThenCodeAdd the following code to the file:

(1) Add data structures and definitions in vclistdlg. h.

Typedef struct tagsps {// define the structure

Char szpm [10]; // Product Name

Int lx; // 0-gj 1-xs

Char szsl [10]; // quantity

Char szdj [10]; // unit price

Char szje [10]; // amount

} SPS;

Int callback comparefunc (lparam lparam1, lparam lparam2, lparam lparamsort );

(2) Add initialization data and program definitions at the beginning of vclistdlg. cpp.

// Add Data Structure initialization at the beginning of the file

SPS [] = {// Information

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

{"Huang Mei", 0, "1000", "29", "29000 "},

{"Greenplum", 0, "1000", "28", "28000 "},

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

{"Bai Mei", 0, "1000", "31", "31000 "},

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

{"Huang Mei", 1, "1000", "29", "29000 "},

{"Greenplum", 1, "1000", "28", "28000 "},

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

{"Bai Mei", 1, "1000", "31", "31000 "}};

Cimagelist cil1, cil2; // size image list

(3) add header, image, and list control creation code at program Initialization

Bool cvclistdlg: oninitdialog ()

{Cdialog: oninitdialog ();

// ...... // Other code

// Todo: add extra initialization here Add 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); // sets the image list.

M_listctrl.setimagelist (& cil1, lvsil_normal );

M_listctrl.setimagelist (& cil2, lvsil_small); // Add a table column to list control.

Lvcol. Mask = lvcf_fmt | lvcf_subitem | lvcf_text | lvcf_width;

Lvcol. FMT = lvcfmt_center; // center

I = 0;

Lvcol. psztext = "Product Name ";

Lvcol. isubitem = I;

Lvcol. Cx = 70;

M_listctrl.insertcolumn (I ++, & lvcol );

Lvcol. psztext = "quantity ";

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 ";

Lvcol. isubitem = I;

Lvcol. Cx = 70;

M_listctrl.insertcolumn (I ++, & lvcol );

// Add Table items to list control

Iitemnum = sizeof (SPS)/sizeof (SPS );

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); // returns the index number after the table item is inserted.

Lvitem. Mask = 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 the list display method code

After you use the classwizard Class Wizard to create function buttons to display function functions, you must improve the Code of these function functions in sequence. These function functions are as follows:

Void cvclistdlg: onstdicon () // sets the large icon display mode.

{// Todo: add your control notification handler code here

Long lstyle;

Lstyle = getwindowlong (m_listctrl.m_hwnd, gwl_style); // obtain the current window type

Lstyle & = ~ Lvs_typemask; // clear the display mode bit

Lstyle | = lvs_icon; // sets the display mode.

Setwindowlong (m_listctrl.m_hwnd, gwl_style, lstyle); // you can specify the window type.

}

Void cvclistdlg: onsmlicon () // sets the small icon display mode.

{// Todo: add your control notification handler code here

Long lstyle;

Lstyle = getwindowlong (m_listctrl.m_hwnd, gwl_style); // obtain the current window type

Lstyle & = ~ Lvs_typemask; // clear the display mode bit

Lstyle | = lvs_smallicon; // sets the display mode.

Setwindowlong (m_listctrl.m_hwnd, gwl_style, lstyle); // you can specify the window type.

}

Void cvclistdlg: onlist () // sets the list display mode.

{// Todo: add your control notification handler code here

Long lstyle;

Lstyle = getwindowlong (m_listctrl.m_hwnd, gwl_style); // obtain the current window type

Lstyle & = ~ Lvs_typemask; // clear the display mode bit

Lstyle | = lvs_list; // sets the display mode.

Setwindowlong (m_listctrl.m_hwnd, gwl_style, lstyle); // you can specify the window type.

}

Void cvclistdlg: onreport () // detailed information display method

{// Todo: add your control notification handler code here

Long lstyle;

Lstyle = getwindowlong (m_listctrl.m_hwnd, gwl_style); // obtain the current window type

Lstyle & = ~ Lvs_typemask; // clear the display mode bit

Lstyle | = lvs_report; // sets the display mode.

Setwindowlong (m_listctrl.m_hwnd, gwl_style, lstyle); // you can specify the window type.

}

(5) implementation of the delete function

To delete a table, You must retrieve the number of items in the selected table and the total number of items in the table, and delete the items from the back to the back. The reason is that after each table item is deleted, after that, the index numbers of each table item change progressively. If you delete the item from the beginning to the end, the selected table item cannot be deleted normally. The 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 (); // The number of items in the selected table

Int nitemcount = m_listctrl.getitemcount (); // total number of table items

If (nitemselected <1) return;

For (I = nItemCount-1; I> = 0; I --){

Istate = m_listctrl.getitemstate (I, lvis_selected );

If (istate! = 0) m_listctrl.deleteitem (I );

}

}

(6) implementation of the sorting Function

List control has a special function. When detailed information is displayed, the header at the top of the list can be used as a button, which can be controlled by the style when the list is created. When you click the name of the List header, the list control sends an lnv_columnclick message to its parent window, and adds the lnv_columnclick message corresponding to idc_listctrl to the corresponding processing function using the list control in class orientation, you can arrange the columns in a specific order. For how to use the function, see the program. isort is the index number of the sorted table column, and (pfnlvcompare) comparefunc is the callback function for specific sorting. That is to say, by clicking the table header, the sorting process is implemented by a dedicated sorting function developed by a third party. The sorting function only compares table items, the whole sorting process is implemented by the sortitems attribute by constantly calling this function. The normal sorting process is in ascending order. By exchanging the parameter values in the sorting function, you can sort the values in descending order, that is, changing the positions of param1 and param2. The first two parameters of this callback function are the index number of the table items in the table column, and the third parameter is the index number of the sorted table column.

Void cvclistdlg: oncolumnclicklistctrl (nmhdr * pnmhdr, lresult * presult)

{// Left-click the header processing function

Nm_listview * pnmlistview = (nm_listview *) pnmhdr;

// Todo: add your control notification handler code here

Static int isorted =-1; // sequence number

If (pnmlistview-> isubitem = isorted) return;

Isorted = pnmlistview-> isubitem;

M_listctrl.sortitems (pfnlvcompare) comparefunc, isorted );

* Presult = 0;

}

// Callback function for comparing table items during sorting

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); // The result is> 0 = 0 <0

}

Similarly, you can use dedicated buttons to sort data. For example, the Code for the sort button in this article is as follows:

Void cvclistdlg: onsort ()

{// Todo: add your control notification handler code here

M_listctrl.sortitems (pfnlvcompare) comparefunc, 0 );}

7. List View drill skills

When using list view, its method is basically the same as list control, but list view is implemented in the window, and list control is implemented in the dialog box, various functions of list view are implemented through menus, while list control is implemented through buttons, to control the list, you must create a list control Control in the dialog box. The list directly occupies the entire window. during the design process, you only need to change the buttons and list control design process to menu design, note that the function is added by using menu commands in the Class Wizard. At the same time, add the command to get the list view reference (clistctrl & listctrl = getlistctrl () before each function ()), the rest of the data structure and Code do not need to be modified, so it is easier to implement.

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.