こんにちはゲストさん。会員登録(無料)して質問・回答してみよう!

解決済みの質問

VB6 API LISTVIEW(チェックボックス付き)にチェックを付けたい

CreateWindowExでLISTVIEW(チェックボックス付きのレポートビュー)を作ったのですが、「山田花子」行にソースでチェックを付ける方法が分かりません。

Form1のソース

Option Explicit

Private Sub Form_Load()

Dim lngRet As Long
Dim lvcol As LV_COLUMN
Dim rStyle As Long
Dim item As LV_ITEM

'-- リストビューの作成
hList = CreateWindowEx(WS_EX_CLIENTEDGE, _
WC_LISTVIEW, "", _
WS_CHILD Or WS_VISIBLE Or WS_BORDER Or _
WS_CLIPSIBLINGS Or WS_CLIPCHILDREN Or _
LVS_REPORT, _
0&, 0&, Me.ScaleWidth / Screen.TwipsPerPixelX, 200&, _
Me.hwnd, _
0&, _
App.HINSTANCE, _
ByVal 0&)

'拡張スタイルを設定
rStyle = rStyle Or LVS_EX_FULLROWSELECT Or LVS_EX_GRIDLINES Or LVS_EX_CHECKBOXES
SendMessageByNum hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0&, rStyle

'-- カラムヘッダーの追加
With lvcol

.mask = LVCF_FMT Or LVCF_WIDTH Or LVCF_TEXT Or LVCF_SUBITEM
.fmt = LVCFMT_LEFT
.cx = 100
.pszText = "名前"
.iSubItem = 0
lngRet = ListView_InsertColumn(hList, 0&, lvcol)

End With

'-- リストアイテムの追加
With item

.mask = LVIF_TEXT Or LVIF_IMAGE

' 1つめ
.pszText = "山田 太郎"
.iItem = 0
.iSubItem = 0
.iImage = 0
lngRet = ListView_InsertItem(hList, item)

' 2つめ
.pszText = "山田 花子"
.iItem = 1
.iSubItem = 0
.iImage = 1
lngRet = ListView_InsertItem(hList, item)

End With

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'-- リストビューを破棄
DestroyWindow hList
End Sub

標準モジュールは補足に書きます

投稿日時 - 2007-07-17 13:36:28

QNo.3175318

すぐに回答ほしいです

質問者が選んだベストアンサー

LVITEMのstateMaskとstateメンバを適切に設定してLVM_SETITEMSTATEメッセージを送ればいいようです

Pubic Sub ListView_SetCheckState( hWnd as long, n as long, bflag as integer)
Const LVM_SETITMSTATE = (LVM_FIRST + 43)
  dim oItem as LVITEM
  dim bit as integer
  if bflag then
    bit = &h2000
  else
    bit = &h1000
  end if
  oItem.stateMask = &HF000
  oItem.state = bit

  SendMessage hWnd, LVM_SETITEMSTATE, n, oItem
End Sub

チェック状態の取得は
Pubic Function ListView_GetCheckState( hWnd as long, n as long ) as integer
  Const LVM_GETITMSTATE = (LVM_FIRST + 44)
  dim state as long
  state = SendMessage( hWnd, LVM_GETITEMSTATE, n, &HF000) / 4096
  ListView_GetCheckState = state - 1
End Function

といった具合だと思います

投稿日時 - 2007-07-17 15:51:06

お礼

上記のとおりで出来ました。
LVM_SETITEMSTATEメッセージのパラメータにビットを設定するんですね。
ありがとうございました。

投稿日時 - 2007-07-18 09:44:08

このQ&Aは役に立ちましたか?

5人が「このQ&Aが役に立った」と投票しています

回答(3)

ANo.2

VCだと
ListView_GetCheckStateというマクロで値を取得
ListView_SetCheckStateというマクロで値を設定しているようですが
VBに移植するのがめんどくさいです。。
動作未確認ですがListView_GetCheckStateだけは移植してみましたので張っておきます。

cmmctrl.hのマクロ(抜粋)

#define INDEXTOSTATEIMAGEMASK(i) ((i) << 12)

#define ListView_SetCheckState(hwndLV, i, fCheck) \
    ListView_SetItemState(hwndLV, i, INDEXTOSTATEIMAGEMASK((fCheck)?2:1), LVIS_STATEIMAGEMASK)

#define LVM_GETITEMSTATE 0x1000+44
#define LVIS_STATEIMAGEMASK 0xF000

#define ListView_GetCheckState(hwndLV, i) \
    ((((UINT)(SNDMSG((hwndLV), LVM_GETITEMSTATE, (WPARAM)(i), LVIS_STATEIMAGEMASK))) >> 12) -1)

'---------------------------------------------
' ListView_GetCheckState
'---------------------------------------------
Function ListView_GetCheckState(hwndLV As Long, i As Long) As Integer

  ListView_GetCheckState = (SendMessage(hwnd, &h102C, 0, &hF000) \ (2 ^ &hC)) - 1

Enf Function

投稿日時 - 2007-07-17 15:20:59

ANo.1

標準モジュール

Public hList As Long

Public Type LV_COLUMN
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

Public Type LV_ITEM
mask As Long
iItem As Long
iSubItem As Long
state As Long
stateMask As Long
pszText As String
cchTextMa As Long
iImage As Long
lParam As Long
iInden As Long
End Type

Public Const LVM_FIRST = &H1000

Public Const WS_EX_CLIENTEDGE = &H200&

