请问如何修改listview列表头?

soec 2003-03-07 05:29:38
请问如何修改listview列表头?
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户 昵称 2003-03-07
  • 打赏
  • 举报
回复
LVHeaderSortIcons.cls

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "cLVHeaderSortIcons"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'
' Copyright ?1997-1999 Brad Martinez, http://www.mvps.org
'
' We're using IE3 definitions
#Const WIN32_IE = &H300

Private m_ListView As ListView
Attribute m_ListView.VB_VarHelpID = -1
Private m_himl As Long

Public Enum SortOrderConstants
soAscending = 0
soDescending = 1
End Enum

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

' ============================================================
' listview definitions

' messages
Private Const LVM_FIRST = &H1000
#If (WIN32_IE >= &H300) Then
Private Const LVM_GETHEADER = (LVM_FIRST + 31)
#End If

' ============================================================
' header definitions

' messages
Private Const HDM_FIRST = &H1200
Private Const HDM_SETITEM = (HDM_FIRST + 4)
#If (WIN32_IE >= &H300) Then
Private Const HDM_SETIMAGELIST = (HDM_FIRST + 8)
#End If

Private Type HDITEM ' was HD_ITEM
mask As Long
cxy As Long
pszText As String ' if retrieving text, must be pre-allocated
hbm As Long
cchTextMax As Long
fmt As Long
lParam As Long
#If (WIN32_IE >= &H300) Then
iImage As Long ' index of bitmap in ImageList
iOrder As Long ' where to draw this item
#End If
End Type

' HDITEM mask
Private Const HDI_FORMAT = &H4
#If (WIN32_IE >= &H300) Then
Private Const HDI_IMAGE = &H20
#End If

' HDITEM fmt
Private Const HDF_LEFT = 0
Private Const HDF_RIGHT = 1
#If (WIN32_IE >= &H300) Then
Private Const HDF_IMAGE = &H800
Private Const HDF_BITMAP_ON_RIGHT = &H1000
#End If
Private Const HDF_STRING = &H4000

' ============================================================
' imagelist definitions

' ImageList_Create flags
Private Const ILC_MASK = &H1
Private Const ILC_COLOR8 = &H8

Private Declare Function ImageList_Create Lib "comctl32.dll" (ByVal cx As Long, ByVal cy As Long, ByVal flags As Long, ByVal cInitial As Long, ByVal cGrow As Long) As Long
Private Declare Function ImageList_Destroy Lib "comctl32.dll" (ByVal himl As Long) As Boolean
Private Declare Function ImageList_ReplaceIcon Lib "comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hIcon As Long) As Long
'

Private Sub Class_Initialize()
m_himl = ImageList_Create(16, 16, ILC_MASK Or ILC_COLOR8, 2, 0)
If m_himl Then
' Load the icons into the image so that their zero-based
' indicescorrespond to the SortOrderConstants values.
Call ImageList_AddIcon(m_himl, LoadPicture("sortascending.ico"))
Call ImageList_AddIcon(m_himl, LoadPicture("sortdescending.ico"))
End If
End Sub

Private Sub Class_Terminate()
If m_himl Then Call ImageList_Destroy(m_himl)
End Sub

Public Property Get ListView() As ListView
Set ListView = m_ListView
End Property

Public Property Set ListView(lv As Object)
Set m_ListView = lv
End Property

' Sets and removes header sort order icons

Public Function SetHeaderIcons(iActiveColumn As Long, iSortOrder As SortOrderConstants) As Boolean
Static hwndHdr As Long
Dim i As Long
Dim fShow As Boolean
Dim fAlignRight As Boolean
Dim hdi As HDITEM

If (m_himl = 0) Or (m_ListView Is Nothing) Then Exit Function
If (m_ListView.View <> lvwReport) Then Exit Function

' The ListView's header is created *after* the first
' ColumnHeader is added.
If (hwndHdr = 0) Then
hwndHdr = ListView_GetHeader(m_ListView.hWnd)
Call Header_SetImageList(hwndHdr, m_himl)
End If

If (hwndHdr = 0) Then Exit Function

With m_ListView.ColumnHeaders
For i = 0 To .Count - 1
hdi.mask = HDI_FORMAT Or HDI_IMAGE

