Adding a Background Image to a ListView Control

The .NET Framework 1.1 listview control does not 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
    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") _
)