About the use of the MFC List control control in C + + original

Source: Internet
Author: User
Tags function prototype strcmp

1\ in the development of the project, the use of the ListControl control, on some issues, make a note, for later use

(1) Delete all items deleteallitems () to the list item;

(2) Add a column to the list item. InsertColumn (0, _t ("number"));

(3) Sets the width of the column for list a items. Setcolumnwidth (0, 50);

(4) can be used before adding a project. SetRedraw (FALSE); To prohibit redrawing, which can improve efficiency. When added is complete, you can use the.  SetRedraw (TRUE); Re-enable redraw

(5) Added items: M_list_iplist.insertitem (3, _t ("4"), 3);

The 1th parameter is the number of rows, and if placed on line No. 0, it is written as 0. The number must be a reasonable number, an unreasonable number, there will be an error

The 2nd parameter is the header of the row

The 3rd parameter is the corresponding display map designator, which will be described later, if not used, set to 1

(6) For non-report projects, article (5) is OK, but for the report style, you also need to add some other columns of information, you can use Setitemtext (0, 1, _t ("192.168.1.4")); To add something else.

(7) You can use SetItemData () to save some important data information in the corresponding line. For program processing

(8) There are several ways to adjust the height of row height, but it is recommended to use CImageList to adjust

There is no function interface for setting the line height of CListCtrl, which can be achieved by self-painting, but it is cumbersome. A simpler approach is to use a blank image to prop up the line so that it changes in height. Examples are as follows:

For example:

CImageList m_image;  M_image. Create (1,ilc_color32,1,0);  M_listinfo.setimagelist (&m_image, Lvsil_small);

(9) For the font settings, we can use the SetFont function to implement. To modify the CListView font as an example, call the Setfontself function before inserting a column in the OnInitialUpdate function (the function is customized as shown in the following example). First create a font and then call SetFont to set it up. It is important to note that you need to delete the created font at exit to avoid a memory leak.

//set font and sizevoidCmylistview::setfontself (intnheight, LPCTSTR lpszfacename) {    //Delete the original font first    if(M_font! =NULL)DeleteM_font; M_font=NewCFont; //Creating FontsM_font->CreateFont (nheight,//nheight        0,//nwidth        0,//nescapement        0,//norientationFw_normal,//NweightFALSE,//BitalicFALSE,//Bunderline        0,//CstrikeoutAnsi_charset,//NcharsetOut_default_precis,//noutprecisionClip_default_precis,//nclipprecisionDefault_quality,//nqualityDefault_pitch | Ff_swiss,//npitchandfamilyLpszfacename);//Lpszfacename//Set FontCListCtrl &thectrl = GetListCtrl ();//get control, reference variableThectrl.setfont (M_font, TRUE);}

(10) Sorting by clicking on the table header

System by sending Lvm_sortitemsMessage to handle the collation problem, in which the handler for the message needs to call a callback function, this callback function needs to be designed to complete the different collation methods. The callback function is prototyped as follows:

int CALLBACK Comparefunc (LPARAM lParam1, LPARAM lParam2, LPARAM lparamsort)

For the above callback functions, the following points need to be made clear:

for parameters lparam1 and LPARAM2, respectively, two rows of data for the CListCtrl, which are the objects used for comparison. The function prototype is set by the member function setitemdata of CListCtrl:

int setitemdata (int nIndex, dword_ptr dwitemdata)

Its first argument is the line number, and the second parameter indicates the parameter for that row. The parameter dwitemdata is usually set to an array of one-line parameters, such as: pdata[2][2] = {{1, 3},{2, 3}}; Use Pdata[i] as dwitemdata each time.

for parameter lparamsort, which indicates the column item, which is the first column. This parameter is set with the callback function through the member function sortitems of CListCtrl, whose function prototype is:

BOOL Sortitems (pfnlvcompare pfncompare,dword_ptr dwdata)

The parameter pfncompare is the callback function entry address, and the parameter dwdata is the column item.

③setitemdata is set when the data is initially inserted, andSortitems is set in the message handler function that responds when the header is clicked.

Examples are as follows:

//initializing a list view controlBOOL Cdataanalysis::initlistctl () {//other processing, including styling, inserting columns, etc.//Insert Row     for(intI=0; i<linenum; i++)    {        //to convert char* to wchar_t*mbstowcs_s (&converted, WSTR, -, M_analysis[i].        Date, _truncate);                                M_listanalysis.insertitem (i, WSTR); //Datembstowcs_s (&converted, WSTR, -, M_analysis[i].        Time, _truncate); M_listanalysis.setitemtext (i,1, WSTR);//Timembstowcs_s (&converted, WSTR, -, M_analysis[i].id, _truncate); M_listanalysis.setitemtext (i,2, WSTR);//IDM_listanalysis.setitemtext (I,3, m_analysis[i].lpszevent);//Events//setting parameters for a callback functionM_listanalysis.setitemdata (i, (LPARAM) (m_analysis+i)); }    returnTRUE;}voidCdataanalysis::onhdnitemclickanalysislist (NMHDR *pnmhdr, LRESULT *PResult) {Lpnmheader PhDr= reinterpret_cast<lpnmheader>(PNMHDR); //Todo:add your control notification handler code here//setting parameters and entry addresses for callback functionsM_listanalysis.sortitems (Sortfunc, phdr->IItem); *presult =0;}//sort of callback functionintCALLBACK Sortfunc (LPARAM lParam1, LPARAM lParam2, LPARAM lparamsort) {intResult//return value//two rows of parameters that are used to compareanalysisformat* pAnalysis1 = (analysisformat*) lParam1; Analysisformat* PAnalysis2 = (analysisformat*) lParam2; //Sort    Switch(lparamsort) { Case 0://Dateresult = strcmp (Panalysis1->date, panalysis2->Date);  Break;  Case 1://Timeresult = strcmp (Panalysis1->time, panalysis2->Time );  Break;  Case 2://IDresult = strcmp (Panalysis1->id, panalysis2->ID);  Break;  Case 3://Eventsresult = wcscmp (panalysis1->lpszevent, panalysis2->lpszevent);  Break; default:         Break; }    returnresult;}

Methods for how to use icons

You need to first bind a list control control to a ImageList project

Methods are as follows

CImageList m_image;  - 1, RGB (2552550));
M_list_iplist.setimagelist (&m_image, Lvsil_small); M_image. Detach ();   This sentence is very important, if there is no such sentence, the icon will not show

The first two sentences mean building a CImagelist project.

The second sentence may not exist on VB6.0 and can be used in VS2012

is to load the bitmap resource directly into the image list

Once the image list is bound to the item that needs to be displayed, the Detach () function is executed once

Purpose: Call this feature to detach the image list from the CImageList object.

For other important functions, refer to the following URL:

http://blog.csdn.net/zwgdft/article/details/7565331

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

About the use of the MFC List control control in C + + original

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.