【已解决】两个TreeView下动态获取item,判断item名称相同时勾选,应该怎么处理?
本帖最后由 cynthia59 于 2013-5-29 15:04 编辑我的流程:
1,通过指定路径获取到新旧版本的工程名(文件夹名)
2,生成新旧工程列表(我用了两个TreeView,下面的TreeViewItem是动态获取的)
3,判断如果两个列表中有相同的工程名,则同时勾选上两个item(这步不会)
4,手动修改时,如果两个列表都有,则勾的时候一起勾上或勾掉,如果只有一个列表有,则勾不上(这步也不会)
如果不用TreeView的方式,还有什么其它更好的方式吗?
#include <GuiConstantsEx.au3>
#include <ButtonConstants.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <AZ.au3>
#Include <File.au3>
#Include <ACN_String.au3>
#include <TreeViewConstants.au3>
Opt ('GUIOnEventMode',1)
; GUI
$parent = GuiCreate('批量对比工具', 400, 420)
GUISetOnEvent($GUI_EVENT_CLOSE, 'SpecialEvents')
GUISetOnEvent($GUI_EVENT_MINIMIZE, 'SpecialEvents')
GUISetOnEvent($GUI_EVENT_RESTORE, 'SpecialEvents')
$Label_1 = GuiCtrlCreateLabel('计算结果保存路径:', 10, 10)
$Label_2 = GuiCtrlCreateLabel('对比结果文件自动保存在此目录下!', 120, 10)
GUICtrlSetColor(-1, 0xff0000) ; Red
$Input_1 = GUICtrlCreateInput ( 'C:\result', 10 , 30 , 315)
$Button_1 = GuiCtrlCreateButton('选择', 333, 28 , 60)
GUICtrlSetOnEvent(-1, 'ChooseResultLocationPressed')
$Button_2 = GuiCtrlCreateButton('获取计算结果文件', 10, 60, 110)
GUICtrlSetOnEvent(-1, 'GetNamePressed')
$Label_3 = GuiCtrlCreateLabel('旧版本:', 10, 90)
$tree_1 = GuiCtrlCreateTreeView(12, 110, 176, 265, $TVS_CHECKBOXES)
$Label_4 = GuiCtrlCreateLabel('新版本:', 210, 90)
$tree_2 = GuiCtrlCreateTreeView(212, 110, 176, 265, $TVS_CHECKBOXES)
$Button_8 = GuiCtrlCreateButton('对比工程量', 100, 383 , 200 , 30)
GUICtrlSetOnEvent(-1, 'RunPressed')
Func ChooseResultLocationPressed()
$var_result = FileSelectFolder('请选择计算结果保存路径:', '', 1)
GUICtrlSetData ( $Input_1, $var_result)
EndFunc ;==>ChooseResultLocationPressed
Func GetNamePressed()
If FileExists(GUICtrlRead($Input_1)) = 0 Then
MsgBox(48,'Error', '"计算结果保存路径"为空!' )
Return
EndIf
$Name_1 = _FileListToArray(GUICtrlRead($Input_1) & '\旧版本\', '*' , 2)
For $i = 1 To $Name_1
GuiCtrlCreateTreeViewItem($Name_1[$i], $tree_1 )
Next
$Name_2 = _FileListToArray(GUICtrlRead($Input_1) & '\新版本\', '*' , 2)
For $i = 1 To $Name_2
GuiCtrlCreateTreeViewItem($Name_2[$i], $tree_2 )
Next
;默认勾选
EndFunc ;==>GetNamePressed
Func RunPressed()
EndFunc ;==>RunPressed
Func SpecialEvents()
Select
Case @GUI_CtrlId = $GUI_EVENT_CLOSE
Exit
Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
Case @GUI_CtrlId = $GUI_EVENT_RESTORE
EndSelect
EndFunc ;==>SpecialEvents
; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd 占个座,等各位大大指导~ 大大们是不是出游了{:face (461):} 回复 1# cynthia59
借鉴了他人的帖子的经验终于可以实现这个效果了.
注册系统信息虽然能及时响应事件,但太及时了,反而会判断不正确!
所以关键在于'延时再进行相应的判断'!
不多说了, 看代码吧, 有不理解的地方请跟帖继续探讨.
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <ButtonConstants.au3>
#include <Constants.au3>
#include <EditConstants.au3>
;#include <AZ.au3>
#include <File.au3>
#include <ACN_String.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
Opt('GUIOnEventMode', 1)
Global $path, $bool
; GUI
$parent = GUICreate('批量对比工具', 400, 420)
GUISetOnEvent($GUI_EVENT_CLOSE, 'SpecialEvents')
GUISetOnEvent($GUI_EVENT_MINIMIZE, 'SpecialEvents')
GUISetOnEvent($GUI_EVENT_RESTORE, 'SpecialEvents')
$Label_1 = GUICtrlCreateLabel('计算结果保存路径:', 10, 10)
$Label_2 = GUICtrlCreateLabel('对比结果文件自动保存在此目录下!', 120, 10)
GUICtrlSetColor(-1, 0xff0000) ; Red
$Input_1 = GUICtrlCreateInput('d:\autoit3', 10, 30, 315)
$path = GUICtrlRead($Input_1)
$Button_1 = GUICtrlCreateButton('选择', 333, 28, 60)
GUICtrlSetOnEvent(-1, 'ChooseResultLocationPressed')
$Button_2 = GUICtrlCreateButton('获取计算结果文件', 10, 60, 110)
GUICtrlSetOnEvent(-1, 'GetNamePressed')
$Label_3 = GUICtrlCreateLabel('旧版本:', 10, 90)
$tree_1 = GUICtrlCreateTreeView(12, 110, 176, 265, $TVS_CHECKBOXES)
$h_t1 = GUICtrlGetHandle(-1)
$Label_4 = GUICtrlCreateLabel('新版本:', 210, 90)
$tree_2 = GUICtrlCreateTreeView(212, 110, 176, 265, $TVS_CHECKBOXES)
$h_t2 = GUICtrlGetHandle(-1)
$Button_8 = GUICtrlCreateButton('对比工程量', 100, 383, 200, 30)
GUICtrlSetOnEvent(-1, 'RunPressed')
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
Sleep(50)
WEnd
Func ChooseResultLocationPressed()
$path = FileSelectFolder('请选择计算结果保存路径:', '', 1)
If @error Then Return
GUICtrlSetData($Input_1, $path)
EndFunc ;==>ChooseResultLocationPressed
Func GetNamePressed()
If FileExists(GUICtrlRead($Input_1)) = 0 Then Return MsgBox(48, 'Error', '"计算结果保存路径"为空!')
_GUICtrlTreeView_DeleteAll($h_t1)
_GUICtrlTreeView_DeleteAll($h_t2)
$Name_1 = _FileListToArray(GUICtrlRead($Input_1) & '\旧版本\', '*', 2)
If Not IsArray($Name_1) Then Return MsgBox(48, 'Error', '旧版本为空目录')
$Name_2 = _FileListToArray(GUICtrlRead($Input_1) & '\新版本\', '*', 2)
If Not IsArray($Name_2) Then Return MsgBox(48, 'Error', '新版本为空目录')
Global $t_item1[$Name_1]
For $i = 1 To $Name_1
$t_item1[$i - 1] = GUICtrlCreateTreeViewItem($Name_1[$i], $tree_1)
GUICtrlSetOnEvent(-1, '_Click1')
If FileExists($path & '\新版本\' & $Name_1[$i]) Then GUICtrlSetState(-1, $Gui_checked)
Next
Global $t_item2[$Name_2]
For $i = 1 To $Name_2
$t_item2[$i - 1] = GUICtrlCreateTreeViewItem($Name_2[$i], $tree_2)
GUICtrlSetOnEvent(-1, '_Click2')
If FileExists($path & '\旧版本\' & $Name_2[$i]) Then GUICtrlSetState(-1, $Gui_checked)
Next
EndFunc ;==>GetNamePressed
Func RunPressed()
EndFunc ;==>RunPressed
Func SpecialEvents()
Select
Case @GUI_CtrlId = $GUI_EVENT_CLOSE
Exit
Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
Case @GUI_CtrlId = $GUI_EVENT_RESTORE
EndSelect
EndFunc ;==>SpecialEvents
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $h_t1
Switch $iCode
Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW ;$NM_CLICK,
$bool = True
AdlibRegister('_set_checked', 50)
EndSwitch
Case $h_t2
Switch $iCode
Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW ;$NM_CLICK,
$bool = False
AdlibRegister('_set_checked', 50)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _click1()
$bool = True
AdlibRegister('_set_checked', 50)
EndFunc ;==>_click1
Func _click2()
$bool = False
AdlibRegister('_set_checked', 50)
EndFunc ;==>_click2
Func _set_checked()
AdlibUnRegister('_set_checked')
Local $a
If $bool Then
$a = $t_item2
$h_t = $h_t1
Else
$a = $t_item1
$h_t = $h_t2
EndIf
For $i = 0 To UBound($a) - 1
$text = GUICtrlRead($a[$i], 1)
$h = _GUICtrlTreeView_FindItem($h_t, $text)
If $h = 0 Then ContinueLoop
If _GUICtrlTreeView_GetChecked($h_t, $h) Then
GUICtrlSetState($a[$i], $Gui_checked)
Else
GUICtrlSetState($a[$i], $Gui_unchecked)
EndIf
Next
EndFunc ;==>_set_checked 一早来看终于等到结果了,哈哈,先谢个,仔细研究研究 回复 4# user3000
大哥,调试错误:
E:\脚本文件\2.au3(135,30) : WARNING: $t_item2: 使用前并未进行声明.
$a = $t_item2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
E:\脚本文件\2.au3(138,30) : WARNING: $t_item1: 使用前并未进行声明.
$a = $t_item1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
E:\脚本文件\2.au3(74,36) : WARNING: $t_item2: declared global in function only. Prefer top of file.
Global $t_item2[$Name_2]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
E:\脚本文件\2.au3 - 0 error(s), 3 warning(s)
====================================================
太高深了,已经超出我的理解范围了,麻烦大哥先把这个错误给改改吧。
另外你贴的代码怎么有高亮的,这个怎么搞的? 回复 6# cynthia59
额,的确做了个不好习惯,在函数里定义了全局变量. 但实质上不影响程序运行,但有的AU3版本可能会这样检测出错误,不允许调试了.
13 行改为: Global $path, $bool, $t_item1, $t_item2
这样就可以了.
你原来的代码有更多不好的编程习惯哦. 本帖最后由 xms77 于 2013-5-29 12:40 编辑
回复 6# cynthia59
至于高亮,把你的代码放入【au3]...中就行了,当然方括号都是用英文字母的那种 回复 7# user3000
可以运行了,谢谢!
我还是新手,都是看着帮助文档做出来的东西,那个再点一下就清空原来数据的那个搞了好久都没搞出来,后来就想干脆自己记着点别点2次就行了,这你都帮我写好了,哈哈!
全局变量什么的还搞不大清楚,很多时候运行时提示错误自己都搞不明白,多亏了你们这些大侠经常帮忙啦! 回复 8# xms77
懂了, 谢谢!下次我也能贴出来至少看上去好看点的代码了,呵呵。 本帖最后由 cynthia59 于 2013-5-29 15:08 编辑
回复cynthia59
至于高亮,把你的代码放入【au3]...中就行了,当然方括号都是用英文字母的那种
xms77 发表于 2013-5-29 12:38 http://www.autoitx.com/images/common/back.gif
不好使啊?MsgBox(48, 'Error', '123')MsgBox(48, 'Error', '123') 建议用一下Total Commander,其目录对比功能非常强大 牛不 好东盟关系啊阿萨德打发 #include <Windowsconstants.au3>
#include <GuiConstants.au3>
#include <GuiTreeView.au3>
#include <WinApiex.au3>
$hGUI = GUICreate("", 400, 300)
$Tree1 = GUICtrlCreateTreeView(0, 0, 180, 300, $TVS_HASBUTTONS + $TVS_HASLINES + $TVS_LINESATROOT + $TVS_CHECKBOXES)
For $x = 1 To Random(2, 20, 1)
GUICtrlCreateTreeViewItem($x, $Tree1)
Next
$hTree1 = GUICtrlGetHandle($Tree1)
$Tree2 = GUICtrlCreateTreeView(220, 0, 180, 300, $TVS_HASBUTTONS + $TVS_HASLINES + $TVS_LINESATROOT + $TVS_CHECKBOXES)
For $x = 1 To 20
GUICtrlCreateTreeViewItem(Random(1, 20, 1), $Tree2)
Next
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
$hTree2 = GUICtrlGetHandle($Tree2)
While GUIGetMsg() <> -3
WEnd
Func WM_NOTIFY($hWnd, $uMsg, $wParam, $lParam)
Local $pNMHDR = DllStructCreate($tagNMHDR, $lParam)
Switch DllStructGetData($pNMHDR, "IDFrom")
Case $Tree1
Switch DllStructGetData($pNMHDR, "Code")
Case $NM_CLICK
Local $PosX = _WinAPI_GetMousePosX(True, $hTree1), $PosY = _WinAPI_GetMousePosY(True, $hTree1)
Local $uFlags = _GUICtrlTreeView_HitTest($hTree1, $PosX, $PosY)
If (BitAND($uFlags, BitOR($TVHT_ONITEM, $TVHT_ONITEMBUTTON, $TVHT_ONITEMICON))) Then
$Item = _GUICtrlTreeView_HitTestItem($hTree1, $PosX, $PosY)
$Text=_GUICtrlTreeView_GetText($hTree1, $Item)
Local $hItem=_GUICtrlTreeView_GetFirstVisible($hTree2)
While $hItem
If _GUICtrlTreeView_GetText($hTree2,$hItem)==$Text Then
_GUICtrlTreeView_SetChecked($hTree2,$hItem,True)
EndIf
$hItem=_GUICtrlTreeView_GetNextVisible($hTree2,$hItem)
WEnd
EndIf
EndSwitch
EndSwitch
EndFunc ;==>WM_NOTIFY是这个意思吗???
还可以不用WM_NOTIFY
直接setwindowlong 截取 WM_LBUTTONDOWN 看不懂啊....
页:
[1]