fAlignRight = .item(i + 1).Alignment = lvwColumnRight

' Since we're setting the header's format, we have to
' specify the string flag
hdi.fmt = HDF_STRING Or (fAlignRight And HDF_RIGHT) ' HDF_LEFT = 0

' If the active column, add the sort icon with the appropriate
' alignment (the icon is removed if HDF_IMAGE is not set).
If (i = iActiveColumn) Then
hdi.fmt = hdi.fmt Or HDF_IMAGE Or ((fAlignRight = False) And HDF_BITMAP_ON_RIGHT)
End If

' If not the soAscending icon index (0), then set to
' the soDescending (1) icon index.
hdi.iImage = Abs(CBool(iSortOrder))

Call Header_SetItem(hwndHdr, i, hdi)
Next
End With

SetHeaderIcons = True

End Function

' ============================================================
' listview, header, imagelist macros

#If (WIN32_IE >= &H300) Then

Private Function ListView_GetHeader(hWnd As Long) As Long
ListView_GetHeader = SendMessage(hWnd, LVM_GETHEADER, 0, 0)
End Function
'
#End If
'

Private Function Header_SetItem(hwndHD As Long, i As Long, phdi As HDITEM) As Boolean
Header_SetItem = SendMessage(hwndHD, HDM_SETITEM, i, phdi)
End Function

#If (WIN32_IE >= &H300) Then

Private Function Header_SetImageList(hWnd As Long, himl As Long) As Long
Header_SetImageList = SendMessage(hWnd, HDM_SETIMAGELIST, 0, ByVal himl)
End Function
'
#End If
'

Private Function ImageList_AddIcon(himl As Long, hIcon As Long) As Long
ImageList_AddIcon = ImageList_ReplaceIcon(himl, -1, hIcon)
End Function

=============================
还有两个图标,自己添加
sortascending.ico
sortdescending.ico
用户 昵称 2003-03-07
  • 打赏
  • 举报
回复
给你一个示例

LVHeaderSortIcons.VBP
Type=Exe
Form=Form1.frm
Class=cLVHeaderSortIcons; LVHeaderSortIcons.cls
Object={6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0; Comctl32.ocx
IconForm="Form1"
Startup="Form1"
HelpFile=""
Command32=""
Name="LVHeaderSortIcons"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="Common Controls Replacement Project (CCRP)"
VersionLegalCopyright="Copyright ?1999 Brad Martinez, CCRP"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
ThreadPerObject=0
MaxNumberOfThreads=1
=============================================
Form1.frm
VERSION 5.00
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "Comctl32.ocx"
Begin VB.Form Form1
Caption = "VB ListView header sort icon demo"
ClientHeight = 5550
ClientLeft = 1485
ClientTop = 1560
ClientWidth = 7155
ClipControls = 0 'False
LinkTopic = "Form1"
ScaleHeight = 5550
ScaleWidth = 7155
Begin ComctlLib.ListView ListView1
Height = 3825
Left = 300
TabIndex = 0
Top = 300
Width = 5415
_ExtentX = 9551
_ExtentY = 6747
LabelWrap = -1 'True
HideSelection = -1 'True
_Version = 327680
ForeColor = -2147483640
BackColor = -2147483643
BorderStyle = 1
Appearance = 1
NumItems = 0
End
Begin ComctlLib.ImageList ImageList2
Left = 3300
Top = 4560
_ExtentX = 1005
_ExtentY = 1005
BackColor = -2147483643
MaskColor = 12632256
_Version = 327680
End
Begin ComctlLib.ImageList ImageList1
Left = 2400
Top = 4590
_ExtentX = 1005
_ExtentY = 1005
BackColor = -2147483643
MaskColor = 12632256
_Version = 327680
End
Begin VB.Menu mnuView
Caption = "&View"
Begin VB.Menu mnuViewLargeIcons
Caption = "Lar&ge Icons"
End
Begin VB.Menu mnuViewSmallIcons
Caption = "S&mall Icons"
End
Begin VB.Menu mnuViewList
Caption = "&List"
End
Begin VB.Menu mnuViewReport
Caption = "&Detatils"
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'
' Copyright ?1997-1999 Brad Martinez, http://www.mvps.org
'
' Demonstrates how to do custom sort icons in the VB ListView's header.

' Much of this demo's inspiration was drawn from Randy Birch's
' "How to Add Images to a ListView Header" code at
' http://www.mvps.org/vbnet/code/comctl/lvheaderimage.htm

Private m_cHdrIcons As New cLVHeaderSortIcons
'

Private Sub Form_Load()
Dim i As Long
Dim item As ListItem

' Initialize the ImageLists
With ImageList1
.ImageHeight = 32
.ImageWidth = 32
.ListImages.Add Picture:=Icon
End With

With ImageList2
.ImageHeight = 16
.ImageWidth = 16
.ListImages.Add Picture:=Icon
End With

' Initialize the ListView
With ListView1
.LabelEdit = lvwManual
.Icons = ImageList1
.SmallIcons = ImageList2

.ColumnHeaders.Add Text:="column1"
.ColumnHeaders.Add Text:="column2", Alignment:=lvwColumnRight
.ColumnHeaders.Add Text:="column3"
.ColumnHeaders.Add Text:="column4", Alignment:=lvwColumnRight

Randomize
For i = 1 To 20
Set item = .ListItems.Add(, , "item" & Format$(i, "00"), 1, 1)
item.SubItems(1) = Format$((20 - i) * 10, "000")
item.SubItems(2) = String$(3, Chr$(Asc("Z") - i))
item.SubItems(3) = Format$(Rnd * 1000, "000")
Next
End With

mnuViewReport_Click

Set m_cHdrIcons.ListView = ListView1
Call m_cHdrIcons.SetHeaderIcons(0, soAscending)

End Sub

Private Sub Form_Resize()
ListView1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

' Toggles the sort order, and sorts the ListView's items or subitems under
' the respectively clicked ColumnHeader.

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
Dim i As Integer

' Toggle the clicked column's sort order only if the active colum is clicked
' (iow, don't reverse the sort order when different columns are clicked).
If (ListView1.SortKey = ColumnHeader.Index - 1) Then
ColumnHeader.Tag = Not Val(ColumnHeader.Tag)
End If

