chzj589
发表于 2017-8-30 16:01:18
回复 45# kk_lee69
先谢谢了
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
;#include <WinAPIShellEx.au3>
#include <GUIEdit.au3>
;#include <WinAPIEx.au3>
Opt('GUIOnEventMode', 1)
Global $idListView, $hListView, $pListViewFunc, $idAddSingleSel, $idDelSingleSel, $hListView2, $Edit1, $Inputu7, $Listview2
Global $Inputu8, $idAddSingleSela
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; Create GUI
GUICreate("ctrl+single-click --> single-click", 840, 610)
GUISetOnEvent(-3, '_Exit')
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
$hListView = GUICtrlGetHandle($idListView)
GUICtrlSetBkColor($idListView, 0xCDCDCD);0xECE9D8);
_GUICtrlListView_SetBkColor($idListView, 14675183)
_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)
$hImage = _GUIImageList_Create(1, 18);30为每行的间距(即网格高度)
_GUICtrlListView_SetImageList($idListView, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($idListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
Local $iItems = 18
For $i = 0 To $iItems - 1
GUICtrlCreateListViewItem($i & "/Column 1|" & $i & "/Column 2|" & $i & "/Column 3|" & $i & "/Column 4", $idListView)
Next
$pListViewFunc = DllCallbackGetPtr(DllCallbackRegister("ListViewFunc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hListView, $pListViewFunc, 0, 0) ; $iSubclassId = 0, $pData = 0
$idAddSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSel')
$idDelSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_DelSingleSel')
$Listview2 = GUICtrlCreateListView("", 420, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
Local $hListView2 = GUICtrlGetHandle($Listview2)
GUICtrlSetBkColor($Listview2, 0xC0E0FF);0xECE9D8);
_GUICtrlListView_SetBkColor($Listview2, 14675183)
_GUICtrlListView_InsertColumn($Listview2, 0, "id", 54)
_GUICtrlListView_InsertColumn($Listview2, 1, "Column 2", 94)
_GUICtrlListView_InsertColumn($Listview2, 2, "Column 3", 94)
_GUICtrlListView_InsertColumn($Listview2, 3, "Column 4", 94)
$hImage = _GUIImageList_Create(1, 18);30为每行的间距(即网格高度)
_GUICtrlListView_SetImageList($Listview2, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
$Edit1 = GUICtrlCreateEdit("", 10, 400, 810, 200, $WS_VSCROLL + $ES_WANTRETURN)
GUICtrlSetFont(-1, 13, 600, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x009900)
$Inputu7 = GUICtrlCreateLabel("", 680, 560, 90, 17)
GUICtrlSetState($Inputu7, $GUI_HIDE)
$Inputu8 = GUICtrlCreateLabel("", 680, 590, 90, 17)
GUICtrlSetState($Inputu8, $GUI_HIDE)
$idAddSingleSela = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSela')
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
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
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 = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSel, $aHit)
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
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 _AddSingleSela()
Local $iItem = GUICtrlRead($idAddSingleSela)
_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_GUICtrlListView_SetItemSelected($Listview2, $iItem, True)
_GUICtrlListView_SetItemFocused($Listview2, $iItem)
GUICtrlSetBkColor($idListView, 0xC0E0FF)
_GUICtrlListView_SetBkColor($idListView, 14675183)
EndFunc ;==>_AddSingleSela
Func _DelSingleSel()
Local $iItem = GUICtrlRead($idDelSingleSel)
_GUICtrlListView_SetItemSelected($idListView, $iItem, False)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_DEL()
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)
Return $aRet
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
EndFunc ;==>_WinAPI_RemoveWindowSubclass
Func WM_NOTIFY($hWndGUI, $MsgID, $wParam, $ilParam)
Local $hWndFrom, $iCode
Local $tNMHDR, $iCode, $hMenu, $hWnd, $iMsg
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView;$hWndListView;这里改成你的listview的ID
Switch $iCode
Case $NM_DBLCLK
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = DllStructGetData($tInfo, "Index")
GUICtrlSetData($Inputu8, $idd)
_ListView2()
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC, $Font1
Switch $iItem
Case 2;条目行数
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then ;设置首列, 即项目
$iColor = RGB2BGR(0xFF0000)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then ;设置子项, 即列索引大于0时
$iColor = RGB2BGR(0x009900)
;_WinAPI_SelectObject($hDC, $Font2) ;设置用于设备场景内文本的字体
DllStructSetData($tCustDraw, "clrText", $iColor) ;设置文本色
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80)) ;设置文本背景色
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF00FF);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
ElseIf $iSubItem = 3 Then
$iColor = RGB2BGR(0x000080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
EndIf
Case 4 ;条目行数
If $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF0080);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(16710321))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Local $tagNMHDR, $Event, $hWndFrom, $IDFrom, $iCode
Local $tagNMHDR = DllStructCreate("int;int;int", $ilParam)
If @error Then Return $GUI_RUNDEFMSG
$IDFrom = DllStructGetData($tagNMHDR, 2)
$Event = DllStructGetData($tagNMHDR, 3)
$tagNMHDR = 0
Switch $IDFrom;选择产生事件的控件
Case $hListView;$hWndListView;这里改成你的listview的ID
Case $Listview2
Switch $Event; 选择产生的事件
Case $NM_CLICK ; 左击
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
If $iMsg <> $WM_LBUTTONDOWN Then
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
If _GUICtrlListView_GetSelectedCount($idListView) Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($ilParam, 0xFFFF), BitShift($ilParam, 16))
If Not (@error Or $aHit = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
;GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSela, $aHit)
EndIf
EndIf
Return 0
EndIf
Case 0x0009
Return 0
EndSwitch
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
Case $NM_DBLCLK ; 双击
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
MsgBox(0, "", "未选定")
Return
EndIf
; MsgBox(0, 0, _GUICtrlListView_GetItemText($ListView2, Number($Index), 0))
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
Case $NM_RCLICK ; 右击
;~ ...
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR
Func _ListView2()
GUICtrlSetData($Inputu7, "")
;$Strn = GUICtrlRead(GUICtrlRead($Listview2))
;$Strnspin = StringSplit($Strn, "|")
Local $Inpa = GUICtrlRead($Inputu8)
$cont = _GUICtrlListView_GetItemCount($hListView);取得数组的行列数
$h1 = $cont + 1
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($hListView, $Inpa)
$h1 = $str
$h2 = $str
$h3 = $str
$h4 = $str
Next
;$num1 = $cont + 1
$Number = _GUICtrlListView_GetItemCount($Listview2)
$num1 = $Number + 1
_GUICtrlListView_AddItem($Listview2, $num1, $Number)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h2, 1)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h3, 2)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h4, 3)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
;_GUICtrlEdit_AppendText($Edit,"记录:---------------------------------" & @CRLF)
_GUICtrlEdit_AppendText($Edit1, "" & $h1 & "," & $h2 & "," & $h3 & "," & $h4 & @CRLF)
EndFunc ;==>_ListView2
Func _DEL();删除
Local $Inpa = GUICtrlRead($Inputu7) - 1
GUICtrlSetData($Inputu7, $Inpa)
_GUICtrlEdit_ReplaceLine($Edit1, '[\r\n]+$', '', 0, 1)
_GUICtrlEdit_ReplaceLine($Edit1, $Inpa)
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If $Index <> "" Then
;$msg = MsgBox(49, "删除记录", "确定要删除记录?")
;If $msg == 1 Then
$DEL = (_GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Listview2)))
;Else
;EndIf
Else
;MsgBox(0, "提示", "请选择条目", 1)
EndIf
GUICtrlSetData($Inputu7, "")
GUICtrlSetData($Inputu8, "")
EndFunc ;==>_DEL
;==========================================================================================
; 说明: 按指定的关键字或行号替换或删除Edit编辑框行字符,可选择是否替换整行、是否使用正则表达式。
; 语法: _GUICtrlEdit_ReplaceLine('控件句柄', '搜索字符或'行号[, '替换'[, 替换行标志[, 正则标志]]])
; 参数: '搜索字符串或'行号: 如果使用行号(0基)则不要用引号,否则会作为字符串搜索;
; 使用 -1 则匹配最后一行
; '替换'[可选]: 如'替换'字符为空(默认)则为删除。
; 替换行标志[可选]: 标志 = 1 :包含关键字的整行被替换;标志 = 0 仅替换搜索字符(默认)。
; 如果搜索参数为行号,则此参数无效(整行被替换,等同于此参数为1)
; 正则标志[可选]: 正则标志 = 1 使用正则,为 0 不使用正则(默认),使用原始字符串匹配。
; 返回值:成功: 返回1;
; 失败: 返回0,并设置@Error为以下值:
; @Error = 1 编辑框为空
; @Error = 2 编辑文本数量超出限制(注:可用 GUICtrlSetLimit() 设置)
; @Error = 3 搜索的行号超出现有总行数范围
; @Error = 4 未搜索到匹配字符串
; 作者: Afan -- http://www.autoitx.com
;=====================================================================================start
Func _GUICtrlEdit_ReplaceLine($hWnd, $pattern, $replace = '', $flag = 0, $SRE = 0)
Local $Str_all = GUICtrlRead($hWnd)
If $Str_all = '' Then Return SetError(1, 0, 0)
If _GUICtrlEdit_GetLimitText($hWnd) < StringLen($Str_all) Then Return SetError(2, 0, 0)
Local $Index, $nLine, $strZY, $sR, $iStart, $iEnd, $str, $Len, $extended
If IsNumber($pattern) Then
$Index = _GUICtrlEdit_GetLineCount($hWnd)
If $pattern = -1 Then
$nLine = $Index - 1
Else
$nLine = $pattern
If $nLine > $Index - 1 Then Return SetError(3, 0, 0)
EndIf
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
If $SRE = 0 Then
$strZY = '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)'
$pattern = StringRegExpReplace($pattern, $strZY, '\\$1')
$replace = StringRegExpReplace($replace, $strZY, '\\$1')
EndIf
$sR = StringRegExp($Str_all, $pattern, 1)
If @error Then Return SetError(4, 0, 0)
$extended = @extended
If $flag = 1 Or $flag = -1 Then
$nLine = _GUICtrlEdit_LineFromChar($hWnd, $extended - 1)
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
$iEnd = $extended - 1
$str = $sR
$Len = StringLen($str)
$iStart = $iEnd - $Len
EndIf
EndIf
_GUICtrlEdit_SetSel($hWnd, $iStart, $iEnd)
GUICtrlSetData($hWnd, $replace, 1)
Return 1
EndFunc ;==>_GUICtrlEdit_ReplaceLine
kk_lee69
发表于 2017-8-30 16:49:32
本帖最后由 kk_lee69 于 2017-8-30 16:55 编辑
回复 46# chzj589
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
;#include <WinAPIShellEx.au3>
#include <GUIEdit.au3>
;#include <WinAPIEx.au3>
Opt('GUIOnEventMode', 1)
Global $idListView, $hListView, $pListViewFunc, $idAddSingleSel, $idDelSingleSel, $hListView2, $Edit1, $Inputu7, $Listview2
Global $Inputu8, $idAddSingleSela
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; Create GUI
GUICreate("ctrl+single-click --> single-click", 840, 610)
GUISetOnEvent(-3, '_Exit')
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES )
$hListView = GUICtrlGetHandle($idListView)
; GUICtrlSetBkColor($idListView, 0xCDCDCD);0xECE9D8);
; _GUICtrlListView_SetBkColor($idListView, 14675183)
_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)
$hImage = _GUIImageList_Create(1, 18);30?每行的?距(即网格高度)
_GUICtrlListView_SetImageList($idListView, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($idListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP+$LVS_EX_CHECKBOXES ))
Local $iItems = 18
For $i = 0 To $iItems - 1
GUICtrlCreateListViewItem($i & "/Column 1|" & $i & "/Column 2|" & $i & "/Column 3|" & $i & "/Column 4", $idListView)
Next
$pListViewFunc = DllCallbackGetPtr(DllCallbackRegister("ListViewFunc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hListView, $pListViewFunc, 0, 0) ; $iSubclassId = 0, $pData = 0
$idAddSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSel')
$idDelSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_DelSingleSel')
$Listview2 = GUICtrlCreateListView("", 420, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
Local $hListView2 = GUICtrlGetHandle($Listview2)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF);0xECE9D8);
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
_GUICtrlListView_InsertColumn($Listview2, 0, "id", 54)
_GUICtrlListView_InsertColumn($Listview2, 1, "Column 2", 94)
_GUICtrlListView_InsertColumn($Listview2, 2, "Column 3", 94)
_GUICtrlListView_InsertColumn($Listview2, 3, "Column 4", 94)
$hImage = _GUIImageList_Create(1, 18);30?每行的?距(即网格高度)
_GUICtrlListView_SetImageList($Listview2, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
$Edit1 = GUICtrlCreateEdit("", 10, 400, 810, 200, $WS_VSCROLL + $ES_WANTRETURN)
GUICtrlSetFont(-1, 13, 600, 0, "微?雅黑")
;GUICtrlSetColor(-1, 0x009900)
$Inputu7 = GUICtrlCreateLabel("", 680, 560, 90, 17)
GUICtrlSetState($Inputu7, $GUI_HIDE)
$Inputu8 = GUICtrlCreateLabel("", 680, 590, 90, 17)
GUICtrlSetState($Inputu8, $GUI_HIDE)
$idAddSingleSela = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSela')
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
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
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
ConsoleWrite(BitAND($lParam, 0xFFFF)&@CRLF)
If _GUICtrlListView_GetSelectedCount($idListView) Then
If BitAND($lParam, 0xFFFF) > 16 Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($lParam, 0xFFFF), BitShift($lParam, 16))
If Not (@error Or $aHit = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSel, $aHit)
EndIf
EndIf
Return 0
EndIf
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
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 _AddSingleSela()
Local $iItem = GUICtrlRead($idAddSingleSela)
_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_GUICtrlListView_SetItemSelected($Listview2, $iItem, True)
_GUICtrlListView_SetItemFocused($Listview2, $iItem)
;GUICtrlSetBkColor($idListView, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($idListView, 14675183)
EndFunc ;==>_AddSingleSela
Func _DelSingleSel()
Local $iItem = GUICtrlRead($idDelSingleSel)
_GUICtrlListView_SetItemSelected($idListView, $iItem, False)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_DEL()
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)
Return $aRet
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
EndFunc ;==>_WinAPI_RemoveWindowSubclass
Func WM_NOTIFY($hWndGUI, $MsgID, $wParam, $ilParam)
Local $hWndFrom, $iCode
Local $tNMHDR, $iCode, $hMenu, $hWnd, $iMsg
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView;$hWndListView;?里改成你的listview的ID
Switch $iCode
Case $NM_DBLCLK
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = DllStructGetData($tInfo, "Index")
GUICtrlSetData($Inputu8, $idd)
_ListView2()
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC, $Font1
IF _GUICtrlListView_GetItemChecked($hListView,$iItem)=TrueThen
DllStructSetData($tCustDraw, "clrTextBk",0x738393);BGR格式顏色值 高亮背景色
DllStructSetData($tCustDraw, "clrText", 0xFFFFFF) ;高亮後文字改為白色
EndIf
Switch $iItem
Case 2;?目行?
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then ;?置首列, 即?目
$iColor = RGB2BGR(0xFF0000)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then ;?置子?, 即列索引大于0?
$iColor = RGB2BGR(0x009900)
;_WinAPI_SelectObject($hDC, $Font2) ;?置用于???景?文本的字体
DllStructSetData($tCustDraw, "clrText", $iColor) ;?置文本色
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80)) ;?置文本背景色
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF00FF);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
ElseIf $iSubItem = 3 Then
$iColor = RGB2BGR(0x000080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
EndIf
Case 4 ;?目行?
If $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF0080);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(16710321))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Local $tagNMHDR, $Event, $hWndFrom, $IDFrom, $iCode
Local $tagNMHDR = DllStructCreate("int;int;int", $ilParam)
If @error Then Return $GUI_RUNDEFMSG
$IDFrom = DllStructGetData($tagNMHDR, 2)
$Event = DllStructGetData($tagNMHDR, 3)
$tagNMHDR = 0
Switch $IDFrom;???生事件的控件
Case $hListView;$hWndListView;?里改成你的listview的ID
Case $Listview2
Switch $Event; ???生的事件
Case $NM_CLICK ; 左?
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
If $iMsg <> $WM_LBUTTONDOWN Then
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
If _GUICtrlListView_GetSelectedCount($idListView) Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($ilParam, 0xFFFF), BitShift($ilParam, 16))
If Not (@error Or $aHit = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
;GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSela, $aHit)
EndIf
EndIf
Return 0
EndIf
Case 0x0009
Return 0
EndSwitch
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
Case $NM_DBLCLK ; ??
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If Not StringLen($Index) Then; ?里用以判?是否?定了ListViewItem
MsgBox(0, "", "未?定")
Return
EndIf
; MsgBox(0, 0, _GUICtrlListView_GetItemText($ListView2, Number($Index), 0))
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
Case $NM_RCLICK ; 右?
;~ ...
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR
Func _ListView2()
GUICtrlSetData($Inputu7, "")
;$Strn = GUICtrlRead(GUICtrlRead($Listview2))
;$Strnspin = StringSplit($Strn, "|")
Local $Inpa = GUICtrlRead($Inputu8)
$cont = _GUICtrlListView_GetItemCount($hListView);取得??的行列?
$h1 = $cont + 1
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($hListView, $Inpa)
$h1 = $str
$h2 = $str
$h3 = $str
$h4 = $str
Next
;$num1 = $cont + 1
$Number = _GUICtrlListView_GetItemCount($Listview2)
$num1 = $Number + 1
_GUICtrlListView_AddItem($Listview2, $num1, $Number)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h2, 1)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h3, 2)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h4, 3)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
;_GUICtrlEdit_AppendText($Edit,"??:---------------------------------" & @CRLF)
_GUICtrlEdit_AppendText($Edit1, "" & $h1 & "," & $h2 & "," & $h3 & "," & $h4 & @CRLF)
EndFunc ;==>_ListView2
Func _DEL();?除
Local $Inpa = GUICtrlRead($Inputu7) - 1
GUICtrlSetData($Inputu7, $Inpa)
_GUICtrlEdit_ReplaceLine($Edit1, '[\r\n]+$', '', 0, 1)
_GUICtrlEdit_ReplaceLine($Edit1, $Inpa)
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If $Index <> "" Then
;$msg = MsgBox(49, "?除??", "确定要?除???")
;If $msg == 1 Then
$DEL = (_GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Listview2)))
;Else
;EndIf
Else
;MsgBox(0, "提示", "????目", 1)
EndIf
GUICtrlSetData($Inputu7, "")
GUICtrlSetData($Inputu8, "")
EndFunc ;==>_DEL
;==========================================================================================
; ?明: 按指定的??字或行?替?或?除Edit??框行字符,可??是否替?整行、是否使用正?表?式。
; ?法: _GUICtrlEdit_ReplaceLine('控件句柄', '搜索字符或'行?[, '替?'[, 替?行?志[, 正??志]]])
; ??: '搜索字符串或'行?: 如果使用行?(0基)?不要用引?,否??作?字符串搜索;
; 使用 -1 ?匹配最后一行
; '替?'[可?]: 如'替?'字符?空(默?)???除。
; 替?行?志[可?]: ?志 = 1 :包含??字的整行被替?;?志 = 0 ?替?搜索字符(默?)。
; 如果搜索???行?,?此???效(整行被替?,等同于此???1)
; 正??志[可?]: 正??志 = 1 使用正?,? 0 不使用正?(默?),使用原始字符串匹配。
; 返回值:成功: 返回1;
; 失?: 返回0,并?置@Error?以下值:
; @Error = 1 ??框?空
; @Error = 2 ??文本?量超出限制(注:可用 GUICtrlSetLimit() ?置)
; @Error = 3 搜索的行?超出?有?行?范?
; @Error = 4 未搜索到匹配字符串
; 作者: Afan -- http://www.autoitx.com
;=====================================================================================start
Func _GUICtrlEdit_ReplaceLine($hWnd, $pattern, $replace = '', $flag = 0, $SRE = 0)
Local $Str_all = GUICtrlRead($hWnd)
If $Str_all = '' Then Return SetError(1, 0, 0)
If _GUICtrlEdit_GetLimitText($hWnd) < StringLen($Str_all) Then Return SetError(2, 0, 0)
Local $Index, $nLine, $strZY, $sR, $iStart, $iEnd, $str, $Len, $extended
If IsNumber($pattern) Then
$Index = _GUICtrlEdit_GetLineCount($hWnd)
If $pattern = -1 Then
$nLine = $Index - 1
Else
$nLine = $pattern
If $nLine > $Index - 1 Then Return SetError(3, 0, 0)
EndIf
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
If $SRE = 0 Then
$strZY = '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)'
$pattern = StringRegExpReplace($pattern, $strZY, '\\$1')
$replace = StringRegExpReplace($replace, $strZY, '\\$1')
EndIf
$sR = StringRegExp($Str_all, $pattern, 1)
If @error Then Return SetError(4, 0, 0)
$extended = @extended
If $flag = 1 Or $flag = -1 Then
$nLine = _GUICtrlEdit_LineFromChar($hWnd, $extended - 1)
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
$iEnd = $extended - 1
$str = $sR
$Len = StringLen($str)
$iStart = $iEnd - $Len
EndIf
EndIf
_GUICtrlEdit_SetSel($hWnd, $iStart, $iEnd)
GUICtrlSetData($hWnd, $replace, 1)
Return 1
EndFunc ;==>_GUICtrlEdit_ReplaceLine
看一下第180 行那邊指定 如果被CHECKED顏色就變 但是你的範例我弄不出來理論上應該是 跟 SELEDTED 顏色依樣才對
然後 你只要 把單擊 SECECTED 變成單擊 CHECKED就可以 顏色不受 其他 LISTVIEW 影響
只是 你要抓資料就不能抓 SELECTED而是要抓 CHECKED
kk_lee69
发表于 2017-8-30 16:54:50
回复 46# chzj589
能理解嗎看一下我的另外一個範例
在公司實在沒時間處理顏色問題也不熟
這個範例的顏色是正常的.....
#PRE_UseX64=n
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
;#include <WinAPITheme.au3>
#include <GuiListView.au3>
#include <WinAPISys.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <array.au3>
#include <Misc.au3>
Global $B_DESCENDING ; 排序用陣列
Global $hActLst
Global $bCtrlDown = False;listview控制項ctrl鍵按下標識
Global $hEdit, $Item = -1, $SubItem = 0
Global $hEnter, $aAccelKeys = [["{ENTER}", $hEnter]];虛擬控制項的控制碼和綁定的快速鍵
Local $hUser32 = DllOpen("user32.dll")
Local $ichked = 0;勾選的行數
Local $tText = DllStructCreate("wchar Text");建個結構,用來放listview列資料
Local $GUI, $hImage
Local $iITEM_COUNT = 1000, $aShowdata[$iITEM_COUNT], $aShowdata2[$iITEM_COUNT], $aShowdata3[$iITEM_COUNT];注意陣列最大專案數為2^24(16777216),所以$iITEM_COUNT = 4194304為二維陣列$aShowdata最大行數
Local $sSomeString = ""
Local $hGUI, $hListView, $hListView2, $hListView3
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$hGUI = GUICreate("ListView虛表 載入[" & UBound($aShowdata) & "]資料 核取方塊 原地編輯例子 By_Crossdoor", 600, 605)
$hListView = GUICtrlCreateListView("$hListView| | ", 2, 2, 295, 296, BitOR($LVS_REPORT, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_OWNERDATA), $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;~ _WinAPI_SetWindowTheme(GUICtrlGetHandle($hListView), "Explorer");設置主題
;設置列寬
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 0, 110)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);設定數據總數
$hListView2 = GUICtrlCreateListView("$hListView2| | ", 300, 2, 295, 296, BitOR($LVS_REPORT, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_OWNERDATA), $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;~ _WinAPI_SetWindowTheme(GUICtrlGetHandle($hListView), "Explorer");設置主題
;設置列寬
GUICtrlSendMsg($hListView2, $LVM_SETCOLUMNWIDTH, 0, 110)
GUICtrlSendMsg($hListView2, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg($hListView2, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlSendMsg($hListView2, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);設定數據總數
$hListView3 = GUICtrlCreateListView("$hListView3| | ", 300, 300, 295, 296, BitOR($LVS_REPORT, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_OWNERDATA), $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;~ _WinAPI_SetWindowTheme(GUICtrlGetHandle($hListView), "Explorer");設置主題
;設置列寬
GUICtrlSendMsg($hListView3, $LVM_SETCOLUMNWIDTH, 0, 110)
GUICtrlSendMsg($hListView3, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg($hListView3, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlSendMsg($hListView3, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);設定數據總數
;===========創建並關聯圖像清單
$hImage = _GUIImageList_Create()
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110)
_GUICtrlListView_SetImageList($hListView, $hImage, 1);分配圖像清單到清單視圖控制項$hListView
_GUICtrlListView_SetImageList($hListView2, $hImage, 1);分配圖像清單到清單視圖控制項$hListView2
$Button1 = GUICtrlCreateButton("選中行", 10, 310, 70, 25)
$Button2 = GUICtrlCreateButton("未選行", 80, 310, 70, 25)
$Button3 = GUICtrlCreateButton("修改", 150, 310, 70, 25)
$Button4 = GUICtrlCreateButton("清空", 220, 310, 70, 25)
GUISetState()
;==================在表單顯示後再初始化資料,因為初始化陣列資料比較慢
;如果先初始化陣列後顯示視窗,會卡很久,就造成虛表也很慢的假像
For $i = 0 To $iITEM_COUNT - 1
;重繪 刷新 listview 0-13行(讓ListView顯示陣列資料),不用太多行,只需要一屏的行數就就可以
;因為下拉滾動的時候listview會收到LVN_GETDISPINFO消息,這個消息我們在WM_NOTIFY內處理過了
If $i = 18 Then
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, $i)
GUICtrlSendMsg($hListView2, $LVM_REDRAWITEMS, 0, $i)
GUICtrlSendMsg($hListView3, $LVM_REDRAWITEMS, 0, $i)
EndIf
$aShowdata[$i] = "爺爺" & $i
$aShowdata[$i] = "爸爸" & $i
$aShowdata[$i] = "兒子" & $i
$aShowdata[$i] = 4096;核取方塊狀態4096未選中 8192選中
$aShowdata2[$i] = "2爺爺" & $i
$aShowdata2[$i] = "2爸爸" & $i
$aShowdata2[$i] = "2兒子" & $i
$aShowdata2[$i] = 4096;核取方塊狀態4096未選中 8192選中
$aShowdata3[$i] = "3爺爺" & $i
$aShowdata3[$i] = "3爸爸" & $i
$aShowdata3[$i] = "3兒子" & $i
$aShowdata3[$i] = 4096;核取方塊狀態4096未選中 8192選中
Next
While 1
$bCtrlDown = _IsPressed("11", $hUser32);取ctrl按鍵的狀態
Switch GUIGetMsg()
Case -3
ExitLoop
Case $Button1
$aShowdata = 8192
$aShowdata = 8192
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 3, 7)
;~ $a = _GetChkIndex(8192)
;~ If $a = -1 Then
;~ MsgBox(32, "提示", "未勾選任何數據")
;~ Else
;~ _ArrayDisplay($a, "勾選行的索引");陣列很大時,_ArrayDisplay會非常耗時
;~ EndIf
Case $Button2
$a = _GetChkIndex(4096)
If $a = -1 Then
MsgBox(32, "提示", "數據全都被勾選了。")
Else
;MsgBox(32, "提示", "未勾選的數據有" & UBound($a) & "行")
_ArrayDisplay($a, "未勾選行的索");陣列很大時,_ArrayDisplay會非常耗時
EndIf
Case $Button3
If StringLen($sSomeString) Then
$sSomeString = ""
Else
$sSomeString = Asc(Random(48, 255, 1))
EndIf
For $i = 0 To $iITEM_COUNT - 1
$aShowdata[$i] = "爺爺" & $i & "-" & $sSomeString
$aShowdata[$i] = "爸爸" & $i & "-" & $sSomeString
$aShowdata[$i] = "兒子" & $i & "-" & $sSomeString
Next
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button4
$aShowdata = 0
$iITEM_COUNT = 0
GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0)
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
EndSwitch
WEnd
GUIDelete()
DllClose($hUser32)
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 $hListView
Switch $iCode
Case $LVN_COLUMNCLICK ; 點擊列頭排序
_SortItems($iIDFrom, $ilParam, $aShowdata)
Case $NM_CUSTOMDRAW;自繪消息處理選中行背景色
Local $tCustDraw = DllStructCreate("hwnd hwndFrom;int idFrom;int code;dword DrawStage;hwnd hdc;long rect;dword ItemSpec;int ItemState;dword Itemlparam;dword clrText;dword clrTextBk;int SubItem; dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText;int Align", $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "DrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
_SetchkedBkClr($tCustDraw, $aShowdata, 0xFF9933) ;高亮藍 0xFF9933更高亮藍 0xff4e00
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED ; An item has changed
_CtrlSel($iIDFrom, $ilParam, $aShowdata)
Case $LVN_BEGINSCROLL ; 滾動事件開始
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata);如果存在$hEdit控制項,則結束編輯
Case $NM_CLICK;按一下
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata)
_chkboxState($iIDFrom, $ilParam, $aShowdata);刷新核取方塊狀態
Case $NM_DBLCLK ; 按兩下
_StartItemEdit($iIDFrom, $ilParam, $aShowdata);開始原地編輯
Case -150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177 請求顯示資料
_LVN_GETDISPINFO($iIDFrom, $ilParam, $aShowdata)
EndSwitch
Case $hListView2
Switch $iCode
Case -150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177 請求顯示資料
_LVN_GETDISPINFO($iIDFrom, $ilParam, $aShowdata2)
Case $LVN_COLUMNCLICK ; 點擊列頭排序
_SortItems($iIDFrom, $ilParam, $aShowdata2)
Case $NM_CUSTOMDRAW;自繪消息處理選中行背景色
Local $tCustDraw = DllStructCreate("hwnd hwndFrom;int idFrom;int code;dword DrawStage;hwnd hdc;long rect;dword ItemSpec;int ItemState;dword Itemlparam;dword clrText;dword clrTextBk;int SubItem; dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText;int Align", $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "DrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
_SetchkedBkClr($tCustDraw, $aShowdata2, 0x738393)
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED ; An item has changed
_CtrlSel($iIDFrom, $ilParam, $aShowdata2)
Case $LVN_BEGINSCROLL ; 滾動事件開始
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata2);如果存在$hEdit控制項,則結束編輯
Case $NM_CLICK;按一下
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata2)
_chkboxState($iIDFrom, $ilParam, $aShowdata2)
Case $NM_DBLCLK ; 按兩下
_StartItemEdit($iIDFrom, $ilParam, $aShowdata2)
EndSwitch
Case $hListView3
Switch $iCode
Case -150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177 請求顯示資料
_LVN_GETDISPINFO($iIDFrom, $ilParam, $aShowdata3)
Case $LVN_COLUMNCLICK ; 點擊列頭排序
_SortItems($iIDFrom, $ilParam, $aShowdata3)
Case $NM_CUSTOMDRAW;自繪消息處理選中行背景色
Local $tCustDraw = DllStructCreate("hwnd hwndFrom;int idFrom;int code;dword DrawStage;hwnd hdc;long rect;dword ItemSpec;int ItemState;dword Itemlparam;dword clrText;dword clrTextBk;int SubItem; dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText;int Align", $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "DrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
_SetchkedBkClr($tCustDraw, $aShowdata3, 0xD307AD, 0xE3F7ED)
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED ; An item has changed
_CtrlSel($iIDFrom, $ilParam, $aShowdata3)
Case $LVN_BEGINSCROLL ; 滾動事件開始
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata3);如果存在$hEdit控制項,則結束編輯
Case $NM_CLICK;按一下
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata3)
_chkboxState($iIDFrom, $ilParam, $aShowdata3)
Case $NM_DBLCLK ; 按兩下
_StartItemEdit($iIDFrom, $ilParam, $aShowdata3)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
$iIDFrom = _WinAPI_LoWord($wParam)
$iCode = _WinAPI_HiWord($wParam)
Switch $lParam
Case $hEdit
Switch $iCode
Case $EN_KILLFOCUS;編輯方塊失去焦點,結束編輯
Switch $hActLst
Case $hListView
_EndItemEdit($hListView, $aShowdata)
Case $hListView2
_EndItemEdit($hListView2, $aShowdata2)
Case $hListView3
_EndItemEdit($hListView3, $aShowdata3)
EndSwitch
Case $EN_UPDATE
EndSwitch
Case $hEnter;虛擬控制項消息觸發收到回車消息才觸發
If IsHWnd($hEdit) Then
If $SubItem = 0 Then
__ClickListViewItem($hActLst, $Item, True);使用點擊來結束編輯,省去判斷listview的資料陣列
Else
__ClickListViewItem($hActLst, $Item)
EndIf
EndIf
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Func _SetchkedBkClr(ByRef $tCustDraw, ByRef $aDataArray, $ihighBkClr = 0xF7D3AD, $idefBkclr = 0xFFFFFF)
Local $iIndex = DllStructGetData($tCustDraw, "ItemSpec")
If $aDataArray[$iIndex] = 4096 Then
DllStructSetData($tCustDraw, "clrTextBk", $idefBkclr);BGR格式顏色值 默認背景色
Else
DllStructSetData($tCustDraw, "clrTextBk", $ihighBkClr);BGR格式顏色值 高亮背景色
DllStructSetData($tCustDraw, "clrText", 0xFFFFFF) ;高亮後文字改為白色
EndIf
EndFunc
Func _chkboxState($hlst, $ilParam, ByRef $aDataArray);刷新核取方塊狀態
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$x = DllStructGetData($tInfo, "X")
; If ($x < 16) And (3 < $x) And (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hlst), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then;使用點擊的x座標來判斷是否在核取方塊上點擊
If $aDataArray[$iIndex] = 4096 Then
$ichked+=1
$aDataArray[$iIndex] = 8192;4096未選中 8192選中
Else
$ichked-=1
$aDataArray[$iIndex] = 4096;4096未選中 8192選中
EndIf
;====立刻刷新核取方塊顯示狀態 重繪$iIndex項
GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, $iIndex, $iIndex)
; EndIf
EndFunc
Func _SortItems($hlst, $ilParam, ByRef $aDataArray);排序
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
$iSub = DllStructGetData($tInfo, "SubItem")
_ArraySort($aDataArray, $B_DESCENDING[$iSub], 0, 0, $iSub)
GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, 0, 20)
$B_DESCENDING[$iSub] = Not $B_DESCENDING[$iSub]
EndFunc
Func _CtrlSel($hlst, $ilParam, ByRef $aDataArray);按住ctrl鍵點選item
If $bCtrlDown Then;ctrl鍵按下
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
Local $iIndex = DllStructGetData($tInfo, "Item")
Local $iNewState = DllStructGetData($tInfo, "NewState")
If $iIndex > -1 And $iNewState = BitOR($LVIS_FOCUSED, $LVIS_SELECTED) Then
If $aDataArray[$iIndex] = 4096 Then
$ichked+=1
$aDataArray[$iIndex] = 8192;4096未選中 8192選中
Else
$ichked-=1
$aDataArray[$iIndex] = 4096;4096未選中 8192選中
EndIf
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
DllStructSetData($tInfo, "State", $aDataArray[$iIndex]);設置核取方塊狀態
;====立刻刷新核取方塊顯示狀態 重繪$iIndex項
GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, $iIndex, $iIndex)
EndIf
EndIf
EndFunc
Func _LVN_GETDISPINFO($hlst, $ilParam, ByRef $aDataArray)
If Not IsArray($aDataArray) Then Return
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
$iIndex = Int(DllStructGetData($tInfo, "Item"))
$iSub = Int(DllStructGetData($tInfo, "SubItem"))
If BitAND(DllStructGetData($tInfo, "Mask"), $LVIF_IMAGE) And $iSub = 0 Then DllStructSetData($tInfo, "Image", 0);判斷是否有LVIF_IMAGE屬性,有則設置列圖像。不設置_GUICtrlListView_SetImageList則無
If (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hlst), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then
;===============設置核取方塊資訊
DllStructSetData($tInfo, "Mask", BitOR($LVIF_STATE, DllStructGetData($tInfo, "Mask")))
DllStructSetData($tInfo, "StateMask", $LVIS_STATEIMAGEMASK)
DllStructSetData($tInfo, "State", $aDataArray[$iIndex])
EndIf
DllStructSetData($tText, "Text", $aDataArray[$iIndex][$iSub]);列資料放入$tText結構
DllStructSetData($tInfo, "Text", DllStructGetPtr($tText));$tText結構的指標來設置列資料
DllStructSetData($tInfo, "TextMax", StringLen($aDataArray[$iIndex][$iSub]));設置列資料字串長
EndFunc
Func _StartItemEdit($hlst, $ilParam, ByRef $aDataArray)
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$iSub = DllStructGetData($tInfo, "SubItem")
$x = DllStructGetData($tInfo, "X")
If ($iIndex <> -1) And ($iSub >= 0) And ($x > 16) Then;點擊的x>16才回應原地編輯
$hActLst = $hlst
If _GUICtrlListView_EnsureVisible($hlst, $iIndex) Then GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, $iIndex, $iIndex + 1);確保列表視圖專案可見
Local $alstPos = ControlGetPos($hGUI, "", $hlst)
$Item = $iIndex
$SubItem = $iSub
;==========計算編輯方塊座標
Local $aRect = _GUICtrlListView_GetSubItemRect($hlst, $iIndex, $iSub)
Local $iEdit_X = $aRect + $alstPos + 4
Local $iEdit_Y = $aRect + $alstPos + 3
Local $iEdit_Width = _GUICtrlListView_GetColumnWidth($hlst, $iSub) - 1
Local $iEdit_Height = $aRect - $aRect
If $iSub = 0 Then
Local $i1 = 0, $i2 = 0
If (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hlst), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then $i1 = 17;判斷是否有核取方塊
If _GUICtrlListView_GetImageList($hlst, 1) Then $i2 = 19;判斷是否有分配圖像清單到清單視圖控制項
$iEdit_X += ($i2 + $i1)
$iEdit_Width -= ($i2 + $i1)
EndIf
Local $iEdit = GUICtrlCreateInput($aDataArray[$iIndex][$iSub], $iEdit_X, $iEdit_Y, $iEdit_Width, $iEdit_Height, BitOR($WS_CHILD, $WS_VISIBLE, $ES_LEFT))
GUICtrlSetFont($iEdit, 11)
GUICtrlSetState($iEdit, $GUI_FOCUS)
$hEdit = GUICtrlGetHandle($iEdit)
_WinAPI_BringWindowToTop($hEdit)
_GUICtrlEdit_SetSel($hEdit, 0, -1) ;全選編輯方塊文字
Local $iEnter = GUICtrlCreateDummy()
$hEnter = GUICtrlGetHandle($iEnter)
GUISetAccelerators($aAccelKeys, $hGUI);在虛擬控制項上綁定一個回車快捷按鍵
EndIf
EndFunc
Func _EndItemEdit($hlst, ByRef $aDataArray)
If ($Item < UBound($aDataArray, 1)) And ($SubItem < UBound($aDataArray, 2)) And ($hActLst == $hlst) Then
Local $sText = _GUICtrlEdit_GetText($hEdit)
_GUICtrlListView_SetItemText($hlst, $Item, $sText, $SubItem)
If IsHWnd($hEdit) Then _WinAPI_DestroyWindow($hEdit)
If IsHWnd($hEnter) Then _WinAPI_DestroyWindow($hEnter)
$hEnter = Null
$hEdit = Null
If ($Item >= 0) Then $aDataArray[$Item][$SubItem] = $sText
$Item = -1
$SubItem = 0
EndIf
EndFunc
Func _GetChkIndex($iChkState)
Local $aIndex = [-1], $j = 0
If $ichked = 0 Then Return $aIndex
If $iChkState = 8192 Then
ReDim $aIndex[$ichked]
Else
ReDim $aIndex
EndIf
For $i = 0 To UBound($aShowdata) -1
If $aShowdata[$i] == $iChkState Then
$aIndex[$j] = $i
$j+=1
EndIf
Next
Return $aIndex
EndFunc
Func __ClickListViewItem($hWnd, $iIndex, $baddX = False)
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
Local $tRECT = _GUICtrlListView_GetItemRectEx($hWnd, $iIndex, $LVIR_LABEL)
Local $tPoint = _WinAPI_PointFromRect($tRECT, True)
$tPoint = _WinAPI_ClientToScreen($hWnd, $tPoint)
Local $iX, $iY
_WinAPI_GetXYFromPoint($tPoint, $iX, $iY)
If $baddX Then $iX += DllStructGetData($tRECT, "Right")
Local $iMode = Opt("MouseCoordMode", 1)
Local $aPos = MouseGetPos()
MouseClick("left", $iX, $iY, 1, 0)
MouseMove($aPos, $aPos, 0)
Opt("MouseCoordMode", $iMode)
EndFunc
kk_lee69
发表于 2017-8-30 17:54:16
回复 46# chzj589
改好了 你看一下這個的效果
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
;#include <WinAPIShellEx.au3>
#include <GUIEdit.au3>
;#include <WinAPIEx.au3>
Opt('GUIOnEventMode', 1)
Global $idListView, $hListView, $pListViewFunc, $idAddSingleSel, $idDelSingleSel, $hListView2, $Edit1, $Inputu7, $Listview2
Global $Inputu8, $idAddSingleSela
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; Create GUI
GUICreate("ctrl+single-click --> single-click", 840, 610)
GUISetOnEvent(-3, '_Exit')
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES )
$hListView = GUICtrlGetHandle($idListView)
; GUICtrlSetBkColor($idListView, 0xCDCDCD);0xECE9D8);
; _GUICtrlListView_SetBkColor($idListView, 14675183)
_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)
$hImage = _GUIImageList_Create(1, 18);30?每行的?距(即网格高度)
_GUICtrlListView_SetImageList($idListView, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($idListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP+$LVS_EX_CHECKBOXES ))
Local $iItems = 18
For $i = 0 To $iItems - 1
GUICtrlCreateListViewItem($i & "/Column 1|" & $i & "/Column 2|" & $i & "/Column 3|" & $i & "/Column 4", $idListView)
Next
$pListViewFunc = DllCallbackGetPtr(DllCallbackRegister("ListViewFunc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hListView, $pListViewFunc, 0, 0) ; $iSubclassId = 0, $pData = 0
$idAddSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSel')
$idDelSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_DelSingleSel')
$Listview2 = GUICtrlCreateListView("", 420, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
Local $hListView2 = GUICtrlGetHandle($Listview2)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF);0xECE9D8);
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
_GUICtrlListView_InsertColumn($Listview2, 0, "id", 54)
_GUICtrlListView_InsertColumn($Listview2, 1, "Column 2", 94)
_GUICtrlListView_InsertColumn($Listview2, 2, "Column 3", 94)
_GUICtrlListView_InsertColumn($Listview2, 3, "Column 4", 94)
$hImage = _GUIImageList_Create(1, 18);30?每行的?距(即网格高度)
_GUICtrlListView_SetImageList($Listview2, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
$Edit1 = GUICtrlCreateEdit("", 10, 400, 810, 200, $WS_VSCROLL + $ES_WANTRETURN)
GUICtrlSetFont(-1, 13, 600, 0, "微?雅黑")
;GUICtrlSetColor(-1, 0x009900)
$Inputu7 = GUICtrlCreateLabel("", 680, 560, 90, 17)
GUICtrlSetState($Inputu7, $GUI_HIDE)
$Inputu8 = GUICtrlCreateLabel("", 680, 590, 90, 17)
GUICtrlSetState($Inputu8, $GUI_HIDE)
$idAddSingleSela = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSela')
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
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
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 = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSel, $aHit)
EndIf
EndIf
Return 0
Else
; If BitAND($lParam, 0xFFFF) > 16 Then
; Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($lParam, 0xFFFF), BitShift($lParam, 16))
; IF _GUICtrlListView_GetItemChecked($idListView,$aHit)=TrueThen
; _GUICtrlListView_SetItemChecked($idListView,$aHit,False)
; Else
; _GUICtrlListView_SetItemChecked($idListView,$aHit,True)
; EndIf
; GUICtrlSendMsg($hListView2, $LVM_REDRAWITEMS,$aHit, $aHit)
; EndIf
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
Else
Return
EndIf
#forceref $iSubclassId, $pData
EndFunc ;==>ListViewFunc
Func _AddSingleSel()
Local $iItem = GUICtrlRead($idAddSingleSel)
;_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_GUICtrlListView_SetItemChecked($idListView,$iItem,True)
EndFunc ;==>_AddSingleSel
Func _AddSingleSela()
Local $iItem = GUICtrlRead($idAddSingleSela)
_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_GUICtrlListView_SetItemSelected($Listview2, $iItem, True)
_GUICtrlListView_SetItemFocused($Listview2, $iItem)
;GUICtrlSetBkColor($idListView, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($idListView, 14675183)
EndFunc ;==>_AddSingleSela
Func _DelSingleSel()
Local $iItem = GUICtrlRead($idDelSingleSel)
; _GUICtrlListView_SetItemSelected($idListView, $iItem, False)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_GUICtrlListView_SetItemChecked($idListView,$iItem,False)
_DEL()
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)
Return $aRet
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
EndFunc ;==>_WinAPI_RemoveWindowSubclass
Func WM_NOTIFY($hWndGUI, $MsgID, $wParam, $ilParam)
Local $hWndFrom, $iCode
Local $tNMHDR, $iCode, $hMenu, $hWnd, $iMsg
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView;$hWndListView;?里改成你的listview的ID
Switch $iCode
Case $NM_DBLCLK
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = DllStructGetData($tInfo, "Index")
GUICtrlSetData($Inputu8, $idd)
_ListView2()
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC, $Font1
IF _GUICtrlListView_GetItemChecked($hListView,$iItem)=TrueThen
DllStructSetData($tCustDraw, "clrTextBk",0xFF901E);BGR格式顏色值 高亮背景色
DllStructSetData($tCustDraw, "clrText", 0xFFFFFF) ;高亮後文字改為白色
EndIf
Switch $iItem
Case 2;?目行?
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then ;?置首列, 即?目
$iColor = RGB2BGR(0xFF0000)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then ;?置子?, 即列索引大于0?
$iColor = RGB2BGR(0x009900)
;_WinAPI_SelectObject($hDC, $Font2) ;?置用于???景?文本的字体
DllStructSetData($tCustDraw, "clrText", $iColor) ;?置文本色
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80)) ;?置文本背景色
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF00FF);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
ElseIf $iSubItem = 3 Then
$iColor = RGB2BGR(0x000080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
EndIf
Case 4 ;?目行?
If $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF0080);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(16710321))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Local $tagNMHDR, $Event, $hWndFrom, $IDFrom, $iCode
Local $tagNMHDR = DllStructCreate("int;int;int", $ilParam)
If @error Then Return $GUI_RUNDEFMSG
$IDFrom = DllStructGetData($tagNMHDR, 2)
$Event = DllStructGetData($tagNMHDR, 3)
$tagNMHDR = 0
Switch $IDFrom;???生事件的控件
Case $hListView;$hWndListView;?里改成你的listview的ID
Case $Listview2
Switch $Event; ???生的事件
Case $NM_CLICK ; 左?
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
If $iMsg <> $WM_LBUTTONDOWN Then
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
If _GUICtrlListView_GetSelectedCount($idListView) Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($ilParam, 0xFFFF), BitShift($ilParam, 16))
If Not (@error Or $aHit = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
;GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSela, $aHit)
EndIf
EndIf
Return 0
EndIf
Case 0x0009
Return 0
EndSwitch
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
Case $NM_DBLCLK ; ??
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If Not StringLen($Index) Then; ?里用以判?是否?定了ListViewItem
MsgBox(0, "", "未?定")
Return
EndIf
; MsgBox(0, 0, _GUICtrlListView_GetItemText($ListView2, Number($Index), 0))
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
Case $NM_RCLICK ; 右?
;~ ...
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR
Func _ListView2()
GUICtrlSetData($Inputu7, "")
;$Strn = GUICtrlRead(GUICtrlRead($Listview2))
;$Strnspin = StringSplit($Strn, "|")
Local $Inpa = GUICtrlRead($Inputu8)
$cont = _GUICtrlListView_GetItemCount($hListView);取得??的行列?
$h1 = $cont + 1
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($hListView, $Inpa)
$h1 = $str
$h2 = $str
$h3 = $str
$h4 = $str
Next
;$num1 = $cont + 1
$Number = _GUICtrlListView_GetItemCount($Listview2)
$num1 = $Number + 1
_GUICtrlListView_AddItem($Listview2, $num1, $Number)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h2, 1)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h3, 2)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h4, 3)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
;_GUICtrlEdit_AppendText($Edit,"??:---------------------------------" & @CRLF)
_GUICtrlEdit_AppendText($Edit1, "" & $h1 & "," & $h2 & "," & $h3 & "," & $h4 & @CRLF)
EndFunc ;==>_ListView2
Func _DEL();?除
Local $Inpa = GUICtrlRead($Inputu7) - 1
GUICtrlSetData($Inputu7, $Inpa)
_GUICtrlEdit_ReplaceLine($Edit1, '[\r\n]+$', '', 0, 1)
_GUICtrlEdit_ReplaceLine($Edit1, $Inpa)
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If $Index <> "" Then
;$msg = MsgBox(49, "?除??", "确定要?除???")
;If $msg == 1 Then
$DEL = (_GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Listview2)))
;Else
;EndIf
Else
;MsgBox(0, "提示", "????目", 1)
EndIf
GUICtrlSetData($Inputu7, "")
GUICtrlSetData($Inputu8, "")
EndFunc ;==>_DEL
;==========================================================================================
; ?明: 按指定的??字或行?替?或?除Edit??框行字符,可??是否替?整行、是否使用正?表?式。
; ?法: _GUICtrlEdit_ReplaceLine('控件句柄', '搜索字符或'行?[, '替?'[, 替?行?志[, 正??志]]])
; ??: '搜索字符串或'行?: 如果使用行?(0基)?不要用引?,否??作?字符串搜索;
; 使用 -1 ?匹配最后一行
; '替?'[可?]: 如'替?'字符?空(默?)???除。
; 替?行?志[可?]: ?志 = 1 :包含??字的整行被替?;?志 = 0 ?替?搜索字符(默?)。
; 如果搜索???行?,?此???效(整行被替?,等同于此???1)
; 正??志[可?]: 正??志 = 1 使用正?,? 0 不使用正?(默?),使用原始字符串匹配。
; 返回值:成功: 返回1;
; 失?: 返回0,并?置@Error?以下值:
; @Error = 1 ??框?空
; @Error = 2 ??文本?量超出限制(注:可用 GUICtrlSetLimit() ?置)
; @Error = 3 搜索的行?超出?有?行?范?
; @Error = 4 未搜索到匹配字符串
; 作者: Afan -- http://www.autoitx.com
;=====================================================================================start
Func _GUICtrlEdit_ReplaceLine($hWnd, $pattern, $replace = '', $flag = 0, $SRE = 0)
Local $Str_all = GUICtrlRead($hWnd)
If $Str_all = '' Then Return SetError(1, 0, 0)
If _GUICtrlEdit_GetLimitText($hWnd) < StringLen($Str_all) Then Return SetError(2, 0, 0)
Local $Index, $nLine, $strZY, $sR, $iStart, $iEnd, $str, $Len, $extended
If IsNumber($pattern) Then
$Index = _GUICtrlEdit_GetLineCount($hWnd)
If $pattern = -1 Then
$nLine = $Index - 1
Else
$nLine = $pattern
If $nLine > $Index - 1 Then Return SetError(3, 0, 0)
EndIf
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
If $SRE = 0 Then
$strZY = '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)'
$pattern = StringRegExpReplace($pattern, $strZY, '\\$1')
$replace = StringRegExpReplace($replace, $strZY, '\\$1')
EndIf
$sR = StringRegExp($Str_all, $pattern, 1)
If @error Then Return SetError(4, 0, 0)
$extended = @extended
If $flag = 1 Or $flag = -1 Then
$nLine = _GUICtrlEdit_LineFromChar($hWnd, $extended - 1)
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
$iEnd = $extended - 1
$str = $sR
$Len = StringLen($str)
$iStart = $iEnd - $Len
EndIf
EndIf
_GUICtrlEdit_SetSel($hWnd, $iStart, $iEnd)
GUICtrlSetData($hWnd, $replace, 1)
Return 1
EndFunc ;==>_GUICtrlEdit_ReplaceLine
chzj589
发表于 2017-8-30 18:18:18
回复 49# kk_lee69
谢谢!测试后:
第一行颜色还不行,
点击后,再点击不能返回原来颜色
kk_lee69
发表于 2017-8-30 20:55:55
回复 50# chzj589
這樣就可以了不過 你使用 _GUICtrlListView_GetSelectedIndices 這樣是讀取不到資料的喔
要改用 _GUICtrlListView_GetItemChecked
只改了 LISTVIEW1
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
;#include <WinAPIShellEx.au3>
#include <GUIEdit.au3>
;#include <WinAPIEx.au3>
Opt('GUIOnEventMode', 1)
Global $idListView, $hListView, $pListViewFunc, $idAddSingleSel, $idDelSingleSel, $hListView2, $Edit1, $Inputu7, $Listview2
Global $Inputu8, $idAddSingleSela
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; Create GUI
GUICreate("ctrl+single-click --> single-click", 840, 610)
GUISetOnEvent(-3, '_Exit')
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES )
$hListView = GUICtrlGetHandle($idListView)
; GUICtrlSetBkColor($idListView, 0xCDCDCD);0xECE9D8);
; _GUICtrlListView_SetBkColor($idListView, 14675183)
_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)
$hImage = _GUIImageList_Create(1, 18);30?每行的?距(即网格高度)
_GUICtrlListView_SetImageList($idListView, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($idListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP+$LVS_EX_CHECKBOXES ))
Local $iItems = 18
For $i = 0 To $iItems - 1
GUICtrlCreateListViewItem($i & "/Column 1|" & $i & "/Column 2|" & $i & "/Column 3|" & $i & "/Column 4", $idListView)
Next
$pListViewFunc = DllCallbackGetPtr(DllCallbackRegister("ListViewFunc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hListView, $pListViewFunc, 0, 0) ; $iSubclassId = 0, $pData = 0
$idAddSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSel')
$idDelSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_DelSingleSel')
$Listview2 = GUICtrlCreateListView("", 420, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
Local $hListView2 = GUICtrlGetHandle($Listview2)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF);0xECE9D8);
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
_GUICtrlListView_InsertColumn($Listview2, 0, "id", 54)
_GUICtrlListView_InsertColumn($Listview2, 1, "Column 2", 94)
_GUICtrlListView_InsertColumn($Listview2, 2, "Column 3", 94)
_GUICtrlListView_InsertColumn($Listview2, 3, "Column 4", 94)
$hImage = _GUIImageList_Create(1, 18);30?每行的?距(即网格高度)
_GUICtrlListView_SetImageList($Listview2, $hImage, 1)
_GUICtrlListView_SetExtendedListViewStyle($Listview2, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
$Edit1 = GUICtrlCreateEdit("", 10, 400, 810, 200, $WS_VSCROLL + $ES_WANTRETURN)
GUICtrlSetFont(-1, 13, 600, 0, "微?雅黑")
;GUICtrlSetColor(-1, 0x009900)
$Inputu7 = GUICtrlCreateLabel("", 680, 560, 90, 17)
GUICtrlSetState($Inputu7, $GUI_HIDE)
$Inputu8 = GUICtrlCreateLabel("", 680, 590, 90, 17)
GUICtrlSetState($Inputu8, $GUI_HIDE)
$idAddSingleSela = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSela')
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
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
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 = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSel, $aHit)
EndIf
EndIf
Return 0
Else
If BitAND($lParam, 0xFFFF) > 16 Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($lParam, 0xFFFF), BitShift($lParam, 16))
IF _GUICtrlListView_GetItemChecked($idListView,$aHit)=TrueThen
_GUICtrlListView_SetItemChecked($idListView,$aHit,False)
Else
_GUICtrlListView_SetItemChecked($idListView,$aHit,True)
EndIf
GUICtrlSendMsg($idListView, $LVM_REDRAWITEMS,$aHit, $aHit)
EndIf
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
Else
Return
EndIf
#forceref $iSubclassId, $pData
EndFunc ;==>ListViewFunc
Func _AddSingleSel()
Local $iItem = GUICtrlRead($idAddSingleSel)
; _GUICtrlListView_SetItemSelected($idListView, -1, False,True)
_GUICtrlListView_SetItemChecked($idListView,$iItem,True)
; _GUICtrlListView_SetItemFocused($idListView, $iItem)
EndFunc ;==>_AddSingleSel
Func _AddSingleSela()
Local $iItem = GUICtrlRead($idAddSingleSela)
_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
_GUICtrlListView_SetItemSelected($Listview2, $iItem, True)
_GUICtrlListView_SetItemFocused($Listview2, $iItem)
;GUICtrlSetBkColor($idListView, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($idListView, 14675183)
EndFunc ;==>_AddSingleSela
Func _DelSingleSel()
Local $iItem = GUICtrlRead($idDelSingleSel)
;_GUICtrlListView_SetItemSelected($idListView, -1, False,True)
_GUICtrlListView_SetItemChecked($idListView,$iItem,False)
; _GUICtrlListView_SetItemFocused($idListView, $iItem)
_DEL()
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)
Return $aRet
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
EndFunc ;==>_WinAPI_RemoveWindowSubclass
Func WM_NOTIFY($hWndGUI, $MsgID, $wParam, $ilParam)
Local $hWndFrom, $iCode
Local $tNMHDR, $iCode, $hMenu, $hWnd, $iMsg
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView;$hWndListView;?里改成你的listview的ID
Switch $iCode
Case$NM_CLICK
_GUICtrlListView_SetItemSelected($idListView, -1, False)
Case $NM_DBLCLK
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = DllStructGetData($tInfo, "Index")
GUICtrlSetData($Inputu8, $idd)
_ListView2()
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC, $Font1
IF _GUICtrlListView_GetItemChecked($hListView,$iItem)=TrueThen
DllStructSetData($tCustDraw, "clrTextBk",0xFF901E);BGR格式顏色值 高亮背景色
DllStructSetData($tCustDraw, "clrText", 0xFFFFFF) ;高亮後文字改為白色
; GUICtrlSendMsg($idListView, $LVM_REDRAWITEMS,$iItem, $iItem)
EndIf
Switch $iItem
Case 2;?目行?
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then ;?置首列, 即?目
$iColor = RGB2BGR(0xFF0000)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then ;?置子?, 即列索引大于0?
$iColor = RGB2BGR(0x009900)
;_WinAPI_SelectObject($hDC, $Font2) ;?置用于???景?文本的字体
DllStructSetData($tCustDraw, "clrText", $iColor) ;?置文本色
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80)) ;?置文本背景色
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF00FF);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
ElseIf $iSubItem = 3 Then
$iColor = RGB2BGR(0x000080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
EndIf
Case 4 ;?目行?
If $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF0080);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(16710321))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Local $tagNMHDR, $Event, $hWndFrom, $IDFrom, $iCode
Local $tagNMHDR = DllStructCreate("int;int;int", $ilParam)
If @error Then Return $GUI_RUNDEFMSG
$IDFrom = DllStructGetData($tagNMHDR, 2)
$Event = DllStructGetData($tagNMHDR, 3)
$tagNMHDR = 0
Switch $IDFrom;???生事件的控件
Case $hListView;$hWndListView;?里改成你的listview的ID
Case $Listview2
Switch $Event; ???生的事件
Case $NM_CLICK ; 左?
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
If $iMsg <> $WM_LBUTTONDOWN Then
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
If _GUICtrlListView_GetSelectedCount($idListView) Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($ilParam, 0xFFFF), BitShift($ilParam, 16))
If Not (@error Or $aHit = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit) Then
;GUICtrlSendToDummy($idDelSingleSel, $aHit)
Else
GUICtrlSendToDummy($idAddSingleSela, $aHit)
EndIf
EndIf
Return 0
EndIf
Case 0x0009
Return 0
EndSwitch
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "ilparam", $ilParam)
If Not @error Then
Return $result
Else
Return
EndIf
Case $NM_DBLCLK ; ??
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If Not StringLen($Index) Then; ?里用以判?是否?定了ListViewItem
MsgBox(0, "", "未?定")
Return
EndIf
; MsgBox(0, 0, _GUICtrlListView_GetItemText($ListView2, Number($Index), 0))
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $idd = _GUICtrlListView_GetItemText($Listview2, Number($Index), 0);DllStructGetData($tInfo, "Index")
;MsgBox(48, '提示', "小苹果提示"&$idd)
GUICtrlSetData($Inputu7, $idd)
Case $NM_RCLICK ; 右?
;~ ...
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR
Func _ListView2()
GUICtrlSetData($Inputu7, "")
;$Strn = GUICtrlRead(GUICtrlRead($Listview2))
;$Strnspin = StringSplit($Strn, "|")
Local $Inpa = GUICtrlRead($Inputu8)
$cont = _GUICtrlListView_GetItemCount($hListView);取得??的行列?
$h1 = $cont + 1
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($hListView, $Inpa)
$h1 = $str
$h2 = $str
$h3 = $str
$h4 = $str
Next
;$num1 = $cont + 1
$Number = _GUICtrlListView_GetItemCount($Listview2)
$num1 = $Number + 1
_GUICtrlListView_AddItem($Listview2, $num1, $Number)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h2, 1)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h3, 2)
_GUICtrlListView_AddSubItem($Listview2, $Number, $h4, 3)
;GUICtrlSetBkColor($Listview2, 0xC0E0FF)
;_GUICtrlListView_SetBkColor($Listview2, 14675183)
;_GUICtrlEdit_AppendText($Edit,"??:---------------------------------" & @CRLF)
_GUICtrlEdit_AppendText($Edit1, "" & $h1 & "," & $h2 & "," & $h3 & "," & $h4 & @CRLF)
EndFunc ;==>_ListView2
Func _DEL();?除
Local $Inpa = GUICtrlRead($Inputu7) - 1
GUICtrlSetData($Inputu7, $Inpa)
_GUICtrlEdit_ReplaceLine($Edit1, '[\r\n]+$', '', 0, 1)
_GUICtrlEdit_ReplaceLine($Edit1, $Inpa)
$Index = _GUICtrlListView_GetSelectedIndices($Listview2)
If $Index <> "" Then
;$msg = MsgBox(49, "?除??", "确定要?除???")
;If $msg == 1 Then
$DEL = (_GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Listview2)))
;Else
;EndIf
Else
;MsgBox(0, "提示", "????目", 1)
EndIf
GUICtrlSetData($Inputu7, "")
GUICtrlSetData($Inputu8, "")
EndFunc ;==>_DEL
;==========================================================================================
; ?明: 按指定的??字或行?替?或?除Edit??框行字符,可??是否替?整行、是否使用正?表?式。
; ?法: _GUICtrlEdit_ReplaceLine('控件句柄', '搜索字符或'行?[, '替?'[, 替?行?志[, 正??志]]])
; ??: '搜索字符串或'行?: 如果使用行?(0基)?不要用引?,否??作?字符串搜索;
; 使用 -1 ?匹配最后一行
; '替?'[可?]: 如'替?'字符?空(默?)???除。
; 替?行?志[可?]: ?志 = 1 :包含??字的整行被替?;?志 = 0 ?替?搜索字符(默?)。
; 如果搜索???行?,?此???效(整行被替?,等同于此???1)
; 正??志[可?]: 正??志 = 1 使用正?,? 0 不使用正?(默?),使用原始字符串匹配。
; 返回值:成功: 返回1;
; 失?: 返回0,并?置@Error?以下值:
; @Error = 1 ??框?空
; @Error = 2 ??文本?量超出限制(注:可用 GUICtrlSetLimit() ?置)
; @Error = 3 搜索的行?超出?有?行?范?
; @Error = 4 未搜索到匹配字符串
; 作者: Afan -- http://www.autoitx.com
;=====================================================================================start
Func _GUICtrlEdit_ReplaceLine($hWnd, $pattern, $replace = '', $flag = 0, $SRE = 0)
Local $Str_all = GUICtrlRead($hWnd)
If $Str_all = '' Then Return SetError(1, 0, 0)
If _GUICtrlEdit_GetLimitText($hWnd) < StringLen($Str_all) Then Return SetError(2, 0, 0)
Local $Index, $nLine, $strZY, $sR, $iStart, $iEnd, $str, $Len, $extended
If IsNumber($pattern) Then
$Index = _GUICtrlEdit_GetLineCount($hWnd)
If $pattern = -1 Then
$nLine = $Index - 1
Else
$nLine = $pattern
If $nLine > $Index - 1 Then Return SetError(3, 0, 0)
EndIf
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
If $SRE = 0 Then
$strZY = '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)'
$pattern = StringRegExpReplace($pattern, $strZY, '\\$1')
$replace = StringRegExpReplace($replace, $strZY, '\\$1')
EndIf
$sR = StringRegExp($Str_all, $pattern, 1)
If @error Then Return SetError(4, 0, 0)
$extended = @extended
If $flag = 1 Or $flag = -1 Then
$nLine = _GUICtrlEdit_LineFromChar($hWnd, $extended - 1)
$Len = StringLen(_GUICtrlEdit_GetLine($hWnd, $nLine))
$iStart = _GUICtrlEdit_LineIndex($hWnd, $nLine)
$iEnd = $iStart + $Len
Else
$iEnd = $extended - 1
$str = $sR
$Len = StringLen($str)
$iStart = $iEnd - $Len
EndIf
EndIf
_GUICtrlEdit_SetSel($hWnd, $iStart, $iEnd)
GUICtrlSetData($hWnd, $replace, 1)
Return 1
EndFunc ;==>_GUICtrlEdit_ReplaceLine
kk_lee69
发表于 2017-8-31 01:09:08
回复 46# chzj589
如果你不介意 使用checked 來判斷其實下面的寫法更簡單....不需要使用 回調函數....
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
;#include <WinAPIShellEx.au3>
#include <GUIEdit.au3>
;#include <WinAPIEx.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", 840, 610)
GUISetOnEvent(-3, '_Exit')
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE+$LVS_EX_CHECKBOXES)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES)
$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
$Listview2 = GUICtrlCreateListView("", 420, 10, 400, 380, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
; Show GUI
GUISetState(@SW_SHOW)
EndFunc ;==>Example
Func _Exit()
; _WinAPI_RemoveWindowSubclass($hListView, $pListViewFunc, 0)
GUIDelete()
Exit
EndFunc ;==>_Exit
Func WM_NOTIFY($hWndGUI, $MsgID, $wParam, $ilParam)
Local $hWndFrom, $iCode
Local $tNMHDR, $iCode, $hMenu, $hWnd, $iMsg
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView;$hWndListView;?里改成你的listview的ID
Switch $iCode
Case$NM_CLICK
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$iSub = DllStructGetData($tInfo, "SubItem")
$aPOSLx = DllStructGetData($tInfo, "X")
If ($aPOSLx >=16) OR ($aPOSLx <=3 ) Then
IF _GUICtrlListView_GetItemChecked($hListView,$iIndex)=TrueThen
_GUICtrlListView_SetItemChecked($idListView,$iIndex,False)
Else
_GUICtrlListView_SetItemChecked($idListView,$iIndex,True)
EndIf
EndIf
_GUICtrlListView_SetItemSelected($idListView, -1, False)
Case $NM_RCLICK ; 右擊
_GUICtrlListView_SetItemSelected($idListView, -1, False)
Case $NM_DBLCLK
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC, $Font1
IF _GUICtrlListView_GetItemChecked($hListView,$iItem)=TrueThen
DllStructSetData($tCustDraw, "clrTextBk",0xFF901E);BGR格式顏色值 高亮背景色
DllStructSetData($tCustDraw, "clrText", 0xFFFFFF) ;高亮後文字改為白色
; GUICtrlSendMsg($idListView, $LVM_REDRAWITEMS,$iItem, $iItem)
EndIf
Switch $iItem
Case 2;?目行?
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then ;?置首列, 即?目
$iColor = RGB2BGR(0xFF0000)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then ;?置子?, 即列索引大于0?
$iColor = RGB2BGR(0x009900)
;_WinAPI_SelectObject($hDC, $Font2) ;?置用于???景?文本的字体
DllStructSetData($tCustDraw, "clrText", $iColor) ;?置文本色
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80)) ;?置文本背景色
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF00FF);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
ElseIf $iSubItem = 3 Then
$iColor = RGB2BGR(0x000080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
;DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xC0C0C0))
EndIf
Case 4 ;?目行?
If $iSubItem = 2 Then
$iColor = RGB2BGR(0xFF0080);0x008080)
;_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(16710321))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR
chzj589
发表于 2017-8-31 09:19:52
回复 52# kk_lee69
早上测试,通过了。谢谢
chzj589
发表于 2017-8-31 12:04:40
回复 52# kk_lee69
测试效果,好玩
yamakawa
发表于 2017-8-31 16:38:10
本帖最后由 yamakawa 于 2017-8-31 17:12 编辑
我也来玩玩{:face (356):}
随便弄的,,见笑见笑
闲的无聊,随手改了下。。。
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
;#include <WinAPITheme.au3>
#include <GuiListView.au3>
#include <WinAPISys.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <array.au3>
#include <Misc.au3>
;----------
Global $hGui, $lListView, $rListView
Global $test,$copy,$del
Global $iSelItem
;-----------
$hGui = GUICreate("Test for multiCheck listview", 850, 350, -1, -1)
;=======左边列表
$lListView = GUICtrlCreateListView("", 5, 10, 380, 280)
_GUICtrlListView_SetExtendedListViewStyle($lListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
_GUICtrlListView_AddColumn($lListView, "列一", 85, 2)
_GUICtrlListView_AddColumn($lListView, "列二", 85, 2)
_GUICtrlListView_AddColumn($lListView, "列三", 85, 2)
_GUICtrlListView_AddColumn($lListView, "列四", 85, 2)
For $i = 0 To 99
_GUICtrlListView_AddItem($lListView, "行" & $i + 1 & "列 1")
For $ii = 1 To 3
_GUICtrlListView_AddSubItem($lListView, $i, "行" & $i + 1 & "列 " & $ii + 1, $ii)
Next
Next
;======右边列表
$rListView = GUICtrlCreateListView("", 465, 10, 380, 280)
_GUICtrlListView_SetExtendedListViewStyle($rListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
_GUICtrlListView_AddColumn($rListView, "列1", 85, 2)
_GUICtrlListView_AddColumn($rListView, "列2", 85, 2)
_GUICtrlListView_AddColumn($rListView, "列3", 85, 2)
_GUICtrlListView_AddColumn($rListView, "列4", 85, 2)
;============================加个按钮,测试用)
$test = GUICtrlCreateButton(" test ", 20, 300)
$del = GUICtrlCreateButton(" Del ",465,300)
$copy = GUICtrlCreateButton("Copy >>",400,150)
;=======随机复选几个
For $i = 0 To 9
If Not _GUICtrlListView_GetItemChecked($lListView, $i) Then _GUICtrlListView_SetItemChecked($lListView, Random(0, 12, 1))
Next
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
While 1
Switch GUIGetMsg()
Case -3
Exit
Case $del
_GUICtrlListView_DeleteAllItems($rListView)
Case $copy
_GUICtrlListView_BeginUpdate($rListView)
_GUICtrlListView_DeleteAllItems($rListView)
For $i = 0 To 99
If _GUICtrlListView_GetItemChecked($lListView, $i) Then GUICtrlCreateListViewItem( _GUICtrlListView_GetItemTextString($lListView, $i) ,$rListView)
Next
_GUICtrlListView_EndUpdate($rListView)
Case $test
ConsoleWrite("!=====当前选中项=====" & @CRLF)
For $i = 0 To 99
If _GUICtrlListView_GetItemChecked($lListView, $i) Then ConsoleWrite("-----------" & $i + 1 & @CRLF)
Next
ConsoleWrite("+=====================" & @CRLF)
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_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $Index = DllStructGetData($tInfo, "Index"), _
$SubItem = DllStructGetData($tInfo, "SubItem"), _
$X = DllStructGetData($tInfo, "X")
_GUICtrlListView_RedrawItems($hWndFrom, $Index, $Index)
Case -150, -177 ;虚拟列表用。。。显示列表
Case $NM_CUSTOMDRAW
Local $tNMLVCUSTOMDRAW = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $dwDrawStage = DllStructGetData($tNMLVCUSTOMDRAW, "dwDrawStage")
Switch $dwDrawStage
Case $CDDS_PREPAINT
Return $CDRF_NOTIFYITEMDRAW
Case $CDDS_ITEMPREPAINT
Return $CDRF_NOTIFYSUBITEMDRAW
Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
Local $iItem = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec")
Local $iSubitem = DllStructGetData($tNMLVCUSTOMDRAW, "iSubitem")
Local $hDC = DllStructGetData($tNMLVCUSTOMDRAW, 'hdc')
Switch $iItem
Case 0 To 9
$iColor1 = RGB2BGR(0xFBFFD8)
$iColor2 = RGB2BGR(-1)
$iColor3 = RGB2BGR(0xFF0000)
If Mod($iSubitem, 2) Then
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor1)
DllStructSetData($tNMLVCUSTOMDRAW, 'clrText', 0)
Else
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor2)
DllStructSetData($tNMLVCUSTOMDRAW, 'clrText', $iColor3)
EndIf
Case 10 To 19
$iColor1 = RGB2BGR(0xFBFFD8)
$iColor2 = RGB2BGR(0x3DF8FF)
If Mod($iItem, 2) Then
If Mod($iSubitem, 2) Then
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor1)
Else
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor2)
EndIf
Else
If Mod($iSubitem, 2) Then
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor2)
Else
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor1)
EndIf
EndIf
Case 20 To 29
$iColor1 = RGB2BGR(0xFBFFD8)
$iColor2 = RGB2BGR(-1)
If Mod($iItem, 2) Then
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor2)
Else
DllStructSetData($tNMLVCUSTOMDRAW, 'clrTextBk', $iColor1)
EndIf
EndSwitch
If _GUICtrlListView_GetItemChecked($lListView, $iItem) Then $tNMLVCUSTOMDRAW.ClrTextbk = 0xff00cc
EndSwitch
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED
Local $tNMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $ilParam)
If Not _GUICtrlListView_GetItemChecked($lListView, DllStructGetData($tNMLISTVIEW, "Item")) Then
_GUICtrlListView_SetItemChecked($lListView, DllStructGetData($tNMLISTVIEW, "Item"))
Else
_GUICtrlListView_SetItemChecked($lListView, DllStructGetData($tNMLISTVIEW, "Item"), False)
EndIf
$iSelItem = DllStructGetData($tNMLISTVIEW, "Item")
_GUICtrlListView_SetItemSelected($hWndFrom, $iSelItem, False)
_GUICtrlListView_SetItemState($hWndFrom, $iSelItem, 0, $LVIS_FOCUSED)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return StringRegExpReplace($iColor, "0x(..)(..)(..)", "0x$3$2$1")
EndFunc ;==>RGB2BGR
kk_lee69
发表于 2017-8-31 23:58:55
回复 55# yamakawa
懂VB 嗎??
http://www.vbforums.com/showthread.php?832123-RESOLVED-Listview-non-focus-selected-backcolor
可否看一下這篇上面同樣有 失去焦點後會變灰色的疑問有人通過
WM_KILLFOCUS
可阻止變色可惜那是VB我只看得懂 AU3
kk_lee69
发表于 2017-9-1 00:37:31
回复 55# yamakawa
這位大哥有件事情 還得請您幫幫忙 !!!
根據chzj589 的希望 他其實是 希望 失去焦點後 不要 變成灰色
而根據我的了解我給了他一個解決方案我使用的是 逃避 SELECTED 的狀態
然後 自繪背景色的方法.....
但是 我的另外一篇 有提到 改變選取的顏色方法我測試過了 選取的顏色 好像也不會變灰色
如果我們把兩者合併.................
不逃避 SELECTED 狀態而直接改變 SECECTED 的顏色.....
這樣是否 也可以達到 不要變成灰色呢??
yamakawa
发表于 2017-9-1 08:47:11
回复 56# kk_lee69
{:face (394):}你给的那个链接,也是用的控件子类化技术。。俺还没吃透。。。试试吧。。。要耐心哦{:face (356):}俺也不知道能不能搞定
chzj589
发表于 2017-9-1 09:13:49
回复 58# yamakawa
55楼的例子里" Del ",能全部删除列表2的数据。
我是:
左键双击第一列表框条目,选择的条目添加显示在第二列表,选择的数据显示在编辑框,
左键单击第二列表框条目,右键单击第一列表框对应条目,删除第二列表框选择条目与对应编辑框数据。
请教,
能不用左键单击第二列表框条目,直接右键单击第一列表框对应条目,就能删除第二列表框选择条目吗?
chzj589
发表于 2017-9-1 09:28:19
回复 59# chzj589
刚才问题没说清楚。
能不用左键单击第二列表框条目,直接右键单击第一列表框选择高亮条目,就能删除第二列表框对应条目吗?