回复 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[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
Else
If BitAND($lParam, 0xFFFF) > 16 Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($lParam, 0xFFFF), BitShift($lParam, 16))
IF _GUICtrlListView_GetItemChecked($idListView,$aHit[0])=True Then
_GUICtrlListView_SetItemChecked($idListView,$aHit[0],False)
Else
_GUICtrlListView_SetItemChecked($idListView,$aHit[0],True)
EndIf
GUICtrlSendMsg($idListView, $LVM_REDRAWITEMS,$aHit[0], $aHit[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[0]
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[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
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)=True Then
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[0]
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[0] = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit[0]) Then
;GUICtrlSendToDummy($idDelSingleSel, $aHit[0])
Else
GUICtrlSendToDummy($idAddSingleSela, $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, "ilparam", $ilParam)
If Not @error Then
Return $result[0]
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[1]
$h2 = $str[2]
$h3 = $str[3]
$h4 = $str[4]
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[0]
$Len = StringLen($str)
$iStart = $iEnd - $Len
EndIf
EndIf
_GUICtrlEdit_SetSel($hWnd, $iStart, $iEnd)
GUICtrlSetData($hWnd, $replace, 1)
Return 1
EndFunc ;==>_GUICtrlEdit_ReplaceLine
|