65.9K
CodeProject 正在变化。 阅读更多。
Home

非常漂亮的 DataGridView

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.63/5 (8投票s)

2008年2月17日

CPOL
viewsIcon

89734

downloadIcon

1446

DataGridView 中非常漂亮的外观和感觉

NiceGridView.JPG

引言

本文演示了如何通过处理 DataGridView CellPainting 事件,使 .net DataGridView 具有美观的外观和感觉。

绘制列标题的函数。

Protected Sub DrawColumnHeader(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        Dim h As Integer = e.CellBounds.Height
        Dim w As Integer = e.CellBounds.Width
        Dim h1 As Integer = h * 0.4
        Dim ct As New OfficeColorTable
        Dim r1 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, w, h1)
        Dim r2 As Rectangle = New Rectangle(e.CellBounds.X, h1, w, h - h1 + 1)
        Dim lb1 As LinearGradientBrush = _
    New LinearGradientBrush(r1, ct.ColumnHeaderStartColor, ct.ColumnHeaderMidColor1, Drawing2D.LinearGradientMode.Vertical)
        Dim lb2 As LinearGradientBrush = _
    New LinearGradientBrush(r2, ct.ColumnHeaderMidColor2, ct.ColumnHeaderEndColor, Drawing2D.LinearGradientMode.Vertical)
        Dim p As Pen = New Pen(ct.GridColor, 1)
        Dim frmt As StringFormat = New StringFormat
        frmt.Alignment = StringAlignment.Center
        frmt.FormatFlags = StringFormatFlags.DisplayFormatControl
        frmt.LineAlignment = StringAlignment.Center
        With e.Graphics
            .FillRectangle(lb1, r1)
            .FillRectangle(lb2, r2)
            .DrawRectangle(p, e.CellBounds)
        End With
    End Sub

绘制 DataGridView 单元格的函数。
Protected Sub DrawCell(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        Dim r1 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)
        Dim r2 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)
        Dim ct As New OfficeColorTable
        Dim cellColor As Color
        Dim borderColor As Color
        Dim p As Pen
        If e.State = 97 Or e.State = 105 Then
            borderColor = ct.GridColor
            cellColor = ct.ActiveCellColor
            p = New Pen(borderColor, 1)
        Else
            borderColor = ct.GridColor
            p = New Pen(borderColor, 1)
            If e.State = 109 Then
                cellColor = ct.ReadonlyCellColor
            Else
                cellColor = ct.DefaultCellColor
            End If

        End If
        If e.ColumnIndex < 0 Then
            cellColor = ct.ColumnHeaderMidColor2
        End If
        With e.Graphics
            .FillRectangle(New SolidBrush(cellColor), e.CellBounds)
            Dim rnd As New Renderer
            If e.State = 97 Then
                rnd.Fill3DRectangle(e.CellBounds, Renderer.RenderingMode.Office2007OrangeHover, e.Graphics)
            End If
            If e.ColumnIndex < 0 Then
                If e.State = 97 Then
                    rnd.FillGradientRectangle(e.CellBounds, Renderer.RenderingMode.Office2007OrangeHover, e.Graphics)
                Else
                    rnd.FillGradientRectangle(e.CellBounds, Renderer.RenderingMode.Office2007GrayHover, e.Graphics)
                End If
            End If
            .DrawRectangle(p, e.CellBounds)
            .DrawRectangle(p, New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, Me.Height))
        End With

    End Sub
非常漂亮的 DataGridView - CodeProject - 代码之家
© . All rights reserved.