#include <GuiListView.au3>
#include <Editconstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
;2011.11.09 by bbs.wglm.net 小A 修改至:yhxhappy 2012.11.22由qinylj修改
;[修改] 删掉了原来的Enter键,仅仅换成Input输入,但是这是目前我比较满意的一个Listview原地编辑例子,字体正常,支持右键复制粘贴
Global $ListView_HitItem, $Listview_HitItemIndex, $Listview_HitItemSubItem, $ListView_HitItemInput;定义全局变量
$Form = GUICreate("LIST原位编辑支持子项 by 小A&修改by qinylj,感谢蛋蛋大大的帮助", 1166, 709)
GUISetFont(9, 400, 0, "微软雅黑")
$ListView = GUICtrlCreateListView("1|2|3|4|5|6|7", 8, 8, 1153, 697)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$label = GUICtrlCreateLabel("", -100, -100, 1, 1)
GUISetState(@SW_SHOW)
For $i = 0 To 9
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, $i, 150)
Next
For $i = 0 To 120
GUICtrlCreateListViewItem("Au3中文" & $i & "|论坛" & $i & "|感谢蛋蛋大大netegg" & $i & "|BY" & $i & "|小A" & $i & "|修改By" & $i & "|Qinylj" & $i, $ListView)
Next
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam, $ilParam
Local $tNMHDR, $hWndFrom, $iCode
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = DllStructGetData($tNMHDR, 'hWndFrom')
$iCode = DllStructGetData($tNMHDR, 'Code')
Switch $hWndFrom
Case GUICtrlGetHandle($ListView)
Switch $iCode
Case $NM_DBLCLK;双击消息
$ListView_HitItem = _GUICtrlListView_SubItemHitTest(GUICtrlGetHandle($ListView));确定哪些项目或子项目在控件的给定位置,参见帮助文件
$Listview_HitItemIndex = $ListView_HitItem[0];获取点击项目的0基索引
$Listview_HitItemSubItem = $ListView_HitItem[1];获取点击项目的子项索引
If $Listview_HitItemIndex = -1 Then Return
$ListView_HitItemPos = ControlGetPos($Form, "", $ListView);获取Listview坐标
$ListView_HitItemRect = _GUICtrlListView_GetSubItemRect($ListView, $ListView_HitItem[0], $ListView_HitItem[1]);获取点击项的边界矩形
$ListView_HitItemText = _GUICtrlListView_GetItemText($ListView, $ListView_HitItem[0], $ListView_HitItem[1]) ;读取所点击单元格的文本
$ListView_HitItemInput = GUICtrlCreateInput($ListView_HitItemText, _ ;创建一个与单元格差不多大小的输入框 ;$ListView_HitItemText为在Input控件中显示的文本
$ListView_HitItemRect[0] + $ListView_HitItemPos[0] + 4, _;Input控件距离左侧的值,即X坐标,由点击项目的X坐标加上Listview的X坐标再加上4(4为微调值,保证X坐标吻合)
$ListView_HitItemRect[1] + $ListView_HitItemPos[1] + 2, _;Input控件距离上方的值,即Y坐标,由点击项目的Y坐标加上Listview的Y坐标再加上2(2为微调值,保证Y坐标吻合)
_GUICtrlListView_GetColumnWidth($ListView, $ListView_HitItem[1]) - 2, _;Input控件的宽度
$ListView_HitItemRect[3] - $ListView_HitItemRect[1]);Input控件的高度
GUICtrlSetState(-1, $GUI_ONTOP + $GUI_FOCUS) ;使输入框具有焦点及置顶
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
Func _WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam, $ilParam
Local $hWndFrom, $iIDFrom, $iCode
$hWndFrom = $ilParam
$iCode = BitShift($iwParam, 16);获取高位
$iIDFrom = BitAND($iwParam, 0x0000FFFF);获取低位
Switch $hWndFrom
Case GUICtrlGetHandle($ListView_HitItemInput)
Switch $iCode
Case $EN_KILLFOCUS ; 控件丢失键盘焦点时发送
_GUICtrlListView_SetItemText($ListView, $Listview_HitItemIndex, GUICtrlRead($ListView_HitItemInput), $Listview_HitItemSubItem) ;更改子项文本,$Listview_HitItemIndex项目索引,$Listview_HitItemSubItem子项的索引
GUICtrlDelete($ListView_HitItemInput);删除Input控件
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND