Type | Description | |||
Item as Variant | A long expression that indicates the item's handle being painted | |||
ColIndex as Variant | A long expression that indicates the column's index or cell's handle, or a string expression that indicates the column's caption | |||
IOwnerDrawHandler | An object that implements the IOwnerDrawHandler interface |
Use the CellOwnerDraw property to paint yourself the cell. The CellOwnerDraw property specifies whether the user is responsible for painting the cell. By default, the CellOwnerDraw property is nothing, and so the control does the painting. Using the notification interfaces is faster than using events. For instance, let's say that we need cells where for some values we need to get displayed other things. Use the Def(exCellOwneDraw) property to assign an owner draw object for the entire column.
The following VB sample displays the CellValue when it is different than 5, and displays another string when the CellValue property is 5:
Implements IOwnerDrawHandler Private Type RECT left As Long top As Long right As Long bottom As Long End Type Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long Private Const DT_VCENTER = &H4 Private Const DT_SINGLELINE = &H20 Private Sub Form_Load() With Grid1.Items .CellValue(.FirstVisibleItem, 0) = 5 Set .CellOwnerDraw(.FirstVisibleItem, 0) = Me End With End Sub Private Sub IOwnerDrawHandler_DrawCell(ByVal hdc As Long, ByVal left As Long, ByVal top As Long, ByVal right As Long, ByVal bottom As Long, ByVal Item As Long, ByVal Column As Long, ByVal Source As Object) Dim s As String s = Source.Items.CellValue(Item, Column) If (s = "5") Then s = "just another string" End If Dim r As RECT r.left = left + 2 r.right = right - 2 r.top = top r.bottom = bottom DrawText hdc, s, Len(s), r, DT_VCENTER Or DT_SINGLELINE End Sub Private Sub IOwnerDrawHandler_DrawCellBk(ByVal hdc As Long, Options As Variant, ByVal left As Long, ByVal top As Long, ByVal right As Long, ByVal bottom As Long, ByVal Item As Long, ByVal Column As Long, ByVal Source As Object) End Sub
The following VB6 sample displays half of the cell's background in green:
Implements IOwnerDrawHandler Private Type RECT left As Long top As Long right As Long bottom As Long End Type Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long Private Const DT_VCENTER = &H4 Private Const DT_SINGLELINE = &H20 Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Sub Form_Load() With Grid1.Items Set .CellOwnerDraw(.FirstVisibleItem, 0) = Me End With End Sub Private Sub IOwnerDrawHandler_DrawCell(ByVal hdc As Long, ByVal left As Long, ByVal top As Long, ByVal right As Long, ByVal bottom As Long, ByVal Item As Long, ByVal Column As Long, ByVal Source As Object) Dim s As String s = Source.Items.CellValue(Item, Column) Dim r As RECT r.left = left + 2 r.right = right - 2 r.top = top r.bottom = bottom DrawText hdc, s, Len(s), r, DT_VCENTER Or DT_SINGLELINE End Sub Private Sub IOwnerDrawHandler_DrawCellBk(ByVal hdc As Long, Options As Variant, ByVal left As Long, ByVal top As Long, ByVal right As Long, ByVal bottom As Long, ByVal Item As Long, ByVal Column As Long, ByVal Source As Object) Dim r As RECT r.left = left + 2 r.right = right - 2 r.top = top r.bottom = bottom r.right = (r.left + r.right) / 2 Dim brush As Long brush = CreateSolidBrush(rgb(0, 255, 0)) FillRect hdc, r, brush DeleteObject brush End Sub