' Set sort order to that of the respective ListSortOrderConstants value
ListView1.SortOrder = Abs(Val(ColumnHeader.Tag))

' Get the zero-based index of the clicked column.
' (ColumnHeader.Index is one-based).
ListView1.SortKey = ColumnHeader.Index - 1

' Sort the ListView
ListView1.Sorted = True

' Set the header icons
Call m_cHdrIcons.SetHeaderIcons(ListView1.SortKey, ListView1.SortOrder)

End Sub

' ==========================================
' View menu

Private Sub mnuViewLargeIcons_Click()
Call SwitchView(lvwIcon)
End Sub

Private Sub mnuViewSmallIcons_Click()
Call SwitchView(lvwSmallIcon)
End Sub

Private Sub mnuViewList_Click()
Call SwitchView(lvwList)
End Sub

Private Sub mnuViewReport_Click()
Call SwitchView(lvwReport)
End Sub

Private Sub SwitchView(dwNewView As ListViewConstants)

ListView1.View = dwNewView
' A bug: http://support.microsoft.com/support/kb/articles/q143/4/06.asp
ListView1.Arrange = lvwAutoTop

mnuViewLargeIcons.Checked = (dwNewView = lvwIcon)
mnuViewSmallIcons.Checked = (dwNewView = lvwSmallIcon)
mnuViewList.Checked = (dwNewView = lvwList)
mnuViewReport.Checked = (dwNewView = lvwReport)

End Sub
syl5005 2003-03-07
  • 打赏
  • 举报
回复
你可以直接用这个Dim item As ListItem

Private Sub Command1_Click()
ListView1.ColumnHeaders(1).Text = "11"
End Sub

Private Sub Form_Load()
Dim i As Integer
Dim a
For i = 1 To 20
Set item = ListView1.ListItems.Add(i, Chr(96 + i), Chr(96 + i))
item.SubItems(1) = Chr(96 + i)
Next
End Sub

peilianhai 2003-03-07
  • 打赏
  • 举报
回复
这个好说

listview.header=

lsitview.columenheader.
、之类
多试一下就可以了

你多实施
等我有时间
告诉你详细的
soec 2003-03-07
  • 打赏
  • 举报
回复
help!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