Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 203,694 times

Contents

Related Categories

ListView Control - Background Images

Background Images

Here's how to display a background image in a ListView. Please note that you do not need this code if you have Visual Basic 6, as you can simply set the Picture property (thanks to Greg Hudson for pointing that out). 


' Extracted & modified from VB Accelerators ListView Play example by
' www.vbweb.f9.co.uk - VB Web - The online guide to Visual Basic
' ===========================================================================
' Author:   Steve McMahon (steve@dogma.demon.co.uk)
' Date:     15 February 1998
' -------------------------------------------------------------
' Visit vbAccelerator - the VB Programmers resource for advanced,free VB
' source code.
'     http://vbaccelerator.com
' ===========================================================================

Private Const NOERROR = &H0&
Private Const S_OK = &H0&
Private Const S_FALSE = &H1&
Private Const LVM_FIRST = &H1000
Private Const LVM_SETBKIMAGE = (LVM_FIRST + 68)
Private Const LVM_SETTEXTBKCOLOR = (LVM_FIRST + 38)
Private Const LVBKIF_SOURCE_URL = &H2
Private Const LVBKIF_STYLE_TILE = &H10
Private Const CLR_NONE = &HFFFFFFFF
' Bitmaps in list views!
Private Type LVBKIMAGE
    ulFlags As Long
    hbm As Long
    pszImage As String
    cchImageMax As Long
    xOffsetPercent As Long
    yOffsetPercent As Long
End Type

Private Declare Sub CoUninitialize Lib "OLE32.DLL" ()
Private Declare Function CoInitialize Lib "OLE32.DLL" (ByVal pvReserved As Long) As Long
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub SetBackground()
Dim sI As String
Dim lHDC As Long
    
   ' Set a background image:
   sI = "BACK.GIF"
   
   If (Len(sI) > 0) Then
      If (InStr(sI, "")) = 0 Then
         sI = App.Path & "" & sI
      End If
      On Error Resume Next
      If (Dir(sI) <> "") Then
         If (Err.Number = 0) Then
            ' Set the background:
            Dim tLBI As LVBKIMAGE
            tLBI.pszImage = sI & Chr$(0)
            tLBI.cchImageMax = Len(sI) + 1
            tLBI.ulFlags = LVBKIF_SOURCE_URL Or LVBKIF_STYLE_TILE
            SendMessage lvwTest.hwnd, LVM_SETBKIMAGE, 0, tLBI
            ' Set the background colour of the ListView to &HFFFFFFFF (-1)
            ' so it will be transparent!
            SendMessageLong lvwTest.hwnd, LVM_SETTEXTBKCOLOR, 0, CLR_NONE
         Else
            MsgBox "Error with File '" & sI & "' :" & Err.Description & ".", vbExclamation
         End If
      Else
         MsgBox "File '" & sI & "' not found.", vbExclamation
      End If
   End If

End Sub

Private Sub Form_Load()
    Dim i As Byte
    Dim itmX As ListItem
    Dim lR As Long
    With lvwTest
        '// required for using bitmaps
        lR = CoInitialize(0)
        Debug.Print lR
        If (lR <> NOERROR) And (lR <> S_FALSE) Then
            Debug.Print "CoInitialize failed"
        End If
        .ColumnHeaders.Add , "H1", "Col1"
        .ColumnHeaders.Add , "H2", "Col2"
        .ColumnHeaders.Add , "H3", "Col3"
        .ColumnHeaders.Add , "H4", "Col4"
        Randomize
        For i = 1 To 20
            ' Add text
            Set itmX = .ListItems.Add(, "C" & i, "Test Item " & i)
            
            ' Col2= Col2 + Item
            itmX.SubItems(1) = "Col2 " & i
            ' Col3= Item Number
            itmX.SubItems(2) = i
        Next i
        SetBackground
   End With
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [74] ListView Control

    Posted by SeanH on 14 Nov 2007

    How to sort a ListView control? Here is the code I have written to handle just that. This code also includes a context menu of items I found I needed on almost every ListView I used (I use them alo...

  • Re: [74] ListView Control

    Posted by SeanH on 14 Nov 2007

    [quote user="Maxjonz"]

    I found that the following code didn't work for me


    ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item...

  • Re: [74] ListView Control

    Posted by anatha1 on 10 Aug 2007

    Hey everyone here are some example of Database that use ListView:


    if u need in C# pls copy this code convert to C# your self.



    1. Use Microsoft Access Database

    ...

  • Re: [74] ListView Control

    Posted by Halina on 08 Aug 2007

    Hi..


    I using ListView in C#.


    I tried to add text into certain colum in ListView.


    For example: I have 3 columns, Column1, Column2, Column3.


    I want to insert "TEST1"&n...

  • Re: [74] ListView Control

    Posted by Maxjonz on 05 Jul 2007

    I found that the following code didn't work for me


    ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item


    I had t...