回复 21# chzj589
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GuiListView.au3>
Opt('GUIOnEventMode', 1)
Global $idListView, $hListView, $pListViewFunc, $idAddSingleSel, $idDelSingleSel
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; Create GUI
GUICreate("ctrl+single-click --> single-click", 420, 200)
GUISetOnEvent(-3, '_Exit')
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 180, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
$hListView = GUICtrlGetHandle($idListView)
; Add columns to ListView
_GUICtrlListView_InsertColumn($idListView, 0, "Column 1", 94)
_GUICtrlListView_InsertColumn($idListView, 1, "Column 2", 94)
_GUICtrlListView_InsertColumn($idListView, 2, "Column 3", 94)
_GUICtrlListView_InsertColumn($idListView, 3, "Column 4", 94)
; Fill ListView
Local $iItems = 100
For $i = 0 To $iItems - 1
GUICtrlCreateListViewItem($i & "/Column 1|" & $i & "/Column 2|" & $i & "/Column 3|" & $i & "/Column 4", $idListView)
Next
; Subclass ListView to receive keyboard/mouse messages
$pListViewFunc = DllCallbackGetPtr(DllCallbackRegister("ListViewFunc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hListView, $pListViewFunc, 0, 0) ; $iSubclassId = 0, $pData = 0
; Add/del single selection
$idAddSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSel')
$idDelSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_DelSingleSel')
; Show GUI
GUISetState(@SW_SHOW)
EndFunc ;==>Example
Func ListViewFunc($hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData)
If $iMsg <> $WM_LBUTTONDOWN Then
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
If Not @error Then
Return $result[0]
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
If _GUICtrlListView_GetSelectedCount($idListView) Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($lParam, 0xFFFF), BitShift($lParam, 16))
If Not (@error Or $aHit[0] = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit[0]) Then
GUICtrlSendToDummy($idDelSingleSel, $aHit[0])
Else
GUICtrlSendToDummy($idAddSingleSel, $aHit[0])
EndIf
EndIf
Return 0
EndIf
Case 0x0009
Return 0
EndSwitch
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
If Not @error Then
Return $result[0]
Else
Return
EndIf
#forceref $iSubclassId, $pData
EndFunc ;==>ListViewFunc
Func _AddSingleSel()
Local $iItem = GUICtrlRead($idAddSingleSel)
_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
EndFunc ;==>_AddSingleSel
Func _DelSingleSel()
Local $iItem = GUICtrlRead($idDelSingleSel)
_GUICtrlListView_SetItemSelected($idListView, $iItem, False)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
EndFunc ;==>_DelSingleSel
Func _Exit()
_WinAPI_RemoveWindowSubclass($hListView, $pListViewFunc, 0)
GUIDelete()
Exit
EndFunc ;==>_Exit
Func _WinAPI_SetWindowSubclass($hWnd, $pSubclassProc, $idSubClass, $pData = 0)
Local $aRet = DllCall('comctl32.dll', 'bool', 'SetWindowSubclass', 'hwnd', $hWnd, 'ptr', $pSubclassProc, 'uint_ptr', $idSubClass, _
'dword_ptr', $pData)
If @error Then Return SetError(@error, @extended, 0)
; If Not $aRet[0] Then Return SetError(1000, 0, 0)
Return $aRet[0]
EndFunc ;==>_WinAPI_SetWindowSubclass
Func _WinAPI_RemoveWindowSubclass($hWnd, $pSubclassProc, $idSubClass)
Local $aRet = DllCall('comctl32.dll', 'bool', 'RemoveWindowSubclass', 'hwnd', $hWnd, 'ptr', $pSubclassProc, 'uint_ptr', $idSubClass)
If @error Then Return SetError(@error, @extended, False)
Return $aRet[0]
EndFunc ;==>_WinAPI_RemoveWindowSubclass
To LZ: au338 缺少的两个api函数给加上了,引用数组的语法也改了,应该可以各版通用 |