C++ about MFC List Control Control Control summary
- 2020-04-02 03:05:21
- OfStack
1\ in the development of the project, the use of the listcontrol control control, some problems, make a note for future use
(1) Delete all items to the list item DeleteAllItems ();
(2) add a column.InsertColumn(0, _T(" number ")) to the list item;
(3) set the column width of list a item.SetColumnWidth(0, 50);
(4).setredraw (false) can be used before adding the project; When the addition is complete, you can use.setredraw (true); Repaint is enabled
(5) add project: M_List_IpList. InsertItem (3, _T (" 4 "), 3);
The first parameter is the number of rows, If you put it on line 0, write it as 0. The number here has to be a reasonable number
The second argument is the row title
The third parameter is corresponding I'm going to show you the icon number, but if you don't use it, I'm going to set it to -1
(6) for non-report items, section (5) is fine, but for the report style, you need to add some other column information that you can use 192.168.1.4 SetItemText (0, 1, _T (" ")); To add something else
(7) SetItemData() can be used to store some important data information in corresponding rows
(8) adjustment of row height There are several ways to adjust the row height, but it is recommended to use Cimagelist to adjust it
There is no functional interface to set the line height of CListCtrl, which can be achieved by drawing, but it is more troublesome. An easier way to change the height of a row is to use a blank image to prop it up. Here's an example:
Such as:
CImageList m_image;
m_image.Create(1,24,ILC_COLOR32,1,0);
m_listInfo.SetImageList(&m_image, LVSIL_SMALL);
(9) For the font Settings, we can use the SetFont function to achieve. To modify the font of the CListView, for example, call the SetFontSelf function (which is custom, as shown in the following example) before inserting the column in the OnInitialUpdate function. First create a font, then call SetFont to set it. Note that you need to delete the created font when exiting to avoid memory leaks.
//Set the font and size
void CMyListView::SetFontSelf(int nHeight, LPCTSTR lpszFacename)
{
//Delete the original font first
if(m_font != NULL)
delete m_font;
m_font = new CFont;
//Create a font
m_font->CreateFont(
nHeight, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
lpszFacename); // lpszFacename
//Set the font
CListCtrl &theCtrl = GetListCtrl(); //Get control, reference variables
theCtrl.SetFont(m_font, TRUE);
}
(10) click on the table head to sort
The system handles the categorization problem by sending a message down to the l_sortitems. In the processing function of the message, a callback function needs to be called. This callback function needs to be designed to complete the different categorization methods. The callback function prototype is as follows:
Int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
For the above callback function, the following points need to be clear:
For parameters lparam1 and lparam2, respectively, are two lines of data of CListCtrl, is used for comparison. Set by the CListCtrl member function SetItemData, the function prototype:
Int SetItemData (int nIndex, DWORD_PTR dwItemData)
The first parameter is the line number, and the second parameter indicates the corresponding parameter of the line. The parameter dwItemData is usually set as an array of one-row parameters, such as: pData[2][2] = {{1, 3},{2, 3}}; Each time pData[I] is used as dwItemData.
For the parameter lParamSort, used to specify the column item, which column. This parameter is set together with the callback function by the member function SortItems of CListCtrl, whose function prototype is:
BOOL SortItems(PFNLVCOMPARE pfnCompare,DWORD_PTR dwData)
The parameter pfnCompare is the entry address of the callback function, and the parameter dwData is the column item.
SetItemData is called to set when the data is initially inserted, and SortItems is set in the message processing function that responds when the list header is clicked.
Here's an example:
//Initializes the list view control
BOOL CDataAnalysis::InitListCtl()
{
//Other processing, including setting styles, inserting columns, and so on
//Insert row
for(int i=0; i<LineNum; i++)
{
//To convert char* to wchar_t*
mbstowcs_s(&converted, wStr, 30, m_analysis[i].Date, _TRUNCATE);
m_listAnalysis.InsertItem(i, wStr); //The date of
mbstowcs_s(&converted, wStr, 30, m_analysis[i].Time, _TRUNCATE);
m_listAnalysis.SetItemText(i, 1, wStr); //time
mbstowcs_s(&converted, wStr, 30, m_analysis[i].ID, _TRUNCATE);
m_listAnalysis.SetItemText(i, 2, wStr); //ID
m_listAnalysis.SetItemText(i, 3, m_analysis[i].lpszEvent); //The event
//Sets the parameters of the callback function
m_listAnalysis.SetItemData(i, (LPARAM)(m_analysis+i));
}
return TRUE;
}
void CDataAnalysis::OnHdnItemclickAnalysisList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
// TODO: Add your control notification handler code here
//Sets the parameters of the callback function And entry address
m_listAnalysis.SortItems(SortFunc, phdr->iItem);
*pResult = 0;
}
//Sort of the callback function
int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
int result; //The return value
//Two line arguments for comparison
ANALYSISFORMAT* pAnalysis1 = (ANALYSISFORMAT*)lParam1;
ANALYSISFORMAT* pAnalysis2 = (ANALYSISFORMAT*)lParam2;
//The sorting
switch(lParamSort)
{
case 0: //The date of
result = strcmp(pAnalysis1->Date, pAnalysis2->Date);
break;
case 1: //time
result = strcmp(pAnalysis1->Time, pAnalysis2->Time);
break;
case 2: //ID
result = strcmp(pAnalysis1->ID, pAnalysis2->ID);
break;
case 3: //The event
result = wcscmp(pAnalysis1->lpszEvent, pAnalysis2->lpszEvent);
break;
default:
break;
}
return result;
}
How to use ICONS
You first need to bind the list control control to an imagelist project
Methods the following
CImageList m_image;
m_image.Create(IDB_IP_BITMAP, 16, 1, RGB(255, 255, 0));
m_List_IpList.SetImageList(&m_image, LVSIL_SMALL);
m_image.Detach(); //This sentence is very important, if there is no this sentence, the icon will not show
Create a CImagelist project
The second sentence may not exist on VB6.0 but can be used in VS2012
This is to load bitmap resources directly into the list of images
After you bind the list of images to the items you want to display, perform the Detach() function once
Objective: Call this function to separate the list of images from the CImageList object.
The above is all the content of this article, I hope you can enjoy it.