listview的隔行显示不同颜色

该博客介绍了如何在VB中实现ListView控件的隔行显示不同颜色,包括使用Enum定义颜色和图片大小类型,以及调整ListView列宽的方法。通过设置ListView的背景图片,实现了条纹效果,分别展示了只有文本、文本加复选框和文本加图标的三种情况。
摘要由CSDN通过智能技术生成

listview的隔行显示不同颜色
Option Explicit

Private Enum ImageSizingTypes
   [sizeNone] = 0
   [sizeCheckBox]
   [sizeIcon]
End Enum

Private Enum LedgerColours
  vbledgerWhite = &HF9FEFF
  vbLedgerGreen = &HD0FFCC
  vbLedgerYellow = &HE1FAFF
  vbLedgerRed = &HE1E1FF
  vbLedgerGrey = &HE0E0E0
  vbLedgerBeige = &HD9F2F7
  vbLedgerSoftWhite = &HF7F7F7
  vbledgerPureWhite = &HFFFFFF
End Enum

'/* Below used for listview column auto-resizing
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2

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 SetListViewLedgerRows(lv As ListView, _
                                  Bar1Color As LedgerColours, _
                                  Bar2Color As LedgerColours, _
                                  nSizingType As ImageSizingTypes, _
                                  Optional nRowsPerBar As Long = 1)
 
   Dim iBarHeight  As Long  '/* height of 1 line in the listview
   Dim lBarWidth   As Long  '/* width of listview
   Dim diff        As Long  '/* used in calculations of row height
   Dim twipsy      As Long  '/* var holding Screen.TwipsPerPixelY
  
   iBarHeight = 0
   lBarWidth = 0
   diff = 0
  
   On Local Error GoTo SetListViewColor_Error
  
   twipsy = Screen.TwipsPerPixelY
  
   If lv.View = lvwReport Then
  
     '/* set up the listview properties
      With lv
        .Picture = Nothing  '/* clear picture
        .Refresh
        .Visible = 1
        .PictureAlignment = lvwTile
        lBarWidth = .Width
      End With  ' lv
       
     '/* set up the picture box properties
      With Picture1
         .AutoRedraw = False       '/* clear/reset picture
         .Picture = Nothing
         .BackColor = vbWhite
         .Height = 1
         .AutoRedraw = True        '/* assure image draws
         .BorderStyle = vbBSNone   '/* other attributes
         .ScaleMode = vbTwips
         .Top = Form1.Top - 10000  '/* move it way off screen
         .Width = Screen.Width
         .Visible = False
         .Font = lv.Font           '/* assure font matches listview font
        
        '/* match picture box font properties
        '/* with those of listview
         With .Font
            .Bold = lv.Font.Bold
            .Charset = lv.Font.Charset
            .Italic = lv.Font.Italic
            .Name = lv.Font.Name
            .Strikethrough = lv.Font.Strikethrough
            .Underline = lv.Font.Underline
            .Weight = lv.Font.Weight
            .Size = lv.Font.Size
         End With  'Picture1.Font
        
        '/* here we calculate the height of each
        '/* bar in the listview. Several things
        '/*  can affect this height - the use
        '/* of item icons, the size of those icons,
        '/* the use of checkboxes and so on through
        '/* all the permutations.
        '/*
        '/* Shown here is code sufficient to calculate
        '/* this height based on three combinations of
        '/*  data, state icons, and imagelist icons:
        '/*
        '/* 1. text only
        '/* 2. text with checkboxes
        '/* 3. text with icons
       
       '/* used by all sizing routines
         iBarHeight = .TextHeight("W")
 
         Select Case nSizingType
            Case sizeNone:
              '/* 1. text only
               iBarHeight = iBarHeight + twipsy
              
            Case sizeCheckBox:
              '/* 2. text with checkboxes: add to TextHeight the
              '/*    difference between 18 pixels and iBarHeight
              '/*    all calculated initially in pixels,
              '/*    then converted to twips
               If (iBarHeight / twipsy) > 18 Then
                  iBarHeight = iBarHeight + twipsy

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值