找回密码
 加入
搜索
查看: 3875|回复: 13

[AU3基础] au3能做出视图管理文件夹及文件

 火.. [复制链接]
发表于 2014-5-14 17:26:04 | 显示全部楼层 |阅读模式
如:打开么个文件夹在ListView显示、管理、和打开子文件夹等

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2014-5-14 19:09:18 | 显示全部楼层
是这种效果么?刚好我有收藏
#include<GuiTreeView.au3>
#include<EditConstants.au3>
#include<GUIConstantsEx.au3>
#include<TreeViewConstants.au3>
#include<WindowsConstants.au3>

Global $sPath = 'C:\WINDOWS'
Global $width = 800, $height = 600
Global $hGui = GUICreate('目录树状图', $width, $height, -1, 0)
Global $hInput = GUICtrlCreateInput($sPath, 10, 10, $width - 160, 20, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
Global $hChoose = GUICtrlCreateButton('...', $width - 148, 9, 30, 22)
GUICtrlSetFont(-1, 9, 600, 0, 'Verdana')
Global $hFiles = GUICtrlCreateCheckbox('包括文件', $width - 100, 10, 80, 16)
Global $bFiles = False
Global $hTreeView = GUICtrlCreateTreeView(10, 40, $width - 20, $height - 70, BitOR($GUI_SS_DEFAULT_TREEVIEW, $TVS_CHECKBOXES), $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 10, 400, 0, 'Arial')
GUICtrlSetBkColor(-1, 0xF8F8F8)
Global $hContextMenu = GUICtrlCreateContextMenu($hTreeView)
Global $hInfo = GUICtrlCreateMenuItem('信息', $hContextMenu)
Global $hExpand = GUICtrlCreateMenuItem('展开所有', $hContextMenu)
Global $hCollapse = GUICtrlCreateMenuItem('折叠所有', $hContextMenu)
Global $hStatus1 = GUICtrlCreateLabel('', 0, $height - 16, $width / 2, 16, -1, $WS_EX_STATICEDGE)
Global $hStatus2 = GUICtrlCreateLabel('', $width / 2 + 1, $height - 16, $width / 2, 16, -1, $WS_EX_STATICEDGE)
GUISetState()

_TreeViewUpdate()

While True
        Switch GUIGetMsg()
                Case $hChoose
                        $sTmp = FileSelectFolder('目录', '', 0, $sPath, $hGui)
                        If Not @error Then
                                $sPath = $sTmp
                                GUICtrlSetData($hInput, $sPath)
                                _TreeViewUpdate()
                        EndIf
                Case $hFiles
                        $bFiles = BitAND(GUICtrlRead($hFiles), $GUI_CHECKED) = $GUI_CHECKED
                        _TreeViewUpdate()
                Case $GUI_EVENT_PRIMARYDOWN
                        $aInfo = GUIGetCursorInfo($hGui)
                        If $aInfo[4] = $hTreeView Then
                                $oldGUIDataSeparatorChar = Opt('GUIDataSeparatorChar', '\')
                                GUICtrlSetData($hStatus1, ' ' & StringReplace(_GUICtrlTreeView_GetTree($hTreeView), '\\', '\'))
                                Opt('GUIDataSeparatorChar', $oldGUIDataSeparatorChar)
                                If BitAND(_GUICtrlTreeView_HitTest($hTreeView, $aInfo[0] - 11, $aInfo[1] - 41), 64) Then
                                        $checked = _GUICtrlTreeView_GetChecked($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView))
                                        _GUICtrlTreeView_SetBold($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView), $checked)
                                        $hItem = _GUICtrlTreeView_GetFirstChild($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView))
                                        If $hItem Then _MarkChildItems($hTreeView, $hItem, $checked)
                                EndIf
                        EndIf
                Case $hInfo
                        $oldGUIDataSeparatorChar = Opt('GUIDataSeparatorChar', '\')
                        $TempPath = StringReplace(_GUICtrlTreeView_GetTree($hTreeView), '\\', '\')
                        Opt('GUIDataSeparatorChar', $oldGUIDataSeparatorChar)
                        MsgBox(0, '信息', $TempPath & @CRLF & '文件大小: ' & FileGetSize($TempPath) & ' Bytes')
                Case $hExpand
                        _ExpandAll($hTreeView)
                Case $hCollapse
                        _CollapseAll($hTreeView)
                Case $GUI_EVENT_CLOSE
                        Exit
        EndSwitch
WEnd

Func _MarkChildItems(ByRef $hTreeView, $hItem, $checked)
        Do
                $hNewItem = _GUICtrlTreeView_GetFirstChild($hTreeView, $hItem)
                If $hNewItem Then _MarkChildItems($hTreeView, $hNewItem, $checked)
                _GUICtrlTreeView_SetChecked($hTreeView, $hItem, $checked)
                _GUICtrlTreeView_SetBold($hTreeView, $hItem, $checked)
                $hItem = _GUICtrlTreeView_GetNextChild($hTreeView, $hItem)
        Until $hItem = 0
EndFunc   ;==>_MarkChildItems

Func _ExpandAll($hTreeView)
        _GUICtrlTreeView_Expand($hTreeView)
EndFunc   ;==>_ExpandAll

Func _CollapseAll($hTreeView)
        _GUICtrlTreeView_Expand($hTreeView, 0, False)
EndFunc   ;==>_CollapseAll

Func _TreeViewUpdate()
        GUICtrlSetData($hStatus1, ' 请稍等!目录是只读...')
        GUICtrlSetData($hStatus2, '')
        $Timer = TimerInit()
        Local $aCount = _GUICtrlTreeView_CreateDirectory($hTreeView, $sPath, $bFiles)
        If Not @error Then
                $msg = ' 耗时: ' & Round(TimerDiff($Timer) / 1000, 3) & ' 秒'
                $msg &= '  |   ' & $aCount[1] & ' 文件 ' & $aCount[0] & ' 目录'
                GUICtrlSetData($hStatus1, ' 空闲.')
                GUICtrlSetData($hStatus2, $msg)
        EndIf
EndFunc   ;==>_TreeViewUpdate


Func _GUICtrlTreeView_CreateDirectory($hTreeView, $sPath, $bFiles = False)
        Local $hFileItem, $FolderColor = 0x0000FF, $Folder, $aCount[2] = [0, 0]
        If Not StringInStr(FileGetAttrib($sPath), 'D') Then Return SetError(1, 0, 0)
        $sPath = FileGetLongName($sPath)
        Local $oFSO = ObjCreate('Scripting.FileSystemObject')
        If @error Then Return SetError(2, 0, 0)
        _GUICtrlTreeView_BeginUpdate($hTreeView)
        _GUICtrlTreeView_DeleteAll(GUICtrlGetHandle($hTreeView))
        Local $hTreeViewItem = GUICtrlCreateTreeViewItem($sPath, $hTreeView)
        GUICtrlSetColor(-1, $FolderColor)
        _GUICtrlTreeView_SetIcon($hTreeView, $hTreeViewItem, 'shell32.dll', 0)
        $Folder = $oFSO.GetFolder($sPath)
        _CreateDirectoryRecursive($hTreeView, $Folder, $hTreeViewItem, $bFiles, $aCount)
        If $bFiles Then
                For $Files In $Folder.Files
                        $hFileItem = GUICtrlCreateTreeViewItem($Files.Name, $hTreeViewItem)
                        $aCount[1] += 1
                Next
        EndIf
        _GUICtrlTreeView_SetIcon($hTreeView, $hTreeViewItem, 'shell32.dll', 3, 2)
        _GUICtrlTreeView_SetIcon($hTreeView, $hTreeViewItem, 'shell32.dll', 110, 4)
        _GUICtrlTreeView_EndUpdate($hTreeView)
        GUICtrlSetState($hTreeViewItem, $GUI_EXPAND)
        $oFSO = ''
        Return $aCount
EndFunc   ;==>_GUICtrlTreeView_CreateDirectory

Func _CreateDirectoryRecursive($hTreeView, $Folder, $ItemOld, $bFiles, ByRef $aCount)
        Local $hFolderItem, $hFileItem, $FolderColor = 0x0000FF
        For $Subfolder In $Folder.SubFolders
                $hFolderItem = GUICtrlCreateTreeViewItem($Subfolder.Name, $ItemOld)
                GUICtrlSetColor(-1, $FolderColor)
                _GUICtrlTreeView_SetIcon($hTreeView, $hFolderItem, 'shell32.dll', 3, 2)
                _GUICtrlTreeView_SetIcon($hTreeView, $hFolderItem, 'shell32.dll', 110, 4)
                $aCount[0] += 1
                _CreateDirectoryRecursive($hTreeView, $Subfolder, $hFolderItem, $bFiles, $aCount)
                If $bFiles Then
                        For $Files In $Subfolder.Files
                                $hFileItem = GUICtrlCreateTreeViewItem($Files.Name, $hFolderItem)
                                $aCount[1] += 1
                        Next
                EndIf
        Next
EndFunc   ;==>_CreateDirectoryRecursive

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +20 收起 理由
vigiles + 20

查看全部评分

发表于 2014-5-14 20:23:46 | 显示全部楼层
顶楼主
发表于 2014-5-15 06:59:50 | 显示全部楼层
学习了。。。。。
发表于 2014-5-15 22:56:36 | 显示全部楼层
2楼的不错、可以学习一下、
发表于 2014-5-17 09:28:48 | 显示全部楼层
回复 2# lpxx


    lpxx 很好
发表于 2014-5-18 21:43:31 | 显示全部楼层
2楼好给力,谢谢
发表于 2014-5-19 08:04:25 | 显示全部楼层
这个好学习一下
发表于 2014-5-19 08:57:28 | 显示全部楼层
感谢二楼,这个很好
发表于 2014-5-19 18:29:04 | 显示全部楼层
回复 2# lpxx

谢谢分享,学习了
发表于 2014-5-19 21:45:33 | 显示全部楼层
回复 2# lpxx
谢谢分享!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2014-5-19 22:04:05 | 显示全部楼层
不错,学习了,感谢感谢
发表于 2014-5-20 15:26:39 | 显示全部楼层
这个好学习一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-17 04:58 , Processed in 0.148777 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表