Public Const WC_LISTVIEW = "SysListView32"
Public Const WS_CHILD = &H40000000
Public Const WS_VISIBLE = &H10000000
Public Const WS_BORDER = &H800000
Public Const WS_CLIPSIBLINGS = &H4000000
Public Const WS_CLIPCHILDREN = &H2000000
Public Const LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 55
Public Const LVS_ICON = &H0
Public Const LVS_REPORT = &H1
Public Const LVS_SMALLICON = &H2
Public Const LVS_LIST = &H3
Public Const LVS_TYPEMASK = &H3
Public Const LVS_SINGLESEL = &H4
Public Const LVS_SHOWSELALWAYS = &H8
Public Const LVS_SORTASCENDING = &H10
Public Const LVS_SORTDESCENDING = &H20
Public Const LVS_SHAREIMAGELISTS = &H40
Public Const LVS_NOLABELWRAP = &H80
Public Const LVS_AUTOARRANGE = &H100
Public Const LVS_EDITLABELS = &H200
Public Const LVS_OWNERDATA = &H1000
Public Const LVS_NOSCROLL = &H2000

Public Const LVS_EX_GRIDLINES = &H1 'グリッドライン
Public Const LVS_EX_SUBITEMIMAGES = &H2 'サブアイテムイメージ
Public Const LVS_EX_CHECKBOXES = &H4 'チェックボックス追加
Public Const LVS_EX_TRACKSELECT = &H8 'トラックセレクト
Public Const LVS_EX_HEADERDRAGDROP = &H10 'ヘッダードラッグ&ドロップ
Public Const LVS_EX_FULLROWSELECT = &H20 '一行選択
Public Const LVS_EX_ONECLICKACTIVATE = &H40 '1クリック選択
Public Const LVS_EX_TWOCLICKACTIVATE = &H80 '2クリック選択




Public Const LVS_TYPESTYLEMASK = &HFC00

Public Const LVS_ALIGNTOP = &H0
Public Const LVS_ALIGNLEFT = &H800
Public Const LVS_ALIGNMASK = &HC00

Public Const LVS_OWNERDRAWFIXED = &H400
Public Const LVS_NOCOLUMNHEADER = &H4000
Public Const LVS_NOSORTHEADER = &H8000

Public Const LVCF_FMT = &H1
Public Const LVCF_WIDTH = &H2
Public Const LVCF_TEXT = &H4
Public Const LVCF_SUBITEM = &H8
Public Const LVCF_IMAGE = &H10
Public Const LVCF_ORDER = &H20

Public Const LVCFMT_LEFT = &H0
Public Const LVCFMT_RIGHT = &H1
Public Const LVCFMT_CENTER = &H2
Public Const LVCFMT_JUSTIFYMASK = &H3
Public Const LVCFMT_IMAGE = &H800
Public Const LVCFMT_BITMAP_ON_RIGHT = &H1000
Public Const LVCFMT_COL_HAS_IMAGES = &H8000

Public Const LVIF_TEXT = &H1
Public Const LVIF_IMAGE = &H2
Public Const LVIF_PARAM = &H4
Public Const LVIF_STATE = &H8
Public Const LVIF_INDENT = &H10
Public Const LVIF_NORECOMPUTE = &H800

Public Const LVIS_FOCUSED = &H1
Public Const LVIS_SELECTED = &H2
Public Const LVIS_CUT = &H4
Public Const LVIS_DROPHILITED = &H8

Public Const LVIS_OVERLAYMASK = &HF00
Public Const LVIS_STATEIMAGEMASK = &HF000

Public Const LVSIL_NORMAL = 0
Public Const LVSIL_SMALL = 1
Public Const LVSIL_STATE = 2


Declare Function CreateWindowEx Lib "USER32" 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, lpParam As Any) As Long
Declare Function SendMessageByNum Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam 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
Declare Function DestroyWindow Lib "USER32" (ByVal hwnd As Long) As Long



Public Function ListView_InsertColumn(ByVal hwnd As Long, ByVal iCol As Long, pcol As LV_COLUMN) As Long
Const LVM_INSERTCOLUMN = (LVM_FIRST + 27)
ListView_InsertColumn = SendMessage(hwnd, LVM_INSERTCOLUMN, iCol, pcol)
End Function

Public Function ListView_SetColumn(ByVal hwnd As Long, ByVal iCol As Long, pcol As LV_COLUMN) As Long
Const LVM_SETCOLUMN = (LVM_FIRST + 26)
ListView_SetColumn = SendMessage(hwnd, LVM_SETCOLUMN, iCol, pcol)
End Function

Public Function ListView_InsertItem(ByVal hwnd As Long, pitem As LV_ITEM) As Long
Const LVM_INSERTITEM = (LVM_FIRST + 7)
ListView_InsertItem = SendMessage(hwnd, LVM_INSERTITEM, 0&, pitem)
End Function

Public Function ListView_SetItem(ByVal hwnd As Long, pitem As LV_ITEM) As Long
Const LVM_SETITEM = (LVM_FIRST + 6)
ListView_SetItem = SendMessage(hwnd, LVM_SETITEM, 0&, pitem)
End Function

Public Function ListView_SetImageList(ByVal hwnd As Long, ByVal himl As Long, ByVal iImageList As Long) As Long
Const LVM_SETIMAGELIST = (LVM_FIRST + 3)
ListView_SetImageList = SendMessage(hwnd, LVM_SETIMAGELIST, iImageList, ByVal himl)
End Function

投稿日時 - 2007-07-17 14:01:19

あなたにオススメの質問