回复 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[0]]
For $i = 1 To $Name_1[0]
$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[0]]
For $i = 1 To $Name_2[0]
$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
|