[已解决]Listview就地编辑,两个编辑框就有问题了
本帖最后由 qinylj 于 2012-11-24 13:20 编辑#include <GuiListView.au3>
#include <Editconstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
;2011.11.09 by bbs.wglm.net 小A修改至:yhxhappy2012.11.22由qinylj修改
;[修改] 删掉了原来的Enter键,仅仅换成Input输入,但是这是目前我比较满意的一个Listview原地编辑例子,字体正常,支持右键复制
Global $ListView_HitItem, $Listview_HitItemIndex, $Listview_HitItemSubItem, $ListView_HitItemInput;定义全局变量粘贴
Global $ListView1_HitItem, $ListView1_HitItemIndex, $ListView1_HitItemSubItem, $ListView1_HitItemInput;定义全局变量
$Form = GUICreate("LIST原位编辑支持子项 by 小A&修改by qinylj,感谢蛋蛋大大的帮助", 1200, 708)
GUISetFont(9, 400, 0, "微软雅黑")
$ListView = GUICtrlCreateListView("1|2|3|4", 8, 8, 592, 700)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$ListView1 = GUICtrlCreateListView("1|2|3|4", 600, 8, 592, 700)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$label = GUICtrlCreateLabel("", -100, -100, 1, 1)
GUISetState(@SW_SHOW)
For $i = 0 To 3
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, $i, 135)
GUICtrlSendMsg($ListView1, $LVM_SETCOLUMNWIDTH, $i, 130)
Next
For $i = 0 To 120
GUICtrlCreateListViewItem("Au3中文论坛啊,呵呵" & $i & "|Autoit中文论坛" & $i & "|感谢蛋蛋大大netegg" & $i & "|还要感谢小A" & $i, $ListView)
GUICtrlCreateListViewItem("Qinylj啊,哈哈" & $i & "|BY Qinylj我晕哦" & $i & "|小A是哪个?" & $i & "|修改By啊Qinylj" & $i, $ListView1)
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)
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基索引
$Listview_HitItemSubItem = $ListView_HitItem;获取点击项目的子项索引
If $Listview_HitItemIndex = -1 Then Return
$ListView_HitItemPos = ControlGetPos($Form, "", $ListView);获取Listview坐标
$ListView_HitItemRect = _GUICtrlListView_GetSubItemRect($ListView, $ListView_HitItem, $ListView_HitItem);获取点击项的边界矩形
$ListView_HitItemText = _GUICtrlListView_GetItemText($ListView, $ListView_HitItem, $ListView_HitItem) ;读取所点击单元格的文本
$ListView_HitItemInput = GUICtrlCreateInput($ListView_HitItemText, _ ;创建一个与单元格差不多大小的输入框 ;$ListView_HitItemText为在Input控件中显示的文本
$ListView_HitItemRect + $ListView_HitItemPos + 4, _;Input控件距离左侧的值,即X坐标,由点击项目的X坐标加上Listview的X坐标再加上4(4为微调值,保证X坐标吻合)
$ListView_HitItemRect + $ListView_HitItemPos + 2, _;Input控件距离上方的值,即Y坐标,由点击项目的Y坐标加上Listview的Y坐标再加上2(2为微调值,保证Y坐标吻合)
_GUICtrlListView_GetColumnWidth($ListView, $ListView_HitItem) - 2, _;Input控件的宽度
$ListView_HitItemRect - $ListView_HitItemRect);Input控件的高度
GUICtrlSetState(-1, $GUI_ONTOP + $GUI_FOCUS) ;使输入框具有焦点及置顶
EndSwitch
Case GUICtrlGetHandle($ListView1)
Switch $iCode
Case $NM_DBLCLK;双击消息
$ListView1_HitItem = _GUICtrlListView_SubItemHitTest(GUICtrlGetHandle($ListView1));确定哪些项目或子项目在控件的给定位置,参见帮助文件
$ListView1_HitItemIndex = $ListView1_HitItem;获取点击项目的0基索引
$ListView1_HitItemSubItem = $ListView1_HitItem;获取点击项目的子项索引
If $ListView1_HitItemIndex = -1 Then Return
$ListView1_HitItemPos = ControlGetPos($Form, "", $ListView1);获取Listview坐标
$ListView1_HitItemRect = _GUICtrlListView_GetSubItemRect($ListView1, $ListView1_HitItem, $ListView1_HitItem);获取点击项的边界矩形
$ListView1_HitItemText = _GUICtrlListView_GetItemText($ListView1, $ListView1_HitItem, $ListView1_HitItem) ;读取所点击单元格的文本
$ListView1_HitItemInput = GUICtrlCreateInput($ListView1_HitItemText, _ ;创建一个与单元格差不多大小的输入框 ;$ListView1_HitItemText为在Input控件中显示的文本
$ListView1_HitItemRect + $ListView1_HitItemPos + 4, _;Input控件距离左侧的值,即X坐标,由点击项目的X坐标加上Listview的X坐标再加上4(4为微调值,保证X坐标吻合)
$ListView1_HitItemRect + $ListView1_HitItemPos + 2, _;Input控件距离上方的值,即Y坐标,由点击项目的Y坐标加上Listview的Y坐标再加上2(2为微调值,保证Y坐标吻合)
_GUICtrlListView_GetColumnWidth($ListView1, $ListView1_HitItem) - 2, _;Input控件的宽度
$ListView1_HitItemRect - $ListView1_HitItemRect);Input控件的高度
GUICtrlSetState(-1, $GUI_ONTOP + $GUI_FOCUS) ;使输入框具有焦点及置顶
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
Func _WM_COMMAND($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
Case GUICtrlGetHandle($ListView1_HitItemInput)
Switch $iCode
Case $EN_KILLFOCUS ; 控件丢失键盘焦点时发送
_GUICtrlListView_SetItemText($ListView1, $ListView1_HitItemIndex, GUICtrlRead($ListView1_HitItemInput), $ListView1_HitItemSubItem) ;更改子项文本,$ListView1_HitItemIndex项目索引,$ListView1_HitItemSubItem子项的索引
GUICtrlDelete($ListView1_HitItemInput);删除Input控件
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND上面这段代码中有两个Listview,现在出现的问题是双击第一个Listview编辑后,再去双击第二个Listview编辑时就会出现问题,失去焦点后不是修改第二个Listview的编辑项,而是修改第一个Listview中最后编辑的项目,我把下面的代码放到前面一点(Case GUICtrlGetHandle($ListView_HitItemInput))就会出现编辑了第二个Listview后再编辑第一个Listview修改的是第二个Listview的值 Case GUICtrlGetHandle($ListView1_HitItemInput)
Switch $iCode
Case $EN_KILLFOCUS ; 控件丢失键盘焦点时发送
_GUICtrlListView_SetItemText($ListView1, $ListView1_HitItemIndex, GUICtrlRead($ListView1_HitItemInput), $ListView1_HitItemSubItem) ;更改子项文本,$ListView1_HitItemIndex项目索引,$ListView1_HitItemSubItem子项的索引
GUICtrlDelete($ListView1_HitItemInput);删除Input控件
EndSwitch纠结了好半天了,不知道问题出在哪里,希望各位帮忙试试,先感谢了
依然要感谢蛋蛋大大,呵呵,以下是修改后的代码:#include <GuiListView.au3>
#include <Editconstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
;2011.11.09 by bbs.wglm.net 小A修改至:yhxhappy2012.11.22由qinylj修改
;[修改] 删掉了原来的Enter键,仅仅换成Input输入,但是这是目前我比较满意的一个Listview原地编辑例子,字体正常,支持右键复制
Global $ListView_HitItem, $Listview_HitItemIndex, $Listview_HitItemSubItem, $ListView_HitItemInput, $Listview_EditLV;定义全局变量粘贴
$Form = GUICreate("LIST原位编辑支持子项 by 小A&修改by qinylj,感谢蛋蛋大大的帮助", 1200, 708)
GUISetFont(9, 400, 0, "微软雅黑")
$ListView = GUICtrlCreateListView("1|2|3|4", 8, 8, 592, 700)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$ListView1 = GUICtrlCreateListView("1|2|3|4", 600, 8, 592, 700)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$label = GUICtrlCreateLabel("", -100, -100, 1, 1)
GUISetState(@SW_SHOW)
For $i = 0 To 3
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, $i, 135)
GUICtrlSendMsg($ListView1, $LVM_SETCOLUMNWIDTH, $i, 130)
Next
For $i = 0 To 120
GUICtrlCreateListViewItem("Au3中文论坛啊,呵呵" & $i & "|Autoit中文论坛" & $i & "|感谢蛋蛋大大netegg" & $i & "|还要感谢小A" & $i, $ListView)
GUICtrlCreateListViewItem("Qinylj啊,哈哈" & $i & "|BY Qinylj我晕哦" & $i & "|小A是哪个?" & $i & "|修改By啊Qinylj" & $i, $ListView1)
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)
Local $tNMHDR, $hWndFrom, $iCode
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = DllStructGetData($tNMHDR, 'hWndFrom')
$iCode = DllStructGetData($tNMHDR, 'Code')
Switch $hWndFrom
Case GUICtrlGetHandle($ListView), GUICtrlGetHandle($ListView1)
Switch $iCode
Case $NM_DBLCLK;双击消息
$Listview_EditLV = $hWndFrom
$ListView_HitItem = _GUICtrlListView_SubItemHitTest($hWndFrom);确定哪些项目或子项目在控件的给定位置,参见帮助文件
$Listview_HitItemIndex = $ListView_HitItem;获取点击项目的0基索引
$Listview_HitItemSubItem = $ListView_HitItem;获取点击项目的子项索引
If $Listview_HitItemIndex = -1 Then Return
$ListView_HitItemPos = ControlGetPos($Form, "", $hWndFrom);获取Listview坐标
$ListView_HitItemRect = _GUICtrlListView_GetSubItemRect($hWndFrom, $ListView_HitItem, $ListView_HitItem);获取点击项的边界矩形
$ListView_HitItemText = _GUICtrlListView_GetItemText($hWndFrom, $ListView_HitItem, $ListView_HitItem) ;读取所点击单元格的文本
$ListView_HitItemInput = GUICtrlCreateInput($ListView_HitItemText, _ ;创建一个与单元格差不多大小的输入框 ;$ListView_HitItemText为在Input控件中显示的文本
$ListView_HitItemRect + $ListView_HitItemPos + 4, _;Input控件距离左侧的值,即X坐标,由点击项目的X坐标加上Listview的X坐标再加上4(4为微调值,保证X坐标吻合)
$ListView_HitItemRect + $ListView_HitItemPos + 2, _;Input控件距离上方的值,即Y坐标,由点击项目的Y坐标加上Listview的Y坐标再加上2(2为微调值,保证Y坐标吻合)
_GUICtrlListView_GetColumnWidth($hWndFrom, $ListView_HitItem) - 2, _;Input控件的宽度
$ListView_HitItemRect - $ListView_HitItemRect);Input控件的高度
GUICtrlSetState(-1, $GUI_ONTOP + $GUI_FOCUS) ;使输入框具有焦点及置顶
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
Func _WM_COMMAND($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_EditLV, $Listview_HitItemIndex, GUICtrlRead($ListView_HitItemInput), $Listview_HitItemSubItem) ;更改子项文本,$Listview_HitItemIndex项目索引,$Listview_HitItemSubItem子项的索引
GUICtrlDelete($ListView_HitItemInput);删除Input控件
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND 问题应该是出在_WM_COMMAND中,但是我就是没找到怎么解决,头大了都,哎 先把所有的消息里的消息条件都删掉一个,同一句柄的同一消息不能重复定义 回复 3# netegg
没怎么看懂,是不是$EN_KILLFOCUS这个放前面,然后再switch控件?还请大大明示? _WM_COMMAND这个子函数内的case底下为什么是重复的 回复 5# netegg
一个是Case GUICtrlGetHandle($ListView)
一个是Case GUICtrlGetHandle($ListView1),没重复啊,只是代码功能相同,我想用两个去区分 本帖最后由 netegg 于 2012-11-24 12:44 编辑
Switch $hWndFrom
Case GUICtrlGetHandle($ListView),GUICtrlGetHandle($ListView1)
子函数里用$hwndfrom
switch切的是同一变量不同的值,把值直接带到子函数里有什么用,那不如不用切了直接带进去不是更简单 回复 7# netegg
哦,对的哈,感谢蛋蛋大大,呵呵,真是糊涂了哈 竟然有注释,赶快做好笔记!
页:
[1]