|
楼主 |
发表于 2023-3-25 00:18
|
显示全部楼层
核心代码。处理WM_MEASUREITEM和WM_DRAWITEM消息,绘制ITEM和SUBITEM的边框和文字。
- Private Function OnMeasureItem(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long, ByRef bHandled As Boolean) As Long
- Dim mis As MEASUREITEMSTRUCT
- OnMeasureItem = 0
- bHandled = False
-
- Call CopyMemory(mis, ByVal lParam, Len(mis))
-
- If (mis.CtlType = ODT_LISTVIEW And GetDlgItem(m_hParentWnd, mis.CtlID) = m_hWnd) Then
- mis.ItemHeight = m_nItemHeight
-
- Call CopyMemory(ByVal lParam, mis, Len(mis))
-
- bHandled = True
- OnMeasureItem = 1
- End If
- End Function
- Private Function OnDrawItem(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long, ByRef bHandled As Boolean) As Long
- Dim dis As DRAWITEMSTRUCT
- Dim rcSubItem As RECT, rcHeaderItem As RECT
-
- Dim nHeaderItemCount As Long
- Dim i As Integer
-
- OnDrawItem = 0
- bHandled = False
-
- Call CopyMemory(dis, ByVal lParam, Len(dis))
-
- If (dis.CtlType = ODT_LISTVIEW And GetDlgItem(m_hParentWnd, dis.CtlID) = m_hWnd) Then
-
- ' 获取列表头个数
- nHeaderItemCount = SendMessage(m_hHeaderWnd, HDM_GETITEMCOUNT, 0, 0)
- Call SetRect(rcSubItem, dis.rcItem.Left, dis.rcItem.Top, dis.rcItem.Left, dis.rcItem.Bottom)
-
- For i = 0 To nHeaderItemCount - 1
-
- Call SendMessage(m_hHeaderWnd, HDM_GETITEMRECT, i, rcHeaderItem)
-
- rcSubItem.Left = rcSubItem.Right
- rcSubItem.Right = rcSubItem.Left + rcHeaderItem.Right - rcHeaderItem.Left
-
- Call OnDrawSubItem(dis, i, rcSubItem)
- Next
- End If
-
- End Function
- Private Sub OnDrawSubItem(dis As DRAWITEMSTRUCT, ByVal nSubItem As Long, rcSubItem As RECT)
- Dim hdi As HDITEM
- Dim li As LVITEM
- Dim szBuff() As Byte
- Dim szText As String
- Dim wTextFormat As Long
- Dim rcPaint As RECT, rcIntersect As RECT
- Dim hOldBrush As Long
- Dim hOldPen As Long
-
- Call GetBoundsRect(dis.hdc, rcPaint, 0)
- If IsRectEmpty(rcPaint) Then
- Call GetClientRect(m_hWnd, rcPaint)
- End If
- Call IntersectRect(rcIntersect, rcPaint, rcSubItem)
-
- If Not IsRectEmpty(rcPaint) Then
-
- ' 设置绘图边界
- Call SetBoundsRect(dis.hdc, rcIntersect, DCB_SET)
- ' 获取列表项文本
- li.mask = LVIF_TEXT
- li.iItem = dis.itemID
- li.iSubItem = nSubItem
- li.stateMask = -1
- li.pszText = LocalAlloc(&H40, 255)
- li.cchTextMax = 255
- Call SendMessage(m_hWnd, LVM_GETITEMTEXTA, li.iItem, li)
-
- szBuff = StrConv(String$(255, 0), vbFromUnicode)
- Call CopyMemory(szBuff(0), ByVal li.pszText, 255)
- Call LocalFree(li.pszText)
-
- szText = StrConv(szBuff, vbUnicode)
- szText = Left$(szText, InStr(1, szText, vbNullChar) - 1)
-
- ' 获取列表头对齐格式
- hdi.mask = HDI_FORMAT
- Call SendMessage(m_hHeaderWnd, HDM_GETITEMA, li.iSubItem, hdi)
- wTextFormat = DT_LEFT Or DT_VCENTER Or DT_SINGLELINE
- If (hdi.fmt And HDF_CENTER) Then
- wTextFormat = wTextFormat Or DT_CENTER
- ElseIf (hdi.fmt And HDF_RIGHT) Then
- wTextFormat = wTextFormat Or DT_RIGHT
- End If
-
- ' 填充背景
- If ShowGird = True Then
- If (dis.itemState And ODS_SELECTED) Then
- hOldBrush = SelectObject(dis.hdc, m_hSelectedItemBkBrush)
- ElseIf m_bItemBkColorHorizontal = True Then
- hOldBrush = SelectObject(dis.hdc, IIf(nSubItem Mod 2, m_hItemBkBrush1, m_hItemBkBrush2))
- Else
- hOldBrush = SelectObject(dis.hdc, IIf(dis.itemID Mod 2, m_hItemBkBrush1, m_hItemBkBrush2))
- End If
- hOldPen = SelectObject(dis.hdc, m_hGridPen)
- Call Rectangle(dis.hdc, rcSubItem.Left, rcSubItem.Top - 1, rcSubItem.Right + 1, rcSubItem.Bottom)
- Else
- If (dis.itemState And ODS_SELECTED) Then
- Call FillRect(dis.hdc, rcSubItem, m_hSelectedItemBkBrush)
- ElseIf m_bItemBkColorHorizontal = True Then
- Call FillRect(dis.hdc, rcSubItem, IIf(nSubItem Mod 2, m_hItemBkBrush1, m_hItemBkBrush2))
- Else
- Call FillRect(dis.hdc, rcSubItem, IIf(dis.itemID Mod 2, m_hItemBkBrush1, m_hItemBkBrush2))
- End If
- End If
-
- Call InflateRect(rcSubItem, -1, -1)
- Call DrawText(dis.hdc, szText, -1, rcSubItem, wTextFormat)
- Call InflateRect(rcSubItem, 1, 1)
-
- ' 还原设备上下文
- If (hOldBrush <> 0) Then Call SelectObject(dis.hdc, hOldBrush)
- If (hOldPen <> 0) Then Call SelectObject(dis.hdc, hOldPen)
- Call SetBoundsRect(dis.hdc, rcPaint, DCB_SET)
- End If
- End Sub
复制代码
|
|