好像有点bug。。。。和wm_notify同时用,,,如果滚动条滚动过快会停止响应?!
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <ButtonConstants.au3>
#cs
autoit.com yamakawa
测试环境 win10 x64
autoit3 3.3.14.2
#ce
;版本判断
Local $verArray = StringSplit(@AutoItVersion,".")
If $verArray[3] < 14 Then MsgBox(4096,"来自Autoit.com -Yamakawa的提示","本段代码测试于 Autoit 3.3.14.2!" & @CRLF & @CRLF & @CRLF & "当前版本低于3.3.14.2 如果出现问题,请自行修改!")
;低于3.3.14.2则弹出提示。不兼容可能是版本问题
Global Const $ODT_LISTVIEW = 102
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODA_FOCUS = 0x4
Global Const $ODS_SELECTED = 0x0001
$hGUI = GUICreate("Test GUI", 650, 300)
;===第一个列表
$lListView = GUICtrlCreateListView("Items|SubItems", 10, 10, 280, 180, BitOR($LVS_REPORT, $LVS_OWNERDRAWFIXED))
$hlListView = GUICtrlGetHandle(-1)
_GUICtrlListView_SetExtendedListViewStyle($hlListView, $LVS_EX_FULLROWSELECT)
For $i = 1 To 15
_GUICtrlListView_AddItem($hlListView, "Item " & $i & " ")
_GUICtrlListView_AddSubItem($hlListView, $i - 1, "SubItem " & $i & " ", 1)
Next
_GUICtrlListView_SetColumnWidth($hlListView, 0, 120)
_GUICtrlListView_SetColumnWidth($hlListView, 1, 120)
;第二个列表
$rListView = GUICtrlCreateListView("", 310, 10, 280, 180)
$hrListView = GUICtrlGetHandle(-1)
_GUICtrlListView_SetExtendedListViewStyle($hrListView, $LVS_EX_FULLROWSELECT)
_GUICtrlListView_InsertColumn($hrListView, 0, "隐藏列", 0)
_GUICtrlListView_InsertColumn($hrListView, 1, "列1", 120)
_GUICtrlListView_InsertColumn($hrListView, 2, "列2", 120)
GUICtrlCreateLabel("双击左边列表项," & @CRLF & "如果当前项在右边列表不存在,则建立" & @CRLF & "如果当前项在右边列表存在,则删除",10,200)
$do = GUICtrlCreateButton("text", 10, 250)
GUICtrlSetStyle(-1, BitOR($WS_TABSTOP, $BS_NOTIFY, $BS_OWNERDRAW)) ; Set the ownerdrawn flag
GUICtrlCreateLabel("←←←←←←←这个按钮感觉如何(我指的形状)", 50, 255)
GUIRegisterMsg($WM_DRAWITEM, "WM_DRAWITEM")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
While 1
Switch GUIGetMsg()
Case -3
Exit
Case $do
MsgBox(0, "", "do is clicked", 0, $hGUI)
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iIDFrom
Case $lListView
Switch $iCode
Case $NM_DBLCLK
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $index = DllStructGetData($tInfo, "Index")
Local $newIndex = _GUICtrlListView_FindText($rListView,String($lListView & "," & $index))
If $newIndex <> -1 Then
_GUICtrlListView_DeleteItem ( $rListView, $newIndex )
Else
GUICtrlCreateListViewItem(String($lListView & "," & $index) &"|" & _GUICtrlListView_GetItemTextString($lListView, -1), $rListView)
EndIf
Return 1 ; not to allow the default processing
;~ Return 0 ; allow the default processing
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam);嘿嘿。新GET的。。。
Local $tagDRAWITEMSTRUCT, $iBrushColor, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC, $bSelected, $oldColor
$tagDRAWITEMSTRUCT = DllStructCreate( _
"uint cType;" & _
"uint cID;" & _
"uint itmID;" & _
"uint itmAction;" & _
"uint itmState;" & _
"hwnd hItm;" & _
"handle hDC;" & _
"long itmRect[4];" & _
"ulong_ptr itmData" _
, $lParam)
If $tagDRAWITEMSTRUCT.cType <> $ODT_LISTVIEW Then Return $GUI_RUNDEFMSG
If $tagDRAWITEMSTRUCT.itmAction <> $ODA_DRAWENTIRE Then Return $GUI_RUNDEFMSG
$txtcolor = BitAND($tagDRAWITEMSTRUCT.itmState, $ODS_SELECTED) = $ODS_SELECTED ? 0xffffff : 0x000000
_WinAPI_SetTextColor($tagDRAWITEMSTRUCT.hDC, $txtcolor)
Local $tRect = DllStructGetPtr($tagDRAWITEMSTRUCT, "itmRect")
$iBrushColor = BitAND($tagDRAWITEMSTRUCT.itmState, $ODS_SELECTED) = $ODS_SELECTED ? 0xff7bcc : 0xffffff ;旧版这句可能需要修改
Local $aBrush = _WinAPI_CreateSolidBrush($iBrushColor)
Local $aBrushOld = _WinAPI_SelectObject($tagDRAWITEMSTRUCT.hDC, $aBrush)
_WinAPI_FillRect($tagDRAWITEMSTRUCT.hDC, $tRect, $aBrush)
_WinAPI_SelectObject($tagDRAWITEMSTRUCT.hDC, $aBrushOld)
_WinAPI_DeleteObject($aBrush)
For $i = 0 To _GUICtrlListView_GetColumnCount($tagDRAWITEMSTRUCT.hItm)
Local $iItmText = _GUICtrlListView_GetItemText($tagDRAWITEMSTRUCT.hItm, $tagDRAWITEMSTRUCT.itmID, $i)
Local $iItmRect = DllStructCreate($tagRect)
If $i Then $iItmRect.top = $i ;这句啥意思还不理解。。。。。。怎么想来想去好像是列索引的意思???可是这个结构中不是指的top么??
DllCall('user32.dll', 'int', 'SendMessage', 'hwnd', $tagDRAWITEMSTRUCT.hItm, 'uint', $LVM_GETSUBITEMRECT, 'wparam', $tagDRAWITEMSTRUCT.itmID, 'lparam', DllStructGetPtr($iItmRect))
$iItmRect.left = $i ? $iItmRect.left + 5 : $iItmRect.left + 10 ;首项缩进10,其他缩进5
Local $iItmText = _GUICtrlListView_GetItemText($tagDRAWITEMSTRUCT.hItm, $tagDRAWITEMSTRUCT.itmID, $i)
_WinAPI_DrawText($tagDRAWITEMSTRUCT.hDC, $iItmText, $iItmRect, $DT_LEFT)
Next
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_DRAWITEM