标题:VB6中如何用代码直接创建ListView控件?
只看楼主
foreach
Rank: 2
等 级:论坛游民
帖 子:30
专家分:57
注 册:2020-4-15
结帖率:14.29%
已结贴  问题点数:20 回复次数:6 
VB6中如何用代码直接创建ListView控件?
VB6中如何用代码直接创建ListView控件?
网上搜索了好久,没有添加此控件的方法,尝试很多方式还是不成功。
搜索更多相关主题的帖子: VB6 控件 ListView 创建 代码 
2020-08-24 21:11
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4912
专家分:29900
注 册:2008-10-15
得分:20 
listview 是外置控件,直接创建是不太可能。也许工程中引用了这个外部控件,然后再可能使用代码能创建,但问题是你这个控件的事件响应打算全部自己写?

但你可以先添加一个 listview 控件到窗体上,然后设置不可见,需要时再显示它。也可以创建控件数组。



授人于鱼,不如授人于渔
早已停用QQ了
2020-08-24 21:32
foreach
Rank: 2
等 级:论坛游民
帖 子:30
专家分:57
注 册:2020-4-15
得分:0 
回复 2楼 风吹过b
尝试在VBA里面可以实现,在VB6没找到方法。事件的话可以类模块实现吧,虽然麻烦些。
想用代码操作,主要是考虑兼容问题,因为这台电脑手动添加的,到了另外一台电脑可能使用不正常,好烦躁
2020-08-24 23:11
William1949
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:109
专家分:0
注 册:2009-3-17
得分:0 
程序代码:
Option Explicit
Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const LVS_REPORT = &H1
Private Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByRef lpParam As Any) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
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 Type LVCOLUMN
    Mask        As Long
    Fmt         As Long
    cX          As Long
    pszText     As String
    cchTextMax  As Long
    iSubItem    As Long
    iImage      As Long
    iOrder      As Long
End Type
Private Const LVM_INSERTCOLUMNA = &H101B

Private Const LVCF_WIDTH = &H2
Private Const LVCF_TEXT = &H4

Private Type LVITEM
    Mask        As Long
    iItem       As Long
    iSubItem    As Long
    State       As Long
    StateMask   As Long
    pszText     As String
    cchTextMax  As Long
    iImage      As Long
    lParam      As Long
    iIndent     As Long
End Type
Private Const LVIF_TEXT = &H1
Private Const LVM_INSERTITEMA = &H1007
Private Const LVM_SETITEMA = &H1006

Private LVhWnd  As Long

Private Sub Form_Load()
    Dim dwStyle     As Long
    dwStyle = WS_CHILD Or WS_VISIBLE Or LVS_REPORT
    LVhWnd = CreateWindowEx(0, "SysListView32", vbNullString, dwStyle, 0, 0, 500, 600, Me.hWnd, 0, App.hInstance, ByVal 0)

    Call ColumnHeadAdd(0, "第 1 列")
    Call ColumnHeadAdd(1, "第 2 列")
    
    Call RowAdd(0, "行 1")
    Call RowSubAdd(0, 1, "子列 2")
    Call RowAdd(1, "行 2")
    Call RowSubAdd(1, 1, "子列 2")
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call DestroyWindow(LVhWnd)
End Sub

Private Sub ColumnHeadAdd(ByVal Index As Long, ByVal Text As String)
    Dim ColHead As LVCOLUMN
    With ColHead
        .Mask = LVCF_WIDTH Or LVCF_TEXT
        .cX = 100
        .pszText = Text
    End With
    Call SendMessage(LVhWnd, LVM_INSERTCOLUMNA, Index, ColHead)
End Sub

Private Sub RowAdd(ByVal Index As Long, ByVal Text As String)
    Dim iItem       As LVITEM
    With iItem
        .Mask = LVIF_TEXT
        .iItem = Index
        .pszText = Text
    End With
    Call SendMessage(LVhWnd, LVM_INSERTITEMA, 0, iItem)
End Sub

Public Sub RowSubAdd(ByVal Row As Long, ByVal Index As Long, ByVal Text As String)
    Dim iItem     As LVITEM
    With iItem
        .Mask = LVIF_TEXT
        .iItem = Row
        .iSubItem = Index
        .pszText = Text
    End With
    Call SendMessage(LVhWnd, LVM_SETITEMA, 0, iItem)
End Sub






题外话:
ListView控件的表头高度怎么改变?这问题困扰了很久,搞不定。
有谁知道?
2020-09-03 13:59
foreach
Rank: 2
等 级:论坛游民
帖 子:30
专家分:57
注 册:2020-4-15
得分:0 
回复 4楼 William1949
谢谢,是可以自动添加。
还有个麻烦就是如何设置行边框,表格样式?

你说的标题行高,百度了一下都是用别的控件去实现,不用listview
2020-09-09 10:14
cwa9958
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:247
专家分:1228
注 册:2006-6-25
得分:0 
回复 4楼 William1949
好像不能单独改高度的。把字体改大,高度也高了。整体都一样。
2020-09-09 12:45
HVB6
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:15
帖 子:320
专家分:561
注 册:2013-10-30
得分:0 
回复 4楼 William1949
您的代码,如何才能自动全变为英文呢?谢谢。
2020-09-13 16:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-502949-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055547 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved