Colourizing Listviews 
Author Message
 Colourizing Listviews

Hi there,

is there anyway I can change the font/appearance of a listview item in a
specific column.
If it is, is it possible it can varied for each item.

Any response gratefully appreciated.

Andy



Thu, 14 Feb 2002 03:00:00 GMT  
 Colourizing Listviews

See on adress
www.mvps.org/vbnet/
there is an article on how to Customizing the Listview's Appearance via API
and click on Search VBNet and enter
listview

Here is the screen copy of article

Visual Basic ListView API Routines
Posted Friday June 19, 1998
Updated Sunday August 22, 1999

Customizing the Listview's Appearance via API

Related Topics

VBnet Frames
Applies to: VB4-32, VB5, VB6*

 Prerequisites

This method is intended for Visual Basic 5 or Visual Basic 6 where the
Common Control library used is the MSComCtl 5
version (comctl32.ocx).
Because the VB6-specific mscomctl.ocx (Common Controls 6) is a complete
implementation of comctl32.dll and not
reliant on the version of
comctl32.dll installed, this routine may not work when applied to a listview
created from the VB6-specific mscomctl.ocx.

Enhanced Comctl32 functionality is only available to users with comctl32.dll
version 4.70 or greater installed. This dll is
typically installed with IE3.x
or greater.

Once again, the SendMessage API, in conjunction with the new extended style
flags
for the ListView control, provides a means to customize the appearance of
the listview
common control in a Visual Basic application.

By combining the new LVBKIMAGE structure (Type), and specifying the
LVBKIF_SOURCE_URL Or LVBKIF_STYLE_TILE style flags, you can apply a
background image to the control.

Obviously, the image applied must be tasteful enough to allow the data to be

presented. Too garish a background only detracts from the appearance of the
app.  In
addition, the background scrolls with the listview. This is not of concern
when
LVBKIF_STYLE_TILE is used (illustration below, left) ... the background
always has
the image, but without it (the main illustration to the right), the graphic
will scroll with
the data.

One of the added benefits of this feature is you can now add a graphic to
the control to
indicate that there is no data available (below, right).

The LVBKIMAGE is only available on systems that has the comctl32.dll version

provided with IE4 and other later MS products installed.

 BAS Module Code

Place the following code into the general declarations area of a bas module:

Option Explicit

Public Type LVBKIMAGE
   uFlags As Long
   hBmp As Long
   pszImage As String
   cchImageMax As Long
   xOffsetPercent As Long
   yOffsetPercent  As Long
End Type

Public Type tagINITCOMMONCONTROLSEX
    dwSize As Long
    dwICC As Long
End Type

Public Const ICC_LISTVIEW_CLASSES As Long = &H1
Public Const CLR_NONE = &HFFFFFFFF
Public Const LVM_FIRST As Long = &H1000
Public Const LVBKIF_SOURCE_NONE = &H0
Public Const LVBKIF_SOURCE_HBITMAP = &H1
Public Const LVBKIF_SOURCE_URL = &H2
Public Const LVBKIF_SOURCE_MASK = &H3
Public Const LVBKIF_STYLE_NORMAL = &H0
Public Const LVBKIF_STYLE_TILE = &H10
Public Const LVBKIF_STYLE_MASK = &H10
Public Const LVM_SETBKIMAGEA = (LVM_FIRST + 68)
Public Const LVM_SETBKIMAGEW = (LVM_FIRST + 138)
Public Const LVM_GETBKIMAGEA = (LVM_FIRST + 69)
Public Const LVM_GETBKIMAGEW = (LVM_FIRST + 139)
Public Const LVM_SETBKIMAGE = LVM_SETBKIMAGEA
Public Const LVM_GETBKIMAGE = LVM_GETBKIMAGEA
Public Const LVM_SETTEXTBKCOLOR = (LVM_FIRST + 38)
Public Const LVM_SETEXTENDEDLISTVIEWSTYLE As Long = (LVM_FIRST + 54)
Public Const LVS_EX_FULLROWSELECT As Long = &H20

