【已解决】关于使用Tree和listView遇到的一个icombo不弹出的问题
本帖最后由 anythinging 于 2021-4-2 17:11 编辑代码是经chzj589指点,改进的tree、子窗、listview的调用。
遇到个小问题,正常运行一切正常,如果在表1中弹出一个新窗口,关闭后,姓别一列里的弹出菜单就失效了,由于不知道差在哪,目前自己的方法是,重新调一遍listview1或将删除新窗口改为隐藏,但不管用哪个方法都感觉比较LOW.
请教各位标准的方法,恳请指点,谢谢!#NoTrayIcon
#include <WinAPI.au3>
#include <WinAPIEx.au3>
#include <TreeViewConstants.au3>
#include <GuiMenu.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $hGui,$hGuiSub,$Sub1gui
Global $hGUI, $ListView1, $hListView1, $ListView2, $hListView2,$aItem, $iInput, $iCombo
Opt("GUIOnEventMode", 1)
CreateGUI()
CreateGUISub1()
Func CreateGUI()
$hGUI = GUICreate("主窗口", 1200, 600, -1, -1,BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_SIZEBOX))
GUISetOnEvent($GUI_EVENT_CLOSE,"_EXIT")
GUISetState()
Local $idTreeview = GUICtrlCreateTreeView(2, 3, 200, 400, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
Local $idTreeviewA = GUICtrlCreateTreeViewItem("Tree", $idTreeview)
Local $idTreeviewA1 = GUICtrlCreateTreeViewItem("表1", $idTreeviewA)
GUICtrlSetOnEvent(-1,'TreeA1')
Dim $iCombo = GUICtrlCreateCombo('', -100, -100)
GUIRegisterMsg(0x004E, '_WM_NOTIFY') ;$WM_NOTIFY = 0x004E
Local $id1 = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_UdpData')
Local $aAccelKeys = [['{Enter}', $id1]]
GUISetAccelerators($aAccelKeys)
Global $aItem
EndFunc
Func CreateGUISub1()
;~ ------------------------- 子窗口 1 ------------------------------
$hGuiSub = GUICreate("子窗口1", 1000, 600, 201, 1, $WS_CHILD, $WS_EX_CLIENTEDGE, $hGUI)
GUISetBkColor(0x4682B4)
GUISetState()
GUICtrlCreateButton('添加',110,550,80,25)
GUICtrlSetOnEvent(-1,'_Sub1Add')
_ListView1()
EndFunc
While 1
Sleep(10)
WEnd
Func TreeA1()
If IsArray($aItem) Then
_UdpData()
EndIf
GUIDelete($hGuiSub)
CreateGUISub1()
EndFunc
Func _ListView1()
Dim $ListView1 = GUICtrlCreateListView('用户名1 | 密码1 |性别|管理', 1, 2, 296, 350)
Dim $hListView1 = GUICtrlGetHandle(-1)
For $i = 1 to 10
GUICtrlCreateListViewItem(''&$i&'|123456|男|删除', $ListView1)
Next
EndFunc
Func _Sub1Add()
$Sub1gui = GUICreate('',300,200,-1,-1)
GUICtrlCreateButton('关闭',10,50,60,25)
GUICtrlSetOnEvent(-1,'_Sub1Close')
GUISetState()
EndFunc
Func _Sub1Close()
GUIDelete($Sub1gui)
;~ GUISetState(@SW_HIDE ,$Sub1gui)
;~ TreeA1() ;临时解决方法
EndFunc
Func _UdpData()
If Not IsArray($aItem) Then Return
Local $sRd = GUICtrlRead($iCombo), $sData = ''
_GUICtrlListView_SetItemText($hListView1, $aItem, $sRd, $aItem)
GUICtrlSetPos($iCombo, -1000, -100)
Dim $aItem = 0
EndFunc
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch DllStructGetData($tNMHDR, 'hWndFrom')
Case $hListView1
Switch $iCode
Case -2 ;$NM_CLICK = -2
If IsArray($aItem) Then
_UdpData()
EndIf
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $index = DllStructGetData($tInfo, 'Index')
Local $SubItem = DllStructGetData($tInfo, 'SubItem')
If $SubItem =2 Then
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('男', $context)
GUICtrlCreateMenuItem('女', $context)
Local $iid = _GUICtrlMenu_TrackPopupMenu($hMenu, $hGui, -1, -1, 1, 1, 2)
If $iid Then _GUICtrlListView_SetItemText($hListView1, $index, _GUICtrlMenu_GetItemText($hMenu, $iid, False), 2)
EndIf
EndSwitch
EndSwitch
EndFunc ;==>_WM_NOTIFY
Func _Exit()
Exit
EndFunc ;==>_Exit
118 行Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
你怎么能在消息里创建控件呢?那样的话你每次点击列表都会创建一次控件。老早就跟你说过,控件在外面创建一次就好,需要的使用弹出就行。
你创建子窗口之后,关闭的动作是删除窗口,这样的话,窗口都没有了,如何创建控件呢?所以你的118行也就不会成功创建。
#NoTrayIcon
#include <WinAPI.au3>
#include <WinAPIEx.au3>
#include <TreeViewConstants.au3>
#include <GuiMenu.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $hGui, $hGuiSub, $Sub1gui
Global $hGui, $ListView1, $hListView1, $ListView2, $hListView2, $aItem, $iInput, $iCombo
Global $hMenu
Opt("GUIOnEventMode", 1)
CreateGUI()
CreateGUISub1()
Func CreateGUI()
$hGui = GUICreate("主窗口", 1200, 600, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_SIZEBOX))
GUISetOnEvent($GUI_EVENT_CLOSE, "_EXIT")
GUISetState()
Local $idTreeview = GUICtrlCreateTreeView(2, 3, 200, 400, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
Local $idTreeviewA = GUICtrlCreateTreeViewItem("Tree", $idTreeview)
Local $idTreeviewA1 = GUICtrlCreateTreeViewItem("表1", $idTreeviewA)
GUICtrlSetOnEvent(-1, 'TreeA1')
Dim $iCombo = GUICtrlCreateCombo('', -100, -100)
GUIRegisterMsg(0x004E, '_WM_NOTIFY') ;$WM_NOTIFY = 0x004E
Local $id1 = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_UdpData')
Local $aAccelKeys = [['{Enter}', $id1]]
GUISetAccelerators($aAccelKeys)
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('男', $context)
GUICtrlCreateMenuItem('女', $context)
EndFunc ;==>CreateGUI
Func CreateGUISub1()
;~ ------------------------- 子窗口 1 ------------------------------
$hGuiSub = GUICreate("子窗口1", 1000, 600, 201, 1, $WS_CHILD, $WS_EX_CLIENTEDGE, $hGui)
GUISetBkColor(0x4682B4)
GUISetState()
GUICtrlCreateButton('添加', 110, 550, 80, 25)
GUICtrlSetOnEvent(-1, '_Sub1Add')
_ListView1()
EndFunc ;==>CreateGUISub1
While 1
Sleep(10)
WEnd
Func TreeA1()
If IsArray($aItem) Then
_UdpData()
EndIf
GUIDelete($hGuiSub)
CreateGUISub1()
EndFunc ;==>TreeA1
Func _ListView1()
Dim $ListView1 = GUICtrlCreateListView('用户名1 | 密码1 |性别|管理', 1, 2, 296, 350)
Dim $hListView1 = GUICtrlGetHandle(-1)
For $i = 1 To 10
GUICtrlCreateListViewItem('' & $i & '|123456|男|删除', $ListView1)
Next
EndFunc ;==>_ListView1
Func _Sub1Add()
$Sub1gui = GUICreate('', 300, 200, -1, -1)
GUICtrlCreateButton('关闭', 10, 50, 60, 25)
GUICtrlSetOnEvent(-1, '_Sub1Close')
GUISetState()
EndFunc ;==>_Sub1Add
Func _Sub1Close()
GUIDelete($Sub1gui)
EndFunc ;==>_Sub1Close
Func _UdpData()
If Not IsArray($aItem) Then Return
Local $sRd = GUICtrlRead($iCombo), $sData = ''
_GUICtrlListView_SetItemText($hListView1, $aItem, $sRd, $aItem)
GUICtrlSetPos($iCombo, -1000, -100)
Dim $aItem = 0
EndFunc ;==>_UdpData
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch DllStructGetData($tNMHDR, 'hWndFrom')
Case $hListView1
Switch $iCode
Case -2 ;$NM_CLICK = -2
If IsArray($aItem) Then
_UdpData()
EndIf
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $index = DllStructGetData($tInfo, 'Index')
Local $SubItem = DllStructGetData($tInfo, 'SubItem')
If $SubItem = 2 Then
Local $iid = _GUICtrlMenu_TrackPopupMenu($hMenu, $hGui, -1, -1, 1, 1, 2)
If $iid Then _GUICtrlListView_SetItemText($hListView1, $index, _GUICtrlMenu_GetItemText($hMenu, $iid, False), 2)
EndIf
Case -3 ;$NM_DBLCLK = -3
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $index = DllStructGetData($tInfo, 'Index')
Local $SubItem = DllStructGetData($tInfo, 'SubItem')
Dim $aItem = 0
Local $aRect = _GUICtrlListView_GetSubItemRect($hListView1, $index, $SubItem)
Dim $aItem = [$index, $SubItem]
Local $sDef ;设置菜单为空
Global $sData = _GUICtrlListView_GetItemText($hListView1, $index, $SubItem)
GUICtrlSetPos($iCombo, $aRect + 204, $aRect + 8, $aRect - $aRect)
GUICtrlSetData($iCombo, '|' & $sData, $sData)
GUICtrlSetState($iCombo, 256)
EndSwitch
EndSwitch
EndFunc ;==>_WM_NOTIFY
Func _Exit()
Exit
EndFunc ;==>_Exit
代码一定要精简到可运行且可重现问题,这样才能有针对性的解决问题。
这其实是个技巧,在精简过程中很容易自己就能找到问题所在。 本帖最后由 anythinging 于 2021-4-1 22:58 编辑
我把1楼代码改成发现问题的样子了
目前已找到2个方法,143行函数
[*]Func _Sub1Close()
[*] GUIDelete($Sub1gui)
[*] ;TreeA1() ;临时解决方法
[*]EndFunc
1、如果用GUIDelete($Sub1gui)删除新窗口,那么就需要 运行TreeA1() 重新加载一遍子窗口和listview1,否则弹出菜单失效
2、或者干脆使用guisetstate隐藏新窗口,不会影响到弹出菜单
感觉都比较低端,不明白icombo的菜单为何会受到删除新窗口后不弹出?
本帖最后由 anythinging 于 2021-4-1 23:10 编辑
afan 发表于 2021-4-1 22:20
代码一定要精简到可运行且可重现问题,这样才能有针对性的解决问题。
这其实是个技巧,在精简过程中很容易 ...
感谢指点,如您所说,把代码精简到最小后,还是没找到问题:face (13): 刚刚再回看当初A版指点的ListView代码,又明白了许多,A版代码真是博大精深!
学习A版的ListView示例半年多了,终于又有点长进了,感谢A版指点! 本帖最后由 anythinging 于 2021-4-2 17:47 编辑
afan 发表于 2021-4-1 23:27
118 行Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
你怎么能在消息里创建控件呢 ...
还有个问题请教A版,如果有多个列对应不同的弹出菜单,是否可以共用一个虚拟控件?我测试好象没遇到什么错误,但还是想向您求证一下,如下:
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('男', $context)
GUICtrlCreateMenuItem('女', $context)
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu2 = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('1', $context)
GUICtrlCreateMenuItem('2', $context)
GUICtrlCreateMenuItem('3', $context)
anythinging 发表于 2021-4-2 17:43
还有个问题请教A版,如果有多个列对应不同的弹出菜单,是否可以共用一个虚拟控件?我测试好象没遇到什么 ...
这不是共用,也不需要共用,就这样可以。
每个菜单 GUICtrlCreateContextMenu 一个主控件就行。 afan 发表于 2021-4-2 18:00
这不是共用,也不需要共用,就这样可以。
每个菜单 GUICtrlCreateContextMenu 一个主控件就行。
收到,感谢指点! 本帖最后由 chzj589 于 2021-4-5 08:11 编辑
把控件放在子窗口里面,一目了然。好添加与修改
#NoTrayIcon
#include <TreeViewConstants.au3>
#include <GuiMenu.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUITreeView.au3>
Global $hGui, $hGuiSub, $Sub1gui, $hMenu
Global $ListView1, $hListView1, $aItem, $iInput, $iCombo
Opt("GUIOnEventMode", 1)
CreateGUI()
While 1
Sleep(1000)
WEnd
Func _Exit()
Exit
EndFunc ;==>_Exit
Func CreateGUI()
$hGui = GUICreate("主窗口", 1200, 600, -1, -1);, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_SIZEBOX))
GUISetOnEvent($GUI_EVENT_CLOSE, "_EXIT")
Local $idTreeview = GUICtrlCreateTreeView(2, 3, 200, 400, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
Local $idTreeviewA = GUICtrlCreateTreeViewItem("Tree", $idTreeview)
Local $idTreeviewA1 = GUICtrlCreateTreeViewItem("表1", $idTreeviewA)
GUICtrlSetOnEvent(-1, '_idTreeviewA1')
Local $idTreeviewA2 = GUICtrlCreateTreeViewItem("表2", $idTreeviewA)
GUICtrlSetOnEvent(-1, '_idTreeviewA2')
_GUICtrlTreeView_SetHeight($idTreeview, 20)
_GUICtrlTreeView_ExpandedOnce($idTreeview, $idTreeviewA)
_GUICtrlTreeView_Expand($idTreeview, $idTreeviewA1)
Dim $iCombo = GUICtrlCreateCombo('', -100, -100)
;~ ------------------------- 子窗口 1 ------------------------------
$hGuiSub = GUICreate("子窗口1", 985, 585, 205, 1, $WS_POPUP, 0x00000010 + 0x00000040 + 0x00000100, $hGui)
Dim $ListView1 = GUICtrlCreateListView('用户名1 | 密码1 |性别|管理', 1, 2, 296, 350)
Dim $hListView1 = GUICtrlGetHandle(-1)
For $i = 1 To 10
GUICtrlCreateListViewItem('' & $i & '|123456|男|删除', $ListView1)
Next
GUISetState(@SW_SHOW, $hGuiSub)
GUISetState(@SW_SHOW, $hGui)
GUIRegisterMsg(0x004E, '_WM_NOTIFY') ;$WM_NOTIFY = 0x004E
Local $id1 = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_UdpData')
Local $aAccelKeys = [['{Enter}', $id1]]
GUISetAccelerators($aAccelKeys)
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('男', $context)
GUICtrlCreateMenuItem('女', $context)
EndFunc ;==>CreateGUI
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch DllStructGetData($tNMHDR, 'hWndFrom')
Case $hListView1
Switch $iCode
Case -3 ;$NM_CLICK = -2 ;$NM_DBLCLK = -3
If IsArray($aItem) Then
_UdpData()
EndIf
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $index = DllStructGetData($tInfo, 'Index')
Local $SubItem = DllStructGetData($tInfo, 'SubItem')
If Not StringRegExp($SubItem, "^(2)$") Then Return 'GUI_RUNDEFMSG'
If $SubItem = 2 Then
Local $iid = _GUICtrlMenu_TrackPopupMenu($hMenu, $hGui, -1, -1, 1, 1, 2)
If $iid Then _GUICtrlListView_SetItemText($hListView1, $index, _GUICtrlMenu_GetItemText($hMenu, $iid, False), 2)
EndIf
EndSwitch
EndSwitch
EndFunc ;==>_WM_NOTIFY
Func _UdpData()
If Not IsArray($aItem) Then Return
Local $sRd = GUICtrlRead($iCombo), $sData = ''
_GUICtrlListView_SetItemText($hListView1, $aItem, $sRd, $aItem)
GUICtrlSetPos($iCombo, -1000, -100)
Dim $aItem = 0
EndFunc ;==>_UdpData
Func _idTreeviewA1()
GUIDelete($hGuiSub)
;~ ------------------------- 子窗口 2------------------------------
$hGuiSub = GUICreate("子窗口2", 985, 585, 205, 1, $WS_POPUP, 0x00000010 + 0x00000040 + 0x00000300, $hGui)
GUISetBkColor(0x4682B4)
;GUISetState()
Dim $ListView1 = GUICtrlCreateListView('用户名1 | 密码1 |性别|管理', 1, 2, 296, 350)
Dim $hListView1 = GUICtrlGetHandle(-1)
For $i = 1 To 10
GUICtrlCreateListViewItem('' & $i & '|123456|男|删除', $ListView1)
Next
GUICtrlCreateButton('添加', 110, 550, 80, 25)
GUICtrlSetOnEvent(-1, '_Sub1Add')
GUISetState(@SW_SHOW, $hGuiSub)
EndFunc ;==>_idTreeviewA1
Func _Sub1Add()
$Sub1gui = GUICreate('', 300, 200, -1, -1)
GUICtrlCreateButton('关闭', 10, 50, 60, 25)
GUICtrlSetOnEvent(-1, '_Sub1Close')
GUISetState()
EndFunc ;==>_Sub1Add
Func _Sub1Close()
GUIDelete($Sub1gui)
;~ GUISetState(@SW_HIDE ,$Sub1gui)
;~ TreeA1() ;临时解决方法
EndFunc ;==>_Sub1Close
Func _idTreeviewA2()
GUIDelete($hGuiSub)
;~ ------------------------- 子窗口 3------------------------------
$hGuiSub = GUICreate("子窗口3", 985, 585, 205, 1, $WS_POPUP, 0x00000010 + 0x00000040 + 0x00000200, $hGui)
GUISetBkColor(0x4682B4)
;GUISetState()
Dim $ListView1 = GUICtrlCreateListView('用户名1 | 密码1 |性别|管理', 1, 2, 296, 350)
Dim $hListView1 = GUICtrlGetHandle(-1)
For $i = 1 To 10
GUICtrlCreateListViewItem('' & $i & '|56789' & $i & '|男|删除', $ListView1)
Next
GUICtrlCreateButton('添加', 110, 550, 80, 25)
GUICtrlSetOnEvent(-1, '_Sub1Add')
GUISetState(@SW_SHOW, $hGuiSub)
EndFunc ;==>_idTreeviewA2
afan 发表于 2021-4-2 18:00
这不是共用,也不需要共用,就这样可以。
每个菜单 GUICtrlCreateContextMenu 一个主控件就行。
A版:请问一下,我用
为何无效? 本帖最后由 afan 于 2021-4-4 11:51 编辑
chzj589 发表于 2021-4-4 11:44
A版:请问一下,我用
为何无效?
【au3】 标签早就不支持了……
【pre】 也不行
只能用【code】标签,但也很多问题
chzj589 发表于 2021-4-4 11:37
把控件放在子窗口里面,一目了然。好添加与修改
感谢指点!我按A的方法把控件放主窗口了,也按你的方法试了放子窗口没有问题,控件不会因为删除子窗口而失效,不知原因,
anythinging 发表于 2021-4-5 12:01
感谢指点!我按A的方法把控件放主窗口了,也按你的方法试了放子窗口没有问题,控件不会因为删除子窗口而 ...
Dim $iCombo = GUICtrlCreateCombo('', -100, -100)
这一句必需放在子窗口的前面
只是告诉你子窗口这样设置,要修改或要添加控件比较不会乱。
想增加子窗口时也很简单
页:
[1]