1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Häufig gestellte Fragen

ListView-Steuerelement mit Hintergrundbild

ListView-Steuerelement mit Hintergrundbild
<URL:https://dotnet.currifex.org/dotnet/faqs/listviewbackgroundimage/de/>
----------------------------------------------------------------------------

Adding a background image to a listview control

The .NET 1.1 listview control doesn't have a property to set its background
image.  This can be done using the code below:

\\\
Imports System.Runtime.InteropServices
.
.
.
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal wMsg As Int32, _
    ByVal wParam As IntPtr, _
    ByRef lParam As LVBKIMAGE _
) As IntPtr

Private Const LVM_FIRST As Int32 = &H1000

' Requires 'CoInitialize' to be called.
Private Const LVM_SETBKIMAGE As Int32 = (LVM_FIRST + 68)

Private Const LVBKIF_SOURCE_URL As Int32 = &H2
Private Const LVBKIF_STYLE_TILE As Int32 = &H10

<StructLayout(LayoutKind.Sequential)> _
Private Structure LVBKIMAGE
    Public ulFlags As Int32
    Public hbm As IntPtr
    Public pszImage As String
    Public cchImageMax As Int32
    Public xOffsetPercent As Int32
    Public yOffsetPercent As Int32
End Structure

Public Function SetListViewBackgroundImage( _
    ByVal ListView As ListView, _
    ByVal BackgroundImageFileName As String _
) As Boolean

    ' Set background image.
    Dim lbi As LVBKIMAGE
    With lbi
        .pszImage = BackgroundImageFileName
        .cchImageMax = Len(.pszImage)
        .ulFlags = LVBKIF_SOURCE_URL Or LVBKIF_STYLE_TILE
    End With
    Return CBool(SendMessage(ListView.Handle, LVM_SETBKIMAGE, IntPtr.Zero, lbi).ToInt32())
End Function
///

Notice that the 'CoInitialize' function must be called before setting the
background image.

Usage:

\\\
Debug.Assert( _
    SetListViewBackgroundImage(Me.ListView1, "C:\WINDOWS\ANGLER.BMP") _
)
///