怎样在Listview标题右边添加图标

freshclue 2011-06-18 03:13:13
使用语句:listview1.ColumnHeaders(Index).Icon = 1 可以在标题上添加图标,但是图标添加到原标题的左边了,怎么能使图标添加到右边?
...全文
216 点赞 收藏 16
写回复
16 条回复
VBAdvisor 2011年06月25日
[Quote=引用 15 楼 freshclue 的回复:]

已经发过去了
[/Quote]
没有收到。
你先将上面的编码拷到bas中,然后再在Form1中引用Microsoft Window Common Controls SP6。在工具栏选中Listview和ListImage,在ListImage中添加Icon:


Private Sub Form_Load()

ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "A Column", , , 2

ListView1.ColumnHeaders.Item(ListView1.ColumnHeaders.Count).Icon = 1

Dim Counter as long
For Counter = 1 To 500
ListView1.ListItems.Add , , Counter
Next Counter
End Sub

Private CmdChangeIcon__Click()
ShowHeaderIcon 1, 2, True, ListView1, True
End Sub

回复 点赞
freshclue 2011年06月25日
已经发过去了
回复 点赞
VBAdvisor 2011年06月23日
[Quote=引用 13 楼 freshclue 的回复:]

不行的,小箭头图标还是显示在标题的左边,我想要的效果是小箭头图标显示在标题的右边
[/Quote]
你把总个工程传给我。 vbadvisor at gmail.com
回复 点赞
freshclue 2011年06月23日
不行的,小箭头图标还是显示在标题的左边,我想要的效果是小箭头图标显示在标题的右边
回复 点赞
VBAdvisor 2011年06月22日
在Win7上做的测试。(Win7,VB6/SP6)
Microsoft Window Common Controls SP6
回复 点赞
VBAdvisor 2011年06月22日

Private Sub Form_Load()

ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "A Column", , , 2

ListView1.ColumnHeaders.Item(ListView1.ColumnHeaders.Count).Icon = 1

Dim Counter as long
For Counter = 1 To 500
ListView1.ListItems.Add , , Counter
Next Counter
End Sub

Private CmdChangeIcon__Click()
ShowHeaderIcon 1, 2, True, ListView1, True
End Sub

回复 点赞
freshclue 2011年06月22日
试了一下9楼的这段代码,不起作用啊
回复 点赞
pfanlkjtroi 2011年06月21日
实际上就是在ColumnHeader上放了一个位图。详情请仔细看MSDN。
回复 点赞
VBAdvisor 2011年06月21日

ShowHeaderIcon 1, 2, True, ListView1, True



Private Const HDF_LEFT As Long = 0
Private Const HDF_RIGHT As Long = 1
Private Const HDF_CENTER As Long = 2
Private Const HDF_CENTER As Long = 2
Private Const HDF_JUSTIFYMASK As Long = &H3
Private Const HDF_RTLREADING As Long = 4
Private Const HDF_IMAGE As Long = &H800
Private Const HDF_OWNERDRAW As Long = &H8000&
Private Const HDF_STRING As Long = &H4000
Private Const HDF_BITMAP As Long = &H2000
'Private Const HDF_BITMAP_ON_RIGHT As Long = &H1000
Private Const HDF_CHECKBOX As Long = &H40
Private Const HDF_CHECKED As Long = &H80
Private Const HDF_FIXEDWIDTH As Long = &H100 ' Can't resize the column; same as LVCFMT_FIXED_WIDTH
Private Const HDF_SPLITBUTTON As Long = &H1000000 'Column is a split button; same as LVCFMT_SPLITBUTTON

Private Const HDF_SORTDOWN As Long = &H400
Private Const HDF_SORTUP As Long = &H200
Private Type HDITEMW
Mask As Long
cxy As Long
pszText As Long
hbm As Long
cchTextMax As Long
fmt As Long
lParam As Long
iImage As Long
iOrder As Long
type As Long ':(?Missing Scope
pvFilter As Long
State As Long 'WIN32_WINNT >= 0x0600
End Type
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_GETHEADER As Long = (LVM_FIRST + 31)

