Introduction
The standard list box control of MFC does not provide an interface for setting the height of each row.
You can set the following two methods:
1. How to insert an icon to the list box. The height of each row is automatically changed to the height of the icon. This method is fast, but it always feels awkward and difficult to transplant. So I didn't use this method.
2. Self-painted list box. This is exactly what we will talk about in this article.
Effect Preview
Implementation Process
1. Drag a List Control in the dialog box and set the attribute Owen Draw Fixed to TRUR. (The reload code is at the end of this Article)
2. Add an MFC class for the project (here I assume the class name is CMyListCtrl), which is derived from the CListCtrl class of MFC. Reload the CListCtrl: DrawItem method;
3. Add the int m_nRowHeight variable to the CMyListCtrl class to save the specified row height;
4. Add the macro ON_WM_MEASUREITEM_REFLECT () to the MESSAGE_MAP of CMyListCtrl ()
Specifically, add it to the cpp file of CMyLsitCtrl, which looks like this:
BEGIN_MESSAGE_MAP (CMyListCtrl, CListCtrl)
ON_WM_MEASUREITEM_REFLECT ()
END_MESSAGE_MAP ()
5. Reload the CMyListCtrl: MeasureItem method. This step needs to be manually added, and the class wizard does not help you. Note: This step does not add a handler to CMyListCtrl. Do not use the Class Wizard.
Set the Row Height in the function as follows:
void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct){lpMeasureItemStruct->itemHeight = m_nRowHeight;}
PS: Step 4: Why can we do this? This involves the Message reflection mechanism, which is unknown for the moment.
6. Add an interface method for the CMyListCtrl class to set the height of m_nRowHeight;
Void CMyListCtrl: SetRowHeight (int nHeight) {m_nRowHeight = nHeight; // without the following code, the setting does not work. CRect rcWin; GetWindowRect (& rcWin); WINDOWPOS wp; wp. hwnd = m_hWnd; wp. cx = rcWin. width (); wp. cy = rcWin. height (); wp. flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER; SendMessage (WM_WINDOWPOSCHANGED, 0, (LPARAM) & wp );}
7. Add a variable m_MyListCtrl to the list box control added in 1. The type is CMyListCtrl. Call m_MyListCtrl.SetRowHeight (60) in the initialization function of the dialog box ).
8. Add other code, compile and run. If there is no problem, you can see that the setting Row Height works.
The reload CListCtrl: DrawItem code is as follows. You can modify it as needed.
void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {TCHAR lpBuffer[256];LV_ITEM lvi;lvi.mask = LVIF_TEXT | LVIF_PARAM ;lvi.iItem = lpDrawItemStruct->itemID ; lvi.iSubItem = 0;lvi.pszText = lpBuffer ;lvi.cchTextMax = sizeof(lpBuffer);VERIFY(GetItem(&lvi));LV_COLUMN lvc, lvcprev ;::ZeroMemory(&lvc, sizeof(lvc));::ZeroMemory(&lvcprev, sizeof(lvcprev));lvc.mask = LVCF_WIDTH | LVCF_FMT;lvcprev.mask = LVCF_WIDTH | LVCF_FMT;for ( int nCol=0; GetColumn(nCol, &lvc); nCol++){ if ( nCol > 0 ) { // Get Previous Column Width in order to move the next display item GetColumn(nCol-1, &lvcprev) ; lpDrawItemStruct->rcItem.left += lvcprev.cx ; lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ; } // Get the text ::ZeroMemory(&lvi, sizeof(lvi)); lvi.iItem = lpDrawItemStruct->itemID; lvi.mask = LVIF_TEXT | LVIF_PARAM; lvi.iSubItem = nCol; lvi.pszText = lpBuffer; lvi.cchTextMax = sizeof(lpBuffer); VERIFY(GetItem(&lvi)); CDC* pDC; pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if ( lpDrawItemStruct->itemState & ODS_SELECTED ) { pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT)) ; pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ; } else { pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ; pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ; } pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT)); UINT uFormat = DT_LEFT ; ::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer), &lpDrawItemStruct->rcItem, uFormat) ; pDC->SelectStockObject(SYSTEM_FONT) ;}}
Reference: http://www.vckbase.com/index.php/wv/1550
Http://www.360doc.com/content/10/1026/21/799_64276847.shtml
Http://www.codeproject.com/Articles/1401/Changing-Row-Height-in-an-owner-drawn-Control
Http://www.codeproject.com/Articles/295909/Ownerdraw-listctrl-with-transparent-background-and