找回密码
 加入
搜索
查看: 3320|回复: 2

[GUI管理] 能不能请大伙帮忙看下我这程序还要怎么优化?

[复制链接]
发表于 2011-1-21 09:45:10 | 显示全部楼层 |阅读模式
本帖最后由 yarsye 于 2011-1-21 13:24 编辑

实现的功能就是在服务器和本地机文件互拷 , 功能简单 ,只是练手,欢迎大伙拍砖指点。
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GUIEdit.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
Global $txt1 , $txt2 
Global $CopyIni
Global Const $FO_COPY = 0x0002
Global Const $FOF_ALLOWUNDO = 0x0040

#Region ### START Koda GUI section ### Form=d:\4 study\autoit\code\ctl file copying utility\form2.kxf
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
$Gui = GUICreate("Server File Copying Utility", 619, 590, 249, 122,$WS_OVERLAPPEDWINDOW)
GUISetBkColor(0x99B4D1)
$hTreeView1 = GUICtrlCreateTreeView(0, 140, 289, 250, $iStyle, $WS_EX_CLIENTEDGE)
$hTreeView2 = GUICtrlCreateTreeView(312, 140, 290, 250, $iStyle, $WS_EX_CLIENTEDGE)
$Button1 = GUICtrlCreateButton("Open", 4, 89, 31, 35)
$Button2 = GUICtrlCreateButton(">>", 260, 89, 31, 35);, $iStyle);, $WS_EX_CLIENTEDGE)
$Button3 = GUICtrlCreateButton("Open", 565, 89, 31, 35 )
$Button4 = GUICtrlCreateButton("<<", 317, 89, 31, 35 );,$TVS_CHECKBOXES);, $WS_EX_CLIENTEDGE)
$Button5 = GUICtrlCreateButton('Create Dir',104,89,71,35)
$Button6 = GUICtrlCreateButton('Create Dir',417,89,71,35)
$hImage = _GUIImageList_Create(16, 16, 5, 2)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 4)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 54) 
_GUICtrlTreeView_SetNormalImageList($hTreeView1, $hImage)
_GUICtrlTreeView_SetNormalImageList($hTreeView2, $hImage)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

$Input1 = GUICtrlCreateInput("Please select your server from Network on Desktop", 56, 48, 233, 21)
$Input2 = GUICtrlCreateInput("Please select your local system", 376, 48, 225, 21)
$Label1 = GUICtrlCreateLabel("Server", 8, 8, 67, 17)
$Label2 = GUICtrlCreateLabel("Local", 320, 8, 43, 17)
$Label3 = GUICtrlCreateLabel("Locate", 8, 48, 45, 17)
$Label4 = GUICtrlCreateLabel("Locate", 320, 48, 45, 17)
$Edit1 = GUICtrlCreateEdit("", 0, 432, 609, 129);,BitOR($ES_MULTILINE, $ES_READONLY, $WS_VSCROLL))
GUICtrlSetData(-1, "")
$Log = GUICtrlCreateLabel("Log", 0, 408, 22, 17)
GUISetState(@SW_SHOW)
;~ GUISetState(@SW_MAXIMIZE) 
$folder = DriveGetDrive("all")
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case -3
                        Exit
                Case $Button1
                        $Button1FileOpenDialog = FileSelectFolder('Select the Files in your server', '', 5)
                        GUICtrlSetData($Input1,$Button1FileOpenDialog)
                        $StringSplitForButton1 = StringSplit($Button1FileOpenDialog,'\',1)
                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1, "", $Button1FileOpenDialog,0);$StringSplitForButton1[UBound($StringSplitForButton1)-1], 0)
                        _GUICtrlTreeView_Delete($hTreeView1)
                        _GUICtrlEdit_AppendText($Edit1,$Button1FileOpenDialog & @LF)
                Case $Button2
                        
                        If StringInStr(FileGetAttrib($txt1),'D') Then;And StringInStr(FileGetAttrib($txt2),'D')Then ;如果2边都是文件夹 ,直接复制整个文件夹
                                If StringInStr(FileGetAttrib($txt2),'D') Then
                                        _ExplorerCopy($txt1,$txt2 & '\' )
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView2)
                                        Sleep(500)
                                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2,'',$txt2,0)
                                Else
                                        Dim $szDrive, $szDir, $szFName, $szExt
                                        $SplitPath1 = _PathSplit($txt2,$szDrive, $szDir, $szFName, $szExt)
                                        _ExplorerCopy($txt1,$SplitPath1[1] & $SplitPath1[2])
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView2)
                                        Sleep(500)
                                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2,'',$txt2,0)
                                EndIf
                        Else
                                If StringInStr(FileGetAttrib($txt2),'D') Then
                                        _ExplorerCopy($txt1,$txt2 & '\')
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView2)
                                        Sleep(500)
                                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2,'',$txt2,0)
                                Else
                                        Dim $szDrive, $szDir, $szFName, $szExt
                                        $SplitPath1 = _PathSplit($txt2,$szDrive, $szDir, $szFName, $szExt)
                                        _ExplorerCopy($txt1,$SplitPath1[1] & $SplitPath1[2])
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView2)
                                        Sleep(500)
                                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2,'',$txt2,0)                                        
                                EndIf
                        EndIf
                        
                Case $Button3
                        $Button3FileOpenDialog = FileSelectFolder('Select the Files in your server', '', 5)
                        GUICtrlSetData($Input2,$Button3FileOpenDialog)
                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2, "", $Button3FileOpenDialog, 0)
                        _GUICtrlTreeView_Delete($hTreeView2)
                        _GUICtrlEdit_AppendText($Edit1,$Button3FileOpenDialog & @LF)
                Case $Button4
                        
                        If StringInStr(FileGetAttrib($txt2),'D') Then
                                If StringInStr(FileGetAttrib($txt1),'D') Then
                                        _ExplorerCopy($txt2,$txt1 & '\' )
                                        _CopyTxt($txt1,$txt2)
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView1)
                                        Sleep(500)
                                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1,'',$txt1,0)
                                Else
                                        Dim $szDrive, $szDir, $szFName, $szExt
                                        $SplitPath2 = _PathSplit($txt1,$szDrive, $szDir, $szFName, $szExt)
                                        _ExplorerCopy($txt2,$SplitPath1[1] & $SplitPath1[2])
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView1)
                                        Sleep(500)
                                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1,'',$txt1,0)
                                EndIf
                        Else
                                If StringInStr(FileGetAttrib($txt1),'D') Then
                                        _ExplorerCopy($txt2,$txt1 & '\')
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView1)
                                        Sleep(500)
                                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1,'',$txt1,0)
                                Else
                                        Dim $szDrive, $szDir, $szFName, $szExt
                                        $SplitPath2 = _PathSplit($txt1,$szDrive, $szDir, $szFName, $szExt)
                                        _ExplorerCopy($txt2,$SplitPath1[1] & $SplitPath1[2])
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView1)
                                        Sleep(500)
                                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1,'',$txt1,0)                                        
                                EndIf
                        EndIf
                Case $Button5
                        If $txt1 <> '' Then
                                If StringInStr(FileGetAttrib($txt1),'D') Then
                                        _FileAndDirCreate($txt1 & '\New Folder')
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView1)
                                        Sleep(500)
                                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1,'',$txt1,0)        
;~                                         _GUICtrlTreeView_Expand($hTreeView1)
                                Else
                                        Dim $szDrive, $szDir, $szFName, $szExt
                                        $SplitPath3 = _PathSplit($txt1,$szDrive, $szDir, $szFName, $szExt)
                                        _FileAndDirCreate($SplitPath3[1] & $SplitPath3[2] & 'New Folder')
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView1)
                                        Sleep(500)
                                        $root1 = _GUICtrlTreeView_AddChild($hTreeView1,'',$txt1,0)                                        
                                EndIf
                        EndIf
                Case $Button6
                        If $txt2 <> '' Then
                                If StringInStr(FileGetAttrib($txt2),'D') Then
                                        _FileAndDirCreate($txt2 & '\New Folder')
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView2)
                                        Sleep(500)
                                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2,'',$txt2,0)        
;~                                         _GUICtrlTreeView_Expand($hTreeView2)
                                Else
                                        Dim $szDrive, $szDir, $szFName, $szExt
                                        $SplitPath4 = _PathSplit($txt2,$szDrive, $szDir, $szFName, $szExt)
                                        _FileAndDirCreate($SplitPath4[1] & $SplitPath4[2] & 'New Folder')
                                        Sleep(500)
                                        _GUICtrlTreeView_Delete($hTreeView2)
                                        Sleep(500)
                                        $root2 = _GUICtrlTreeView_AddChild($hTreeView2,'',$txt2,0)        
;~                                         _GUICtrlTreeView_Expand($hTreeView2)
                                EndIf
                        EndIf
        EndSwitch
WEnd
;=====================Functions below====================

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
        Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR 
        Local $i = 1
        $hWndTreeView = GUICtrlGetHandle($hTreeView1)
        $hWndTreeView2 = GUICtrlGetHandle($hTreeView2)
        $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
        $iCode = DllStructGetData($tNMHDR, "Code")
        Switch $hWndFrom
                Case $hWndTreeView
                        Switch $iCode
                                Case -451
                                        $item = _GUICtrlTreeView_GetSelection($hWndTreeView)
                                        $root = $item
                                        If _GUICtrlTreeView_GetChildCount($hWndTreeView, $item) <= 0 Then
                                                Global $txt1
                                                $txt1 = _GUICtrlTreeView_GetText($hWndTreeView, $item)
                                                Do
                                                        $parent = _GUICtrlTreeView_GetParentHandle($hWndTreeView, $item)
                                                        If $parent <> 0 Then
                                                                $txt1 = _GUICtrlTreeView_GetText($hWndTreeView, $parent) & "" & $txt1
                                                                $item = $parent
;=====================set checkbox checked
                                                                $inireadfolder = IniReadSection(@desktopdir & '\test.ini','folderitem')
                                                                If @error Then 
                                                                        Exit
                                                                Else
                                                                        For $j = 1 To $inireadfolder[0][0]
                                                                                If _GUICtrlTreeView_GetChecked($hWndTreeView,$inireadfolder[$j][1]) Then
;~                                                                                         MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$inireadfolder[$j][1]' & @lf & @lf & 'Return:' & @lf & $inireadfolder[$j][1] & @lf & @lf & '@Error:' & @lf & @Error) ;### Debug MSGBOX
;~                                                                                         MsgBox(0,1,1)
                                                                                        
                                                                                        $inireadfile = IniReadSection(@desktopdir & '\test.ini','file')
                                                                                        If @error Then 
                                                                                                Exit
                                                                                        Else
                                                                                                For $k = 1 To $inireadfile[0][0]
                                                                                                        _GUICtrlTreeView_SetChecked($hWndTreeView,$inireadfile[$k][1])
                                                                                                Next
                                                                                        EndIf
                                                                                EndIf
                                                                        Next
                                                                EndIf
;====================end here
                                                        EndIf                                                        
                                                        Until $parent = 0
                                                _GUICtrlEdit_AppendText($Edit1,$txt1 & @LF)
                                                _SearchFolder($txt1, $root)
                                        EndIf
                        EndSwitch
                Case $hWndTreeView2
                        Switch $iCode
                                Case -451
                                        $item = _GUICtrlTreeView_GetSelection($hWndTreeView2)
                                        $root = $item
                                        If _GUICtrlTreeView_GetChildCount($hWndTreeView2, $item) <= 0 Then
                                                Global $txt2
                                                $txt2 = _GUICtrlTreeView_GetText($hWndTreeView2, $item)
                                                Do
                                                        $parent = _GUICtrlTreeView_GetParentHandle($hWndTreeView2, $item)
                                                        If $parent <> 0 Then
                                                                $txt2 = _GUICtrlTreeView_GetText($hWndTreeView2, $parent) & "" & $txt2
                                                                $item = $parent
                                                        EndIf
                                                Until $parent = 0
;~                                                 MsgBox(0,1,$txt)
;~                                                 FileCopy($txt,@DesktopDir)
                                                _SearchFolder($txt2, $root)
                                        EndIf
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Func _SearchFolder($folder, $parent, $level = 0)
        If $level >= 1 Then Return
        $files = _FileListToArray($folder, "*", 1)
        $folders = _FileListToArray($folder, "*", 2)
        _FolderFunc($folders, $folder, $parent, $level)
        _FileFunc($files, $parent)
EndFunc   ;==>_SearchFolder

Func _FileFunc($files, $parent)
        For $i = 1 To UBound($files) - 1
                $fileitem = _GUICtrlTreeView_AddChild($hTreeView1, $parent, $files[$i], 1, 1)
                IniWrite(@DesktopDir & '\test.ini','file','key' & $i ,$fileitem)
        Next
EndFunc   ;==>_FileFunc

Func _FolderFunc($folders, $folder, $parent, $level)
        For $i = 1 To UBound($folders) - 1
                $folderitem = _GUICtrlTreeView_AddChild($hTreeView1, $parent, $folders[$i], 0)
                IniWrite(@DesktopDir & '\test.ini','folderitem','key'&$i,$folderitem)
                _SearchFolder($folder & "" & $folders[$i], $folderitem, $level + 1)
        Next
EndFunc   ;==>_FolderFunc

Func _ExplorerCopy($source, $dest)

    Local $SHFILEOPSTRUCT, $source_struct, $dest_struct
 
    $SHFILEOPSTRUCT = DllStructCreate("hwnd hWnd;uint wFunc;ptr pFrom;ptr pTo;int fFlags;" & _
                                      "int fAnyOperationsAborted;ptr hNameMappings;ptr lpszProgressTitle")
 
    $source_struct = DllStructCreate("char[" & StringLen($source) + 2 & "]")
    DllStructSetData($source_struct, 1, $source)
    DllStructSetData($source_struct, 1, 0, StringLen($source) + 2)
 
    $dest_struct = DllStructCreate("char[" & StringLen($dest) + 2 & "]")
    DllStructSetData($dest_struct, 1, $dest)
    DllStructSetData($dest_struct, 1, 0, StringLen($dest) + 2)
 
    DllStructSetData($SHFILEOPSTRUCT, "hWnd", 0)
    DllStructSetData($SHFILEOPSTRUCT, "wFunc", $FO_COPY)
    DllStructSetData($SHFILEOPSTRUCT, "pFrom", DllStructGetPtr($source_struct))
    DllStructSetData($SHFILEOPSTRUCT, "pTo", DllStructGetPtr($dest_struct))
    DllStructSetData($SHFILEOPSTRUCT, "fFlags", $FOF_ALLOWUNDO)
 
    DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
EndFunc   ;==>_ExplorerCopy

Func _CopyTxt($path,$file)
        FileWriteLine($path & '\Copy.txt','[Computer Name]: '&@ComputerName&@CRLF _
                                                                           &'[User Name]: '&@UserName&@CRLF _
                                                                           &'[IP Address]: '&@IPAddress1&@CRLF _
                                                                           &'[OS Version]: '&@OSVersion&@OSArch&@CRLF _
                                                                           &'[Time Stamp]: '&@YEAR&'/'&@MON&'/'&@MDAY&'/'&@HOUR&':'&@MIN&':'&@SEC &@CRLF _
                                                                           &'[File Location]: '&$file)
EndFunc
                                                                   
Func _FileAndDirCreate($FilePach)
        Dim $szDrive, $szDir, $szFName, $szExt
        $arrPath = _PathSplit($FilePach, $szDrive, $szDir, $szFName, $szExt)
        If Not FileExists($FilePach) Then
                If $szExt = '' Then
                        DirCreate($FilePach)
                Else
                        _FileCreate($FilePach)
                EndIf
        Else
                For $i = 1 To 1000
                        If Not FileExists($szDrive & $szDir & $szFName & '(' & $i & ')' & $szExt) Then ExitLoop
                Next
                If $szExt = '' Then
                        DirCreate($szDrive & $szDir & $szFName & '(' & $i & ')')
                Else
                        _FileCreate($szDrive & $szDir & $szFName & '(' & $i & ')' & $szExt)
                EndIf
        EndIf
EndFunc   ;==>_FileAndDirCreate
                                                                                                                                      
发表于 2011-1-21 11:01:27 | 显示全部楼层
回复 1# yarsye

If StringInStr(FileGetAttrib($txt1),'D') Then
要是检查文件夹的话可以这样
If FileGetAttrib($txt1) = 'D' Then
声明变量 Dim $szDrive, $szDir, $szFName, $szExt
这个代码的循环里声明六次。

在GUISetState(@SW_SHOW)之下声明一次就可以了。
While里的都可以去掉,但用户定义函数里的不能去掉。

评分

参与人数 1金钱 +10 收起 理由
yarsye + 10 谢谢指点

查看全部评分

 楼主| 发表于 2011-1-26 18:08:50 | 显示全部楼层
问题大了 太复杂了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-21 04:29 , Processed in 0.083906 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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