Public Declare Function SendMessageAny Lib "User32" _
   Alias "SendMessageA" _
  (ByVal hWnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Public 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

Public Declare Sub InitCommonControls _
    Lib "comctl32.dll" ()

Public Declare Function InitCommonControlsEx _
    Lib "comctl32.dll" _
   (lpInitCtrls As tagINITCOMMONCONTROLSEX) As Boolean

'--end block--'

 Form Code

Create a form like the one shown, with a listview, an imagelist containing a
16x16 icon or bitmap, five command buttons
and three text boxes
(txtNoData, txtPixPathTile and txtPixPathCentered). Set the textbox paths to
point to the files you want to use for the
demo. Set the listview style to
report mode, then add the following code to the form:

Option Explicit

Private Sub Form_Load()

   With ListView1
      .SortKey = 0
      .SmallIcons = ImageList1
      .ColumnHeaders(1).Width = .Width - 600
      .View = lvwReport
   End With

   Call InitComctl32(ICC_LISTVIEW_CLASSES)

  'An alternate way to set the extended listview styles!
   Call SendMessageLong(ListView1.hWnd, _
                        LVM_SETEXTENDEDLISTVIEWSTYLE, _
                        LVS_EX_FULLROWSELECT, True)

   DisplayNoDataImage

End Sub

Private Sub cmdEnd_Click()

   Unload Me

End Sub

Private Sub Form_Unload(Cancel As Integer)

   Set Form1 = Nothing

End Sub

Private Sub BuildLVdata()

   Dim itmX As ListItem
   Dim imgX As ListImage
   Dim nItem As Long
   Dim sItem As String
   Dim c As Integer

   For c = 1 To 20

      sItem = "Listview listitem #" & c
      Set itmX = ListView1.ListItems.Add(, , Trim$(sItem))

      itmX.SmallIcon = ImageList1.ListImages("page").Key

   Next

  'free the objects
   Set itmX = Nothing
   Set imgX = Nothing

End Sub

Private Sub DisplayNoDataImage()

   Dim BKIMG As LVBKIMAGE

   With BKIMG

      If ListView1.ListItems.Count > 0 Then

       'remove the 'no data' image by clearing
       'the image flag
        .uFlags = LVBKIF_SOURCE_NONE

      Else
        'set the image to the 'no data text' image
        'Specifying LVBKIF_SOURCE_URL indicates that the
        'lParam member in SendMessage contains the
        'path/filename of the image.
        .uFlags = LVBKIF_SOURCE_URL
        .pszImage = (txtNoData)
        .xOffsetPercent = 3
        .yOffsetPercent = 3
      End If

   End With

   Call SendMessageLong(ListView1.hWnd, _
                        LVM_SETTEXTBKCOLOR, _
                        0&, CLR_NONE)

   Call SendMessageAny(ListView1.hWnd, _
                       LVM_SETBKIMAGE, _
                       0&, BKIMG)

End Sub

Private Sub Command1_Click()

   Dim BKIMG As LVBKIMAGE

  'prepare the backimage structure data
   With BKIMG
      'use the file provided in pszImage member
      .uFlags = LVBKIF_SOURCE_URL

      'the imagefile to display
      .pszImage = (txtPixPathCentered)

      'h&v offsets for the pix in percent. A setting
      'of 0 aligns the image against the left or top;
      '100 against the right or bottom.  A setting of
      '50 each centers the image in the control.
      .xOffsetPercent = 50
      .yOffsetPercent = 50

   End With

  'set the text background to none (transparent) to
  'allow image to show through.  A RGB() value can
  'also be passed to colour the text background
  '(to the edge of the control)
   Call SendMessageLong(ListView1.hWnd, _
                        LVM_SETTEXTBKCOLOR, _
                        0&, CLR_NONE)

  'set the image to the listview
   Call SendMessageAny(ListView1.hWnd, _
                       LVM_SETBKIMAGE, _
                       0, BKIMG)

End Sub

Private Sub Command2_Click()

   BuildLVdata
   DisplayNoDataImage

End Sub

Private Sub Command3_Click()

   Dim BKIMG As LVBKIMAGE

  'prepare the backimage structure data
   With BKIMG
      'use the file provided in pszImage and tile
      .uFlags = LVBKIF_SOURCE_URL Or LVBKIF_STYLE_TILE

      'the imagefile to display
      .pszImage = (txtPixPathTile)

      'h&v offsets for the pix (only if tiled not specified)
      '.xOffsetPercent = 0
      '.yOffsetPercent = 0
   End With

  'set the text background to none (transparent) to
  'allow image to show through.  A RGB() value can
  'also be passed to colour the text background
  '(to the edge of the control)
   Call SendMessageLong(ListView1.hWnd, _
                        LVM_SETTEXTBKCOLOR, _
                        0&, CLR_NONE)

  'set the image to the listview
   Call SendMessageAny(ListView1.hWnd, _
                              LVM_SETBKIMAGE, _
                              0&, BKIMG)

End Sub

Private Sub Command5_Click()

      ListView1.ListItems.Clear
      DisplayNoDataImage

End Sub

Private Function InitComctl32(dwFlags As Long) As Boolean

  Dim icc As tagINITCOMMONCONTROLSEX
  On Error GoTo Err_OldVersion

  icc.dwSize = Len(icc)
  icc.dwICC = dwFlags

 'VB will generate error 453 "Specified
 'DLL function not found" here if the new
 'version isn't installed and it can't find
 'the function's name. We'll hopefully be
 'able to load the old version below. Note
 'that the methods here must have comctl32.dll
 'v4.72 or greater; this routine only
 'initializes the API; it does not
 'determine the version.
  InitComctl32 = InitCommonControlsEx(icc)
  Exit Function

Err_OldVersion:
  InitCommonControls

End Function

'--end block--'

Andy Knight a crit :

Quote:
> Hi there,

> is there anyway I can change the font/appearance of a listview item in a
> specific column.
> If it is, is it possible it can varied for each item.

> Any response gratefully appreciated.

> Andy

  ppp.vcf
< 1K Download


Fri, 15 Feb 2002 03:00:00 GMT  
 Colourizing Listviews
On Sun, 29 Aug 1999 21:37:53 -0700, "Andy Knight"

Quote:

>Hi there,

>is there anyway I can change the font/appearance of a listview item in a
>specific column.
>If it is, is it possible it can varied for each item.

>Any response gratefully appreciated.

Andy,

It is possible, using the Common controls custom draw feature. That
would reqire some subclassing though. For more information, see the
following page in the MSDN library:

http://msdn.microsoft.com/library/sdkdoc/shellcc/CommCtls/CustDraw/Cu...

Mattias



Fri, 15 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ListView & DBGrid Colourizing

2. ListView colourizing and refreshing

3. REQ: Colourized ListBox Challange

4. Drag an item of Listview to another position of the same ListView

5. Ownerdraw works with ListView 5.0 but not with ListView 6.0

6. Dragging from ListView to ListView or TreeView

7. Listview vs. Extended Listview

8. Listview: synchronize two listviews

9. Ownerdraw works with ListView 5.0 but not with ListView 6.0 (in this sample)

10. Listview: check if an item in current listview

11. ListView to ListView copying ???

12. ListView column width = ListView width ?

 

 
Powered by phpBB® Forum Software