下列代码给出了一个接管 GridView.CustomDrawColumnHeader 事件的示例,在此事件中自定义绘制视图的 列标头。 在本例中,除去 排序符号 之外,列标头被以默认设置绘制。 使用从 SortingGlyphsImageList 图像列表中获取的图像绘制排序符号。

为了使用默认的机制绘制列标头,调用了 CustomDrawObjectEventArgs.Painter 参数的 DrawObject 方法。 事件的 ColumnHeaderCustomDrawEventArgs.Info 参数被传递到该方法。 注意,在绘制列标头而不绘制默认的排序符号时,这是必要的。 因此,必须清除这些排序符号的边界矩形。 使用事件的 ColumnHeaderCustomDrawEventArgs.Info 参数的 InnerElements 属性来访问边界矩形。

可以参阅 GridDrawColumnObject 演示获得关于自定义绘制列标头的完整示例。

C#CopyCode image复制代码
using DevExpress.XtraGrid.Views.Grid;

private void gridView1_CustomDrawColumnHeader(object sender, 
  ColumnHeaderCustomDrawEventArgs e) {
    if(e.Column == null) return;
    //A rectangle for the sort glyph
    Rectangle sortBounds = Rectangle.Empty;            
    try {
        //Store the rectangle for the sort glyph in sortBounds and
        //then clear this region in the e.Info object
        UpdateInnerElements(e, false, ref sortBounds);
        //Draw the column header without the sort glyph
        e.Painter.DrawObject(e.Info);
    }
    finally {
        //Restore the rectangle for the sort glyph
        UpdateInnerElements(e, true, ref sortBounds);
    }

    if(!sortBounds.IsEmpty) {
        //Perform custom painting of the sort glyph
        DrawCustomSortedShape(e.Graphics, sortBounds, e.Column.SortOrder, 
          SortingGlyphsImageList);
    }            
    e.Handled = true;
}


void UpdateInnerElements(ColumnHeaderCustomDrawEventArgs e, bool restore, 
  ref Rectangle sortBounds) {
    //Locate an element corresponding to the sort glyph
    foreach(DevExpress.Utils.Drawing.DrawElementInfo item in e.Info.InnerElements) {
        if (item.ElementPainter is DevExpress.Utils.Drawing.SortedShapeObjectPainter) {
            if(restore) {
                //Restore the rectangle for the sort glyph
                item.ElementInfo.Bounds = sortBounds;
            } else {
                //Store the rectangle for the sort glyph in sortBounds and
                //then clear this region in the e.Info object
                sortBounds = item.ElementInfo.Bounds;
                item.ElementInfo.Bounds = Rectangle.Empty;
            }                    
        }
    }
}


private static void DrawCustomSortedShape(Graphics g, Rectangle r, ColumnSortOrder so, 
  ImageList iml) {
    //Draw a custom image for the sort glyph
    if (so == ColumnSortOrder.None) return;
    int i = 0;
    if (so == ColumnSortOrder.Descending) i = 1;
    g.DrawImageUnscaled(iml.Images[i], r.X + (r.Width - iml.ImageSize.Width) / 2, r.Y + 
      (r.Height - iml.ImageSize.Height) / 2);
}

Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.Utils.Drawing

Private Sub gridView1_CustomDrawColumnHeader(ByVal sender As Object, _
  ByVal e As ColumnHeaderCustomDrawEventArgs) Handles gridView1.CustomDrawColumnHeader
    If e.Column Is Nothing Then Return
    'A rectangle for the sort glyph
    Dim sortBounds As Rectangle = Rectangle.Empty
    Try
        'Store the rectangle for the sort glyph in sortBounds and
        'then clear this region in the e.Info object
        UpdateInnerElements(e, False, sortBounds)
        'Draw the column header without the sort glyph
        e.Painter.DrawObject(e.Info)
    Finally
        'Restore the rectangle for the sort glyph
        UpdateInnerElements(e, True, sortBounds)
    End Try
    If Not sortBounds.IsEmpty Then
        'Perform custom painting of the sort glyph
        DrawCustomSortedShape(e.Graphics, sortBounds, e.Column.SortOrder, _
          SortingGlyphsImageList)
    End If
    e.Handled = True
End Sub
 
Sub UpdateInnerElements(ByVal e As ColumnHeaderCustomDrawEventArgs, _
  ByVal restore As Boolean, ByRef sortBounds As Rectangle)
    Dim item As DevExpress.Utils.Drawing.DrawElementInfo
    'Locate an element corresponding to the sort glyph
    For Each item In e.Info.InnerElements
        If TypeOf item.ElementPainter Is SortedShapeObjectPainter Then
            If restore Then
                'Restore the rectangle for the sort glyph
                item.ElementInfo.Bounds = sortBounds
            Else
                'Store the rectangle for the sort glyph in sortBounds and
                'then clear this region in the e.Info object
                sortBounds = item.ElementInfo.Bounds
                item.ElementInfo.Bounds = Rectangle.Empty
            End If
        End If
    Next
End Sub

Private Shared Sub DrawCustomSortedShape(ByVal g As Graphics, ByVal r As Rectangle, _
  ByVal so As ColumnSortOrder, ByVal iml As ImageList)
    'Draw a custom image for the sort glyph
    If so = ColumnSortOrder.None Then Exit Sub
    Dim i As Integer = 0
    If so = ColumnSortOrder.Descending Then i = 1
    g.DrawImageUnscaled(iml.Images(i), r.X + (r.Width - iml.ImageSize.Width) / 2, r.Y + _
      (r.Height - iml.ImageSize.Height) / 2)
End Sub