Tipp 0074 Bitmaps in DataGrid-Steuerelement anzeigen
Autor/Einsender:
Datum:
  Klaus D. Raudszus
11.04.2005
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Um Bitmaps im DataGrid-Steuerelement anzeigen zu können, kann das DataGrid-Steuerelement benutzerdefiniert um eine Spalte mit Bitmaps erweitert werden.
Code in modInitDataGrid
 
Imports newDatagridBitmapColumn.bmpDataset
Imports newDatagridBitmapColumn.DataGridBmpColumn
Imports System.Drawing.Drawing2D

Module modInitDataGrid
  Private m_Datagrid As DataGrid

  Public Property modDatagrid() As DataGrid
    Get
      Return m_Datagrid
    End Get
    Set(ByVal Value As DataGrid)
      m_Datagrid = Value
      InitDataGrid()
    End Set
  End Property

  Private Sub InitDataGrid()
    With m_Datagrid
      .ReadOnly = True
      .Size = New Size(384, 312)
      .BackgroundColor = Color.LightSalmon
      .CaptionText = "Bitmap - DataGrid"
      .CaptionBackColor = Color.LightSalmon
      .CaptionForeColor = Color.Black
    End With

    Dim newDataGridTableStyle As DataGridTableStyle = _
          New DataGridTableStyle()
    With newDataGridTableStyle
      .MappingName = "Bitmap"
      .HeaderBackColor = Color.Salmon
      .BackColor = Color.LightSalmon
      .AlternatingBackColor = Color.Salmon
    End With
    '* Die neue Spalte DATAGRIDBMPCOLUMN mit neuen Eigenschaften *
    Dim newBitmapColumn As DataGridBmpColumn = _
          New DataGridBmpColumn()
    With newBitmapColumn
      .MappingName = "Bitmap"
      .HeaderText = "Bmp-Column"
      .Width = 90
      .Alignment = HorizontalAlignment.Center
      .BmpSize = New SizeF(33, 33)
      .BackColorStyle = _
            eBackColorStyle.BackColorLinearGradientBrush
      .BackColorStart = Color.White
      .BackColorEnd = Color.Black
      .BackColorLinearGradientMode = LinearGradientMode.Vertical
    End With
    newDataGridTableStyle.GridColumnStyles.Add(newBitmapColumn)
    '*************************************************************
    Dim newTextColumnLand As DataGridTextBoxColumn = _
          New DataGridTextBoxColumn()
    With newTextColumnLand
      .MappingName = "Land"
      .HeaderText = "normale TextColumn"
      .Width = 150
    End With
    newDataGridTableStyle.GridColumnStyles.Add(newTextColumnLand)
    Dim newTextColumnKont As DataGridTextBoxColumn = _
          New DataGridTextBoxColumn()
    With newTextColumnKont
      .MappingName = "Kontinent"
      .HeaderText = "normale TextColumn"
      .Width = 105
    End With
    newDataGridTableStyle.GridColumnStyles.Add(newTextColumnKont)
    m_Datagrid.TableStyles.Add(newDataGridTableStyle)
    Call FillDataGrid()
  End Sub

  Private Sub FillDataGrid()
    Dim strBilder As String = "..\Bilder\"
    Dim Dataset As bmpDataset = New bmpDataset()
    m_Datagrid.SetDataBinding(Dataset.Bitmap, Nothing)
    With Dataset.Bitmap()
      .AddBitmapRow(strBilder & "Germany.bmp", "Deutschland", _
            "Europa")
      .AddBitmapRow(strBilder & "France.bmp", "Frankreich", _
            "Europa")
      .AddBitmapRow(strBilder & "Kanada.bmp", "Kanada", "Amerika")
      .AddBitmapRow(strBilder & "Japan.bmp", "Japan", "Asien")
      .AddBitmapRow(strBilder & "Italy.bmp", "Italien", "Europa")
      .AddBitmapRow(strBilder & "Spain.bmp", "Spanien", "Europa")
      .AddBitmapRow(strBilder & "GB.bmp", "United Kingdom", _
            "Europa")
      .AddBitmapRow(strBilder & "USA.bmp", "Vereinigte Staaten", _
            "Amerika")
    End With
  End Sub
End Module
 
Code in DataGridBmpColumn
 
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Math

Public Class DataGridBmpColumn
  Inherits DataGridColumnStyle

#Region "Deklarationen"
  Private mBmpSizeF = New SizeF(33, 33)
  Private mHorizontalAlignment As HorizontalAlignment = _
        HorizontalAlignment.Center
  Private mBackColorStart As Color = Color.White
  Private mBackColorEnd As Color = Color.Black
  Private mBackColorStyle As eBackColorStyle = _
        eBackColorStyle.BackColorLinearGradientBrush
  Private mLinearGradientMode As LinearGradientMode = _
        LinearGradientMode.Vertical

  Enum eBackColorStyle
    BackColorAsTable
    BackColorLinearGradientBrush
  End Enum
#End Region

