热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在线急求listview控件设置隔行背景色源码

有listview控件显示若干行数据,默认背景色为白色,为观察方便,希望偶数行改成淡黄色显示。为求该功能,查遍网上若干解答,经实地测试均不能正确通过。绝大多数采用的方式是设一个picture控件,但可
有listview控件显示若干行数据,默认背景色为白色,为观察方便,希望偶数行改成淡黄色显示。为求该功能,查遍网上若干解答,经实地测试均不能正确通过。绝大多数采用的方式是设一个picture控件,但可能代码问题,不能正确显示有数据格次的背景色。现特求完整的示例代码,最好是通过一个过程的调用即可实现列表的刷新。敬望帮助,谢谢!

6 个解决方案

#1


怎么发给你?

#2


设置一行的背景颜色  
http://www.mvps.org/vbnet/index.html?code/comctl/lvledger.htm  
 
'设置多行的背景颜色  
http://www.mvps.org/vbnet/index.html?code/comctl/lvledgerrows.htm

#3


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
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 Form_Load()
   Command1.Caption = "Text Only"
   Command2.Caption = "Text && Checks"
   Command3.Caption = "Text && Icons"  
End Sub
Private Sub Command1_Click()  
   With ListView1
      .Visible = False
      .Checkboxes = False
      .FullRowSelect = True
       Set .SmallIcons = Nothing
      Call LoadData(sizeNone)
      Call SetListViewLedger(ListView1, _
                             vbLedgerYellow, _
                             vbLedgerGrey, _
                             sizeNone)      
      .Refresh
      .Visible = True            '/* Restore visibility
   End With
End Sub
Private Sub Command2_Click()
   With ListView1
      .Visible = False
      .Checkboxes = True
      .FullRowSelect = True
      Set .SmallIcons = Nothing   
      Call LoadData(sizeCheckBox)
      Call SetListViewLedger(ListView1, _
                             vbLedgerYellow, _
                             vbLedgerGrey, _
                             sizeCheckBox)            
      .Refresh
      .Visible = True
   End With   
End Sub
Private Sub Command3_Click()
   With ListView1
      .Visible = False
      .Checkboxes = False
      .FullRowSelect = True
      Set .SmallIcons = imagelist1   
      Call LoadData(sizeIcon)
      Call SetListViewLedger(ListView1, _
                             vbLedgerYellow, _
                             vbLedgerGrey, _
                             sizeIcon)      
      .Refresh
      .Visible = True
   End With   
   Command1.Enabled = False   
End Sub
Private Sub SetListViewLedger(lv As ListView, _
                              Bar1Color As LedgerColours, _
                              Bar2Color As LedgerColours, _
                              nSizingType As ImageSizingTypes)

   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  '/* variable holding Screen.TwipsPerPicture1elY   
   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 Picture1 font matched 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
               Else
                  diff = 18 - (iBarHeight \ twipsy)
                  iBarHeight = iBarHeight + (diff * twipsy) + (twipsy * 1)
               End If

#4



            Case sizeIcon:
              '/* 3. text with icons: add to textheight the
              '/*    difference between textheight and image
              '/*    height, all calculated initially in Pixels,
              '/*    then converted to twips. Handles 16x16 icons
               diff = imagelist1.ImageHeight - (iBarHeight \ twipsy)
               iBarHeight = iBarHeight + (diff * twipsy) + (twipsy * 1)
               
         End Select
      
        '/* since we need two-tone bars, the
        '/* picturebox needs to be twice as high
         .Height = iBarHeight * 2
         .Width = lBarWidth
         
        '/* paint the two bars of color and refresh
        '/* Note: The line method does not support
        '/* With/End With blocks
         Picture1.Line (0, 0)-(lBarWidth, iBarHeight), Bar1Color, BF
         Picture1.Line (0, iBarHeight)-(lBarWidth, iBarHeight * 2), Bar2Color, BF
      
         .AutoSize = True
         .Refresh
         
      End With  'Picture1
     
     '/* set the lv picture to the
     '/* Picture1 image
     
      lv.Refresh
      lv.Picture = Picture1.Image
      
   Else
    
      lv.Picture = Nothing
        
   End If  'lv.View = lvwReport

SetListViewColor_Exit:
On Local Error GoTo 0
Exit Sub
    
SetListViewColor_Error:

  '/* clear the listview's picture and exit
   With lv
      .Picture = nothing
      .Refresh
   End With
   
   Resume SetListViewColor_Exit
    
End Sub


Private Sub LoadData(nSizingType As ImageSizingTypes)

   Dim cnt As Long
   Dim itmX As ListItem
   
   With ListView1
      .ListItems.Clear
      .ColumnHeaders.Clear
      .ColumnHeaders.Add , , "Number"
      .ColumnHeaders.Add , , "Time"
      .ColumnHeaders.Add , , "User"
      .ColumnHeaders.Add , , "Tag "
      .View = lvwReport
      .Sorted = False
   End With
   
  '/* Create some fake data
   For cnt = 1 To 100
   
      Set itmX = Form1.ListView1.ListItems.Add(, , Format$(cnt, "###"))
      If nSizingType = sizeIcon Then itmX.SmallIcon = 1
      itmX.SubItems(1) = Format$(Time, "hh:mm:ss am/pm")
      itmX.SubItems(2) = "RGB-T"
      itmX.SubItems(3) = "SYS-1234"
         
   Next

  '/* Now that the control contains data, this
  '/* causes the columns to resize to fit the items
   Call lvAutosizeControl(Form1.ListView1)
   
   
End Sub


Private Sub lvAutosizeControl(lv As ListView)

   Dim col2adjust As Long

  '/* Size each column based on the maximum of
  '/* EITHER the columnheader text width, or,
  '/* if the items below it are wider, the
  '/* widest list item in the column
   For col2adjust = 0 To lv.ColumnHeaders.Count - 1
   
      Call SendMessage(lv.hwnd, _
                       LVM_SETCOLUMNWIDTH, _
                       col2adjust, _
                       ByVal LVSCW_AUTOSIZE_USEHEADER)

   Next
   
   
End Sub

#5


对不起,我的邮箱是webmaster@cs1688.com ,发送时请注明您的社区名,以便收到后为您加分,谢谢

#6


求朋友们提供有正确显示的源码程序,烦请帮助发到我的邮箱webmaster@cs1688.com ,深表谢意

推荐阅读
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 本文介绍了一个从入门到高手的VB.NET源代码,通过学习这些源代码,可以在21天内成为VB.NET高手。文章提供了下载地址,并提醒读者加入作者的QQ群和收藏作者的博客。 ... [详细]
  • loader资源模块加载器webpack资源模块加载webpack内部(内部loader)默认只会处理javascript文件,也就是说它会把打包过程中所有遇到的 ... [详细]
  • 注:根据Qt小神童的视频教程改编概论:利用最新的Qt5.1.1在windows下开发的一个小的时钟程序,有指针与表盘。1.Qtforwindows开发环境最新的Qt已经集 ... [详细]
  • 关于python调试大法的信息
    本文目录一览:1、pdbpython调试怎么用 ... [详细]
  • Material Design Lite ,简洁惊艳的前端工具箱。
    2019独角兽企业重金招聘Python工程师标准MaterialDesignLite简介本文主要介绍MaterialDesign设计语言的HTMLCSSJS部分实现。对应每一 ... [详细]
  • Bootstrap实战注册和登录
    注册和登录在社交和商业网站中是 ... [详细]
  • 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件
    本文旨在全面介绍Windows内存管理机制及C++内存分配实例中的内存映射文件。通过对内存映射文件的使用场合和与虚拟内存的区别进行解析,帮助读者更好地理解操作系统的内存管理机制。同时,本文还提供了相关章节的链接,方便读者深入学习Windows内存管理及C++内存分配实例的其他内容。 ... [详细]
  • 本文介绍了禅道作为一款国产开源免费的测试管理工具的特点和功能,并提供了禅道的搭建和调试方法。禅道是一款B/S结构的项目管理工具,可以实现组织管理、后台管理、产品管理、项目管理和测试管理等功能。同时,本文还介绍了其他软件测试相关工具,如功能自动化工具和性能自动化工具,以及白盒测试工具的使用。通过本文的阅读,读者可以了解禅道的基本使用方法和优势,从而更好地进行测试管理工作。 ... [详细]
  • asp中如何嵌入python的简单介绍
    本文目录一览:1、如何在IIS中执行Python脚本 ... [详细]
  • vb.net不用多线程如何同时运行两个过程?不用多线程?即使用多线程,也不会是“同时”执行,题主只要略懂一些计算机编译原理就能明白了。不用多线程更不可能让两个过程同步执行了。不过可 ... [详细]
  • Thisissuewasoriginallyopenedbyashashicorp/terraform#5664.Itwasmigratedhe ... [详细]
  • 在最近的一系列文章,对midipadAPP,有一个关于一个radialgradiant渲染每个padview利用的探讨,对审美的原因&#x ... [详细]
author-avatar
Android代码
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有