VB6中如何用代码直接创建ListView控件?
VB6中如何用代码直接创建ListView控件?网上搜索了好久,没有添加此控件的方法,尝试很多方式还是不成功。
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