#Region "Property-Region"
  Public Property BackColorLinearGradientMode() As _
        LinearGradientMode
    Get
      Return mLinearGradientMode
    End Get
    Set(ByVal Value As LinearGradientMode)
      mLinearGradientMode = Value
    End Set
  End Property

  Public Property BackColorStyle() As eBackColorStyle
    Get
      Return mBackColorStyle
    End Get
    Set(ByVal Value As eBackColorStyle)
      mBackColorStyle = Value
    End Set
  End Property

  Public Property BmpSize() As SizeF
    Get
      Return mBmpSizeF
    End Get
    Set(ByVal Value As SizeF)
      mBmpSizeF = Value
    End Set
  End Property

  Public Property BackColorStart() As Color
    Get
      Return mBackColorStart
    End Get
    Set(ByVal Value As Color)
      mBackColorStart = Value
    End Set
  End Property

  Public Property BackColorEnd() As Color
    Get
      Return mBackColorEnd
    End Get
    Set(ByVal Value As Color)
      mBackColorEnd = Value
    End Set
  End Property

  Public Overrides Property Alignment() As HorizontalAlignment
    Get
      Return mHorizontalAlignment
    End Get
    Set(ByVal Value As System.Windows.Forms.HorizontalAlignment)
      mHorizontalAlignment = Value
    End Set
  End Property
#End Region

#Region "Sonstige Funktionen und Subs"
  Protected Overrides Function GetMinimumHeight() As Integer
    Return mBmpSizeF.Height
  End Function

  Protected Overrides Function GetPreferredHeight(ByVal g As _
        System.Drawing.Graphics, ByVal value As Object) As Integer
    Return mBmpSizeF.Height
  End Function

  Protected Overrides Function GetPreferredSize(ByVal g As _
        System.Drawing.Graphics, ByVal value As Object) As _
        System.Drawing.Size
    Return New Size(mBmpSizeF.Width, mBmpSizeF.Height)
  End Function

  Protected Overrides Function Commit(ByVal dataSource As _
        System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer) As Boolean
    Return True
  End Function

  Protected Overrides Sub Abort(ByVal rowNum As Integer)
    'ReadOnly
  End Sub

  Protected Overloads Overrides Sub Edit(ByVal source As _
        System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer, ByVal bounds As _
        System.Drawing.Rectangle, ByVal [readOnly] As Boolean)
    'ReadOnly
  End Sub

  Protected Overloads Overrides Sub Edit(ByVal source As _
        System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer, ByVal bounds As _
        System.Drawing.Rectangle, ByVal [readOnly] As Boolean, _
        ByVal instantText As String)
    'ReadOnly
  End Sub

  Protected Overloads Overrides Sub Edit(ByVal source As _
        System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer, ByVal bounds As _
        System.Drawing.Rectangle, ByVal [readOnly] As Boolean, _
        ByVal instantText As String, ByVal cellIsVisible _
        As Boolean)
    'ReadOnly
  End Sub
#End Region

#Region "Paint"
  Protected Overloads Overrides Sub Paint( _
        ByVal g As System.Drawing.Graphics, _
        ByVal bounds As System.Drawing.Rectangle, _
        ByVal source As System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer, _
        ByVal backBrush As System.Drawing.Brush, _
        ByVal foreBrush As System.Drawing.Brush, _
        ByVal alignToRight As Boolean)
    Try
      If mBackColorStyle = _
            eBackColorStyle.BackColorLinearGradientBrush Then
        Dim newBrush = New LinearGradientBrush _
            (New RectangleF(bounds.X, bounds.Y, bounds.Width, _
            bounds.Height), mBackColorStart, mBackColorEnd, _
            mLinearGradientMode)
        g.FillRegion(newBrush, New Region(bounds))
      Else
        g.FillRegion(backBrush, New Region(bounds))
      End If
      Dim BitMapFileName As String = _
            GetColumnValueAtRow(source, rowNum).ToString()
      Dim newBitmap As Bitmap = New Bitmap(BitMapFileName)
      Dim newHeight As Integer = _
            Min(bounds.Height, mBmpSizeF.Height)
      Dim newWidth As Integer = Min(bounds.Width, mBmpSizeF.Width)
      Dim x As Integer = (bounds.Width / 2) - (mBmpSizeF.Width / 2)
      Dim y As Integer = _
            (bounds.Height / 2) - (mBmpSizeF.Height / 2)
      If mHorizontalAlignment = HorizontalAlignment.Right Then
        Dim rx As Integer = bounds.Width - mBmpSizeF.Width
        bounds.Offset(rx, y)
      ElseIf mHorizontalAlignment = HorizontalAlignment.Left Then
        bounds.Offset(0, y)
      Else
        bounds.Offset(x, y)
      End If
      Dim newRectangle = _
            New RectangleF(bounds.X, bounds.Y, newWidth, newHeight)
      g.DrawImage(newBitmap, newRectangle)
    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Protected Overloads Overrides Sub Paint( _
        ByVal g As System.Drawing.Graphics, _
        ByVal bounds As System.Drawing.Rectangle, _
        ByVal source As System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer, _
        ByVal alignToRight As Boolean)
    Try
      Dim BitMapFileName As String = _
            GetColumnValueAtRow(source, rowNum).ToString()
      Dim newBitmap As Bitmap = New Bitmap(BitMapFileName)
      g.DrawImage(newBitmap, bounds)
    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Protected Overloads Overrides Sub Paint( _
        ByVal g As System.Drawing.Graphics, _
        ByVal bounds As System.Drawing.Rectangle, _
        ByVal source As System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer)
    Paint(g, bounds, source, rowNum, False)
  End Sub
#End Region
End Class
 
Weitere Links zum Thema
Spalten farbig unterlegen
Zellen farbig unterlegen 

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (15,8 kB) Downloads bisher: [ 883 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Montag, 23. Januar 2012