找回密码
 加入
搜索
查看: 3484|回复: 8

[GUI管理] 如何做类似于文件资源管理器的管理软件,谢谢

  [复制链接]
发表于 2015-11-9 10:42:57 | 显示全部楼层 |阅读模式
大家好,请问如何做类似于 文件资源管理器 的软件
大致能想到的思路是:
1:建立一个treeview控件
2:使用_FileListToArray 得到文件路径的数组清单
3:GUICtrlCreateTreeViewItem在treeview里根据得到的数组清单建立  项目
4:当选中 treeview里的项目时,再使用GUICtrlCreateTreeViewItem建立当前选中项目下的文件

现在的难点在于如何方便判断当前 选中 项目的文件路径
并且如果选中就运行GUICtrlCreateTreeViewItem建立当前文件路径

请帮忙,谢谢。
发表于 2015-11-9 11:00:04 | 显示全部楼层
创建TreeView时同时保存与索引相对应的路径数组
选中项目时直接取数组路径
 楼主| 发表于 2015-11-9 11:46:35 | 显示全部楼层
回复 2# afan


    版主,有相关例子吗,谢谢。
发表于 2015-11-9 13:14:52 | 显示全部楼层
回复 3# light_he


    没有注意过…
不过,这应该是很简单的,你有试过吗
 楼主| 发表于 2015-11-9 14:08:37 | 显示全部楼层
有的,比如在论坛里找的例子,原style 是checkbox
如果要有类似于windows资源管理器的效果,是没有checkbox的

如果取消checkbox,如何判断当前item是否被选中?
如果判断item被选中了,在不运行button的情况下,运行相关函数?

再谢谢了
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Local $sTest
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
$hGUI = GUICreate("Treeview", 400, 300)
$Treeview = GUICtrlCreateTreeView(2, 2, 180, 294, $iStyle, $WS_EX_CLIENTEDGE)
$Fonts1 = GUICtrlCreateTreeViewItem("项目1", $Treeview)
GUICtrlCreateTreeViewItem("项目1-1", $Fonts1)
GUICtrlCreateTreeViewItem("项目1-2", $Fonts1)
GUICtrlCreateTreeViewItem("项目1-3", $Fonts1)
GUICtrlSetState($Fonts1, $GUI_EXPAND)
$Fonts2 = GUICtrlCreateTreeViewItem("项目2", $Treeview)
GUICtrlCreateTreeViewItem("项目2-1", $Fonts2)
GUICtrlCreateTreeViewItem("项目2-2", $Fonts2)
GUICtrlCreateTreeViewItem("项目2-3", $Fonts2)
GUICtrlSetState($Fonts2, $GUI_EXPAND)
$Button1 = GUICtrlCreateButton("获取", 264, 96, 75, 25, $WS_GROUP)
GUISetState()
 
While 1
        Switch GUIGetMsg()
                Case - 3
                        Exit
                Case $Button1 
                        $sTest = ''
                        For $i = 4 To 11
                                If BitAND(GUICtrlRead($i), $GUI_CHECKED) Then $sTest &= _GUICtrlTreeView_GetText($Treeview, $i) & @CRLF
                        Next
                        MsgBox(0, '被选项目文本', $sTest)
        EndSwitch
WEnd
发表于 2015-11-9 15:21:50 | 显示全部楼层
本帖最后由 80107671 于 2015-11-9 15:22 编辑

这个相信就是你想要的! 你可以在树形结构上加入鼠标的双击。将C:\myfile改成你的路径。
#include <GUIConstantsEx.au3>
#include <GUIImageList.au3>
#include <GUITreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

Global $hForm, $hTreeView, $hImageList, $hItem = 0, $hSelect = 0, $hInput, $Input, $Dummy1, $Dummy2
Global $X, $Y, $sPath, $sRoot = "c:\myfile"

$hForm = GUICreate('MyGUI', 600, 600)
$Input = GUICtrlCreateInput('', 20, 20, 560, 19)
$hInput = GUICtrlGetHandle(-1)
GUICtrlSetState(-1, $GUI_DISABLE)
;$hTreeView = _GUICtrlTreeView_Create($hForm, 20, 50, 560, 530, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
;_GUICtrlTreeView_SetHeight($hTreeView, 18)
GUICtrlCreateTreeView(20, 50, 560, 530, -1, $WS_EX_CLIENTEDGE)
$hTreeView = GUICtrlGetHandle(-1)
$Dummy1 = GUICtrlCreateDummy()
$Dummy2 = GUICtrlCreateDummy()

If _WinAPI_GetVersion() >= '6.0' Then
        _WinAPI_SetWindowTheme($hTreeView, 'Explorer')
EndIf

$hImageList = _GUIImageList_Create(16, 16, 5, 1)
_GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 3)
_GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 4)
_GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 51)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImageList)
$sRoot = StringRegExpReplace(FileGetLongName($sRoot), '\\+\Z', '')
$sPath = StringRegExpReplace($sRoot, '^.*\\', '')
If StringInStr($sPath, ':') Then
        $sRoot &= '\'
        $sPath &= '\'
EndIf

If _WinAPI_PathIsDirectory($sRoot) Then
        $hItem = _GUICtrlTreeView_AddChild($hTreeView, 0, $sPath, 0, 0)
        If FileClose(FileFindFirstFile($sRoot & '\*')) Then
                _GUICtrlTreeView_AddChild($hTreeView, $hItem, '', 2, 2)
        EndIf
EndIf

GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()

If $hItem Then
        _GUICtrlTreeView_Expand($hTreeView, $hItem)
EndIf

While 1
        Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Dummy1 ; Update
                        GUISetCursor(1, 1)
                        _TVUpdate($hTreeView, GUICtrlRead($Dummy1))
                        GUISetCursor(2, 0)
                Case $Dummy2 ; Menu
                        $hItem = GUICtrlRead($Dummy2)
                        $sPath = _TVGetPath($hTreeView, $hItem, $sRoot)

                        ConsoleWrite('-------------------------------' & @CR)
                        ConsoleWrite('Handle: ' & $hItem & @CR)
                        ConsoleWrite('Path:   ' & $sPath & @CR)
                        If _WinAPI_PathIsDirectory($sPath) Then
                                ConsoleWrite('Type:   ' & 'Directory' & @CR)
                        Else
                                ConsoleWrite('Type:   ' & 'File' & @CR)
                        EndIf
                        ConsoleWrite('X:      ' & MouseGetPos(0) & @CR)
                        ConsoleWrite('Y:      ' & MouseGetPos(1) & @CR)
                        ConsoleWrite('-------------------------------' & @CR)

        EndSwitch
WEnd

Func _TVGetPath($hTV, $hItem, $sRoot)

        Local $Path = StringRegExpReplace(_GUICtrlTreeView_GetTree($hTV, $hItem), '([|]+)|(\\[|])', '\\')

        If Not $Path Then
                Return ''
        EndIf
        If Not StringInStr($Path, ':') Then
                Return StringRegExpReplace($sRoot, '(\\[^\\]*(\\|)+)\Z', '\\') & $Path
        EndIf
        Return $Path
EndFunc   ;==>_TVGetPath

Func _TVSetPath($hTV, $hItem, $sRoot)
        GUICtrlSetData($Input, _WinAPI_PathCompactPath($hInput, _TVGetPath($hTV, $hItem, $sRoot), 554))
        $hSelect = $hItem
EndFunc   ;==>_TVSetPath

Func _TVUpdate($hTV, $hItem)

        Local $hImageList = _SendMessage($hTV, $TVM_GETIMAGELIST)
        Local $Path = StringRegExpReplace(_TVGetPath($hTV, $hItem, $sRoot), '\\+\Z', '')
        Local $hIcon, $hSearch, $hSubitem
        Local $Index, $File

        _WinAPI_LockWindowUpdate($hTV)
        _GUICtrlTreeView_Delete($hTV, _GUICtrlTreeView_GetFirstChild($hTV, $hItem))

        $hSearch = FileFindFirstFile($Path & '\*')
        If $hSearch = -1 Then
                If Not @error Then
                        If FileExists($Path) Then
                                ;               If _WinAPI_PathIsDirectory($Path) Then
                                ;                   ; Access denied
                                ;               EndIf
                        Else
                                _GUICtrlTreeView_Delete($hTV, $hItem)
                                _WinAPI_LockWindowUpdate(0)
                                Return 0
                        EndIf
                EndIf
        Else
                While 1
                        $File = FileFindNextFile($hSearch)
                        If @error Then
                                ExitLoop
                        EndIf
                        If @extended Then
                                $hSubitem = _GUICtrlTreeView_AddChild($hTV, $hItem, $File, 0, 0)
                                If FileClose(FileFindFirstFile($Path & '\' & $File & '\*')) Then
                                        _GUICtrlTreeView_AddChild($hTV, $hSubitem, '', 2, 2)
                                EndIf
                        EndIf
                WEnd
                FileClose($hSearch)
        EndIf

        $hSearch = FileFindFirstFile($Path & '\*')
        If $hSearch = -1 Then

        Else
                While 1
                        $File = FileFindNextFile($hSearch)
                        If @error Then
                                ExitLoop
                        EndIf
                        If Not @extended Then
                                $hIcon = _WinAPI_ShellExtractAssociatedIcon($Path & '\' & $File, 1)
                                $Index = _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon)
                                _GUICtrlTreeView_AddChild($hTV, $hItem, $File, $Index, $Index)
                                _WinAPI_DestroyIcon($hIcon)
                        EndIf
                WEnd
                FileClose($hSearch)
        EndIf

        _GUICtrlTreeView_SetItemParam($hTV, $hItem, 0x7FFFFFFF)
        _WinAPI_LockWindowUpdate(0)

        Return 1
EndFunc   ;==>_TVUpdate


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

        Local $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $lParam)
        Local $hItem = DllStructGetData($tNMTREEVIEW, 'NewhItem')
        Local $iState = DllStructGetData($tNMTREEVIEW, 'NewState')
        Local $hTV = DllStructGetData($tNMTREEVIEW, 'hWndFrom')
        Local $ID = DllStructGetData($tNMTREEVIEW, 'Code')
        Local $tTVHTI, $tPoint

        Switch $hTV
                Case $hTreeView
                        Switch $ID
                                Case $TVN_ITEMEXPANDEDW
                                        If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then
                                                _GUICtrlTreeView_Delete($hTV, $hItem)
                                                If BitAND($iState, $TVIS_SELECTED) Then
                                                        _TVSetPath($hTV, _GUICtrlTreeView_GetSelection($hTV), $sRoot)
                                                EndIf
                                        Else
                                                If Not BitAND($iState, $TVIS_EXPANDED) Then
                                                        _GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 0)
                                                        _GUICtrlTreeView_SetImageIndex($hTV, $hItem, 0)
                                                Else
                                                        _GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 1)
                                                        _GUICtrlTreeView_SetImageIndex($hTV, $hItem, 1)
                                                        If Not _GUICtrlTreeView_GetItemParam($hTV, $hItem) Then
                                                                GUICtrlSendToDummy($Dummy1, $hItem)
                                                        EndIf
                                                EndIf
                                        EndIf
                                Case $TVN_SELCHANGEDW
                                        If BitAND($iState, $TVIS_SELECTED) Then
                                                If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then
                                                        _GUICtrlTreeView_Delete($hTV, $hItem)
                                                        $hItem = _GUICtrlTreeView_GetSelection($hTV)
                                                EndIf
                                                If $hItem <> $hSelect Then
                                                        _TVSetPath($hTV, $hItem, $sRoot)
                                                EndIf
                                        EndIf
                                Case $NM_RCLICK
                                        $tPoint = _WinAPI_GetMousePos(1, $hTV)
                                        $tTVHTI = _GUICtrlTreeView_HitTestEx($hTV, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2))
                                        $hItem = DllStructGetData($tTVHTI, 'Item')
                                        If BitAND(DllStructGetData($tTVHTI, 'Flags'), $TVHT_ONITEM) Then
                                                _GUICtrlTreeView_SelectItem($hTreeView, $hItem)
                                                If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then
                                                        _GUICtrlTreeView_Delete($hTV, $hItem)
                                                        $hItem = _GUICtrlTreeView_GetSelection($hTV)
                                                Else
                                                        GUICtrlSendToDummy($Dummy2, $hItem)
                                                EndIf
                                                If $hItem <> $hSelect Then
                                                        _TVSetPath($hTV, $hItem, $sRoot)
                                                EndIf
                                        EndIf
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
 楼主| 发表于 2015-11-9 16:45:10 | 显示全部楼层
回复 6# 80107671


    太感谢了,需要的功能都能实现
代码有点复杂,我先学习,谢啦。
发表于 2015-11-10 13:28:19 | 显示全部楼层
回复 6# 80107671



你好,麻烦帮看看这个帖子的问题要怎么解决?


http://www.autoitx.com/forum.php ... id=50325&extra=
发表于 2018-6-11 23:24:02 | 显示全部楼层
多谢楼主提供好源码
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-3 17:34 , Processed in 0.084095 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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