Private Const HDM_FIRST As Long = &H1200
Private Const HDM_GETITEM As Long = (HDM_FIRST + 3)
Private Const HDM_SETITEM As Long = (HDM_FIRST + 4)

Private Const HDF_BITMAP_ON_RIGHT As Long = &H1000

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 ':(?Missing Scope
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

Public Sub ShowHeaderIcon(ByVal colNo As Long, ByVal imgIconNo As Long, ByVal bShowImage As Boolean, _
ByVal lvwControl As ListView, Optional alignImageRight As Boolean = True)
Dim lHeader As Long
Dim HD As HDITEMW
Dim imageAlignment As Long

' get a handle to the listview header component
lHeader = SendMessage(lvwControl.hWnd, LVM_GETHEADER, 0, ByVal 0)

imageAlignment = IIf(alignImageRight, HDF_BITMAP_ON_RIGHT, HDF_BITMAP)

' set up the structure entries
With HD
.Mask = HDI_IMAGE Or HDI_FORMAT

If bShowImage Then ' show
.fmt = HDF_STRING Or HDF_IMAGE Or imageAlignment
.iImage = imgIconNo
Else
.fmt = HDF_STRING ' just string
End If
.fmt = .fmt Or GetAlignment(lvwControl.ColumnHeaders(colNo))
End With

' modify the header
Call SendMessage(lHeader, HDM_SETITEM, colNo - 1, HD)

End Sub
Private Function GetAlignment(col As ColumnHeader)
' Get the columns current alignment
With col
Select Case .Alignment
Case lvwColumnRight
GetAlignment = HDF_RIGHT
Case lvwColumnCenter
GetAlignment = HDF_CENTER
Case Else
GetAlignment = HDF_LEFT
End Select
End With
End Function
回复 点赞
难道是 OwnerDraw 的?

请出示源代码。
回复 点赞
freshclue 2011年06月20日
各位帮看看,ListView标题上的图标怎么能在标题的右边显示
窗体上添加1个ListView和1个Imagelist控件
窗体代码:
Private Sub Form_Load()
ListView1.View = lvwReport
ListView1.FullRowSelect = True
ListView1.ColumnHeaderIcons = ImageList1 'ImageList里面添加两个图标

ListView1.ColumnHeaders.Add Text:="column1"
ListView1.ColumnHeaders.Add Text:="column2", Alignment:=lvwColumnRight
End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

Dim index As Integer
Dim i As Integer

index = ColumnHeader.index

With ListView1
For i = 1 To .ColumnHeaders.Count
.ColumnHeaders(i).Icon = 0
If .ColumnHeaders(i).Tag = "" Then
.ColumnHeaders(i).Tag = "1"
End If
Next i

If .ColumnHeaders(index).Tag = "1" Then
.SortOrder = lvwAscending
.ColumnHeaders(index).Icon = 1
.ColumnHeaders(index).Tag = "0"
ElseIf .ColumnHeaders(index).Tag = "0" Then
.SortOrder = lvwDescending
.ColumnHeaders(index).Tag = "1"
.ColumnHeaders(index).Icon = 2
End If

.SortKey = index - 1
.Refresh
End With

End Sub
回复 点赞
VBAdvisor 2011年06月20日
Dim uHDI As HDITEMW

uHDI.fmt = HDF_STRING Or Align * -(Column <> 0) Or HDF_BITMAP_ON_RIGHT

'...
'Call SendMessage(m_hHeader, HDM_SETITEMW, Column, uHDI)
回复 点赞
cqq_chen 2011年06月19日
你不是已经有源码了吗?好好看,如果不理解你就全用,不就行了...
回复 点赞
freshclue 2011年06月18日
回复 点赞
freshclue 2011年06月18日
就是这样效果的,http://hi.csdn.net/attachment/201106/18/662859_13083905471N1X.jpg
在网上查了一下,说是需要绘制一个图标才行,有VC的代码,用VB怎么实现啊
回复 点赞
jackey 2011年06月18日
都是在左边吧 我没见过在文字右边的
回复 点赞
发动态
发帖子
VB基础类
创建于2007-09-28

2748

社区成员

19.7w+

社区内容

VB 基础类
社区公告
暂无公告