ListViewをコードで作成          <TOP>


ListViewを作成します。

CreateWindowEx ウインドウ(コントロール)を作成

DestroyWindow CreateWindowExの解放

SendMessage ウィンドウにメッセージを送信

 

 

 

'================================================================
'= ListViewをコードで作成
'=    (CreateWindowEx6.bas)
'================================================================
#include "Windows.bi"

Type LVITEM
    mask       As Long
    iItem      As Long
    iSubItem   As Long
    state      As Long
    stateMask  As Long
    pszText    As Long
    cchTextMax As Long
    iImage     As Long
    lParam     As Long
    iIndent    As Long
End Type

Type LVCOLUMN
    mask       As Long
    fmt        As Long
    cx         As Long
    pszText    As Long
    cchTextMax As Long
    iSubitem   As Long
End Type

#define WS_CHILD &H40000000             '親ウィンドウを持つコントロール(子ウィンドウ)を作成する
#define WS_EX_CLIENTEDGE &H200          '縁が沈んで見える境界線を持つウィンドウを指定
#define WS_VISIBLE &H10000000           '可視状態のウィンドウを作成する
#define LVS_LIST &H3                    'リストビューを指定
#define LVS_REPORT &H1                  'レポートビューを指定
#define LVS_AUTOARRANGE &H100           'アイコンビューおよび小さなアイコンビューで、アイコンが自動的に整列
#define LVM_FIRST &H1000                '
#define LVM_INSERTCOLUMN &H101B         '新しいカラム(列)を挿入
#define LVM_SETIMAGELIST &H1003         'イメージリストの割り当て
#define LVM_GETIMAGELIST &H1002
#define LVSIL_SMALL 1                   '小さいアイコン
#define LVSIL_NORMAL 0                  '大きいアイコン
#define LVCF_TEXT &H4                   'LVCOLUMNメンバ(pszText)
#define LVCF_WIDTH &H2                  'LVCOLUMNメンバ(cx)
#define LVIF_TEXT &H1
#define LVIF_IMAGE &H2
#define LVIF_PARAM &H4
#define LVIF_STATE &H8
#define LVM_INSERTITEMA &H1007          '(LVM_FIRST + 7)
#define LVM_GETITEMCOUNT &H1004         '(LVM_FIRST + 4)
#define LVM_SETITEMA &H1006             '(LVM_FIRST + 6)
#define LVM_SETITEMTEXTA (&H1000 Or 46) '(LVM_FIRST + 46)

' ウインドウ(コントロール)を作成
Declare Function Api_CreateWindowEx& Lib "user32" Alias "CreateWindowExA" (ByVal ExStyle&, ByVal ClassName$, ByVal WinName$, ByVal Style&, ByVal x&, ByVal y&, ByVal nWidth&, ByVal nHeight&, ByVal Parent&, ByVal Menu&, ByVal Instance&, ByVal Param&)

' CreateWindowExの解放
Declare Function Api_DestroyWindow& Lib "user32" Alias "DestroyWindow" (ByVal hWnd&)

' ウィンドウにメッセージを送信
Declare Function Api_SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hWnd&, ByVal wMsg&, ByVal wParam&, lParam As Any)

Var Shared Button1 As Object

Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

Var Shared hWndLV As Long
Var Shared Style As Long

'================================================================
'=
'================================================================
Declare Function AddItem(hwnd As Long, Index As Long, Buffer As String)
Function AddItem(hwnd As Long, Index As Long, Buffer As String)
    Var lvi As LVITEM
    Var test As Long
    
    lvi.mask = LVIF_TEXT Or LVIF_IMAGE Or LVIF_PARAM Or LVIF_STATE
    lvi.state = 0
    lvi.stateMask = 0
    lvi.pszText = StrAdr(Buffer & Chr$(0))
    lvi.iImage = 0
    lvi.iItem = Api_SendMessage(hWndLV, LVM_GETITEMCOUNT, 0, 0)
    
    AddItem = Api_SendMessage(hWndLV, LVM_INSERTITEMA, Index, lvi)
End Function

'================================================================
'=
'================================================================
Declare Function AddSubItem(hwnd As Long, Index As Long, SubIndex As Long, Buffer As String)
Function AddSubItem(hwnd As Long, Index As Long, SubIndex As Long, Buffer As String)
    Var lvi As LVITEM
    
    lvi.mask = LVIF_TEXT Or LVIF_IMAGE Or LVIF_PARAM Or LVIF_STATE
    lvi.state = 0
    lvi.stateMask = 0
    lvi.pszText = StrAdr(Buffer & Chr$(0))
    lvi.iItem = Api_SendMessage(hWndLV, LVM_GETITEMCOUNT, 0, 0)
    lvi.iSubItem = SubIndex
    
    AddSubItem = Api_SendMessage(hwnd, LVM_SETITEMTEXTA, Index, lvi)
End Function

'================================================================
'=
'================================================================
Declare Function AddColumn(hwnd As Long, txt As String)
Function AddColumn(hwnd As Long, txt As String)
    Var lvc As LVCOLUMN
    Var Col As Long
    
    lvc.mask = LVCF_TEXT Or LVCF_WIDTH
    lvc.pszText = StrAdr(txt & Chr$(0))
    lvc.fmt = 2
    lvc.cx = 100
    
    AddColumn = Api_SendMessage(hwnd, LVM_INSERTCOLUMN, Col, lvc)
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var Ret As Long

    Style = WS_CHILD Or WS_VISIBLE Or LVS_REPORT Or LVS_AUTOARRANGE
    
    hWndLV = Api_CreateWindowEx(WS_EX_CLIENTEDGE, "SysListView32", ByVal 0, Style, 10, 5, 210, 100, GethWnd, 0, GetInst, ByVal 0)

    Ret = AddColumn(hWndLV, "項目2")
    Ret = AddColumn(hWndLV, "項目1")

    Ret = AddItem(hWndLV, 0, "test1_0")
    Ret = AddItem(hWndLV, 1, "test2_0")

    Ret = AddSubItem(hWndLV, 0, 1, "test1_1")
    Ret = AddSubItem(hWndLV, 1, 1, "test2_1")

    Button1.DestroyWindow
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_QueryClose edecl ()
Sub MainForm_QueryClose()
    Var Ret As Long

    Ret = Api_DestroyWindow(hWndLV)
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End