powerbasic listview点击某行 获取行内信息
时间: 2023-08-18 18:15:04
浏览: 125
在 PowerBASIC 中,可以使用 Listview 控件的消息处理程序来获取用户点击某一行时该行的信息。下面是一个示例代码:
#COMPILE EXE
#DIM ALL
#INCLUDE "WIN32API.INC"
%ID_LISTVIEW = 100
GLOBAL hDlg AS DWORD
FUNCTION PBMAIN () AS LONG
LOCAL hListView AS DWORD
DIALOG NEW 0, "Listview Example",,, 300, 200, %WS_SYSMENU TO hDlg
CONTROL ADD "SysListView32", hDlg, %ID_LISTVIEW, "", 10, 10, 280, 150
ListView_SetExtendedListViewStyle GetDlgItem(hDlg, %ID_LISTVIEW), %LVS_EX_GRIDLINES OR %LVS_EX_FULLROWSELECT
' Add columns to the listview
LOCAL lvColumn AS LV_COLUMN
lvColumn.mask = %LVCF_TEXT OR %LVCF_WIDTH
lvColumn.cx = 100
lvColumn.pszText = "Column 1"
ListView_InsertColumn GetDlgItem(hDlg, %ID_LISTVIEW), 0, lvColumn
lvColumn.cx = 100
lvColumn.pszText = "Column 2"
ListView_InsertColumn GetDlgItem(hDlg, %ID_LISTVIEW), 1, lvColumn
' Add items to the listview
LOCAL lvItem AS LV_ITEM
lvItem.mask = %LVIF_TEXT
lvItem.iItem = 0
lvItem.iSubItem = 0
lvItem.pszText = "Item 1"
ListView_InsertItem GetDlgItem(hDlg, %ID_LISTVIEW), lvItem
lvItem.iSubItem = 1
lvItem.pszText = "Subitem 1"
ListView_SetItem GetDlgItem(hDlg, %ID_LISTVIEW), lvItem
lvItem.iItem = 1
lvItem.iSubItem = 0
lvItem.pszText = "Item 2"
ListView_InsertItem GetDlgItem(hDlg, %ID_LISTVIEW), lvItem
lvItem.iSubItem = 1
lvItem.pszText = "Subitem 2"
ListView_SetItem GetDlgItem(hDlg, %ID_LISTVIEW), lvItem
DIALOG SHOW MODAL hDlg CALL DlgProc
END FUNCTION
CALLBACK FUNCTION DlgProc() AS LONG
LOCAL nIndex AS LONG
LOCAL szBuffer AS ASCIIZ * 256
SELECT CASE CBMSG
CASE %WM_INITDIALOG
' Do initialization here
CASE %WM_COMMAND
SELECT CASE LOWRD(CBWPARAM)
CASE %ID_LISTVIEW
SELECT CASE HIWRD(CBWPARAM)
CASE %NM_CLICK
' Get the index of the clicked item
nIndex = ListView_GetNextItem(GetDlgItem(hDlg, %ID_LISTVIEW), -1, %LVNI_SELECTED)
IF nIndex >= 0 THEN
' Get the text of the first subitem in the clicked row
ListView_GetItemText GetDlgItem(hDlg, %ID_LISTVIEW), nIndex, 0, szBuffer, SIZEOF(szBuffer)
MSGBOX "Clicked row: " & STR$(nIndex + 1) & $CRLF & "Text: " & szBuffer
END IF
END SELECT
END SELECT
END SELECT
END FUNCTION
在这个示例中,我们创建了一个包含两列数据的 Listview 控件,并在其中添加了两个项目。当用户单击某个项目时,会在消息处理程序中获取所选项目的行索引,并使用 ListView_GetItemText 函数获取该行第一个子项的文本。然后,我们将该信息显示在一个消息框中。
注意,在这个示例中,我们使用了 Listview 控件的扩展风格 %LVS_EX_FULLROWSELECT,这使得用户单击 Listview 中的任何位置都会选择整行。