(已解决)数组维数报错!
本帖最后由 yarsye 于 2010-12-31 17:47 编辑以下是解决好的代码 以前的数组越位了 改成$array2[$i+1]里面加上个1就OK 了
谢谢各位了
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#include <array.au3>
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
Global $array1, $array2, $array3, $inipath, $inivalue, $readini
$inipath = @DesktopDir & '\test.ini'
$inivalue = ''
$GUI = GUICreate("(UDF Created) TreeView Create", 400, 640)
$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 568, $iStyle, $WS_EX_CLIENTEDGE)
Recursion(@ProgramFilesDir, '')
GUISetState()
$readini = IniReadSection($inipath , 'section1')
If @error Then
MsgBox(4096, "", "错误, 不是 INI 文静.")
Else
For $k = 1 To $readini
MsgBox(0,1,'key'&$readini[$i] & @CRLF & 'value' & $readini[$i])
Next
EndIf
Do
Until GUIGetMsg() = -3
Func Recursion($current, $flag)
Local $i = 0, $j = 0
If Not FileExists($inipath) Then
IniWrite($inipath, 'section', 'key', 1)
$inivalue = IniRead($inipath, 'section', 'key', '')
Else
$inivalue = IniRead($inipath, 'section', 'key', '')
$inivalue += 1
IniWrite($inipath, 'section', 'key', $inivalue)
$inivalue = IniRead($inipath, 'section', 'key', '')
EndIf
If $flag = '' Then $item1 = GUICtrlCreateTreeViewItem($current, $hTreeView)
Local $search = FileFindFirstFile($current & "\*.*")
While 1
Dim $file = FileFindNextFile($search)
If @error Or StringLen($file) < 1 Then ExitLoop
If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
If $flag <> '' Then $item1 = $flag
$item2 = GUICtrlCreateTreeViewItem($file, $item1)
$i += 1
ReDim $array2[$i + 1]
$array2[$i] = $item2
IniWrite($inipath, 'section1', 'key' & $i, $array2[$i])
Recursion($current & "\" & $file, $item2)
Else
If $flag <> '' Then $item1 = $flag
$item3 = GUICtrlCreateTreeViewItem($file, $item1)
$j += 1
ReDim $array3[$j + 1]
$array3[$j] = $item3
IniWrite($inipath, 'section2', 'key' & $j, $array3[$j])
EndIf
WEnd
Return
EndFunc ;==>Recursion
回复 1# yarsye #include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#include <array.au3>
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
Global $item1 , $item2 , $item3 , $file , $inivalue , $inipath
$inipath = @DesktopDir & '\test.ini'
$inivalue = ''
$GUI = GUICreate("(UDF Created) TreeView Create", 400, 640)
$hTreeView = GUICtrlCreateTreeView( 2, 2, 396, 568,$iStyle, $WS_EX_CLIENTEDGE)
Recursion(@ProgramFilesDir,'')
GUISetState()
Do
Until GUIGetMsg() = -3
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case - 3
Exit
;~ Case $aFitem To $aFitem ;父项目
;~ If BitAND(GUICtrlRead($nMsg), $GUI_CHECKED) Then ;如果父项目勾选,其下所有子项目全选。
;~ $iSitemNumber = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $nMsg))
;~ For $i = $nMsg To $nMsg + $iSitemNumber
;~ GUICtrlSetState($i, $GUI_CHECKED)
;~ Next
;~ Else
;~ $iSitemNumber = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $nMsg))
;~ For $i = $nMsg To $nMsg + $iSitemNumber
;~ GUICtrlSetState($i, $GUI_UNCHECKED)
;~ Next
;~ EndIf
;~ ContinueCase
;~ Case $it
;~ $sText = ''
;~ For $i = 0 To UBound($aSitem) - 1
;~ If _GUICtrlTreeView_GetChecked($TreeView1, $aSitem[$i]) Then
;~ $sText &= '被选项目句柄:' & $aSitem[$i] & ' ' & '项目文本:' & $aSitem[$i] & @CRLF
;~ EndIf
;~ Next
;~ If $sText <> '' Then MsgBox(0, '获取被选项目句柄+项目文本', $sText)
EndSwitch
WEnd
Func Recursion($current, $flag)
If Not FileExists($inipath) Then
IniWrite($inipath,'section','key',3)
$inivalue = 3
Else
$inivalue = IniRead($inipath,'section','key','')
$inivalue +=1
IniWrite($inipath,'section','key',$inivalue)
EndIf
If $flag = '' Then
ReDim $item1[$inivalue]
$item1 = GUICtrlCreateTreeViewItem($current,$hTreeView); _GUICtrlTreeView_Add($hTreeView, 0, $current)
EndIf
Local $search = FileFindFirstFile($current & "\*.*")
While 1
Dim $file = FileFindNextFile($search)
If @error Or StringLen($file) < 1 Then ExitLoop
If StringInStr(FileGetAttrib($current & "\" & $file), "D") And($file <> "." Or $file <> "..") Then
If $flag <> '' Then
ReDim $item1[$inivalue]
$item1[$inivalue] = $flag
ReDim $item2[$inivalue]
$item2[$inivalue] = GUICtrlCreateTreeViewItem($file , $item1[$inivalue])
EndIf
_ArrayDisplay($item2)
MsgBox(0,"l",$inivalue)
Recursion($current & "\" & $file, $item2[$inivalue])
Else
If $flag <> '' Then
ReDim $item1[$inivalue]
$item1[$inivalue] = $flag
ReDim $item3[$inivalue]
$item3[$inivalue]= GUICtrlCreateTreeViewItem($file , $item1[$inivalue])
EndIf
EndIf
WEnd
Return
EndFunc ;==>Recursion
;~ #include <GuiConstantsEx.au3>
;~ #include <GuiTreeView.au3>
;~ #include <WindowsConstants.au3>
;~ #include <EditConstants.au3>
;~ #include <file.au3>
;~ #include <array.au3>
;~ Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
;~ $GUI = GUICreate("(UDF Created) TreeView Create", 400, 640)
;~ $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 396, 568,$iStyle, $WS_EX_CLIENTEDGE)
;~ _GUICtrlTreeView_BeginUpdate($hTreeView)
;~ Recursion(@AppDataCommonDir, '')
;~ _GUICtrlTreeView_EndUpdate($hTreeView)
;~ GUISetState()
;~ Do
;~ Until GUIGetMsg() = -3
;~ Func Recursion($current, $flag)
;~ If $flag = '' Then $item1 = _GUICtrlTreeView_Add($hTreeView, 0, $current)
;~ Local $search = FileFindFirstFile($current & "\*.*")
;~ While 1
;~ Dim $file = FileFindNextFile($search)
;~ If @error Or StringLen($file) < 1 Then ExitLoop
;~ If StringInStr(FileGetAttrib($current & "\" & $file), "D") And($file <> "." Or $file <> "..") Then
;~ If $flag <> '' Then $item1 = $flag
;~ $item2 = _GUICtrlTreeView_AddChild($hTreeView, $item1, $current & '\' & $file)
;~ Recursion($current & "\" & $file, $item2)
;~ Else
;~ If $flag <> '' Then $item1 = $flag
;~ $item3 = _GUICtrlTreeView_AddChild($hTreeView, $item1, $current & '\' & $file)
;~ EndIf
;~ WEnd
;~ Return
;~ EndFunc ;==>Recursion 回复 2# lainline
提示81行数组越界 ReDim后原来数组里的数值就不存在了,你应该是插入或添加.
btw:像这样一大堆的代码,没几个人有空帮你去分析,你应该将问题简化出来. 问题是 我想在那个递归里面 每调用一次就把数组的值加一 他现在老是提示我数组越界
正在把问题简化 谢谢提醒 做资源管理器?
如果是,试试这个
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiTreeView.au3>
#Include <File.au3>
$gui = GUICreate("File Browser", 362, 378, -1, -1)
$tree = GUICtrlCreateTreeView(0, 0, 361, 377)
GUICtrlSetFont(-1,10)
GUICtrlSetColor($tree, 0x00FF00)
GUICtrlSetBkColor($tree,0x000000)
$hImage = _GUIImageList_Create(16, 16, 5, 2)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 4)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 54)
_GUICtrlTreeView_SetNormalImageList($tree, $hImage)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$root = _GUICtrlTreeView_AddChild($tree,"","C:\",0)
$root = _GUICtrlTreeView_AddChild($tree,"","D:\",0)
GUISetState(@SW_SHOW)
$folder = DriveGetDrive( "all" )
While 1
$msg = GUIGetMsg()
If $msg= -3 Then ExitLoop
WEnd
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
Func _FileFunc($files,$parent)
For $i = 1 To UBound($files)-1
_GUICtrlTreeView_AddChild($tree,$parent,$files[$i],1,1)
Next
EndFunc
Func _FolderFunc($folders,$folder,$parent,$level)
For $i = 1 To UBound($folders)-1
$parentitem = _GUICtrlTreeView_AddChild($tree,$parent,$folders[$i],0)
_SearchFolder($folder & "\" & $folders[$i],$parentitem,$level+1)
Next
EndFunc
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
$hWndTreeView = GUICtrlGetHandle($tree)
$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
$txt = _GUICtrlTreeView_GetText($hWndTreeView,$item)
Do
$parent = _GUICtrlTreeView_GetParentHandle($hWndTreeView,$item)
If $parent <> 0 Then
$txt = _GUICtrlTreeView_GetText($hWndTreeView,$parent) & "\" & $txt
$item = $parent
EndIf
Until $parent = 0
_SearchFolder($txt,$root)
EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc 回复 6# 3mile
非常感谢!
基本上这个问题 我已经解决啦 开始着手下一步了 呵呵 谢谢了 不过我还是会研究下你的代码的 #include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#include <array.au3>
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
Global $array1, $array2, $array3, $inipath, $inivalue, $readini
$inipath = @DesktopDir & '\test.ini'
$inivalue = ''
$GUI = GUICreate("(UDF Created) TreeView Create", 400, 640)
$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 568, $iStyle, $WS_EX_CLIENTEDGE)
Recursion(@ProgramFilesDir, '')
GUISetState()
$readini = IniReadSection($inipath , 'section1')
If @error Then
MsgBox(4096, "", "错误, 不是 INI 文静.")
Else
For $k = 1 To $readini
MsgBox(0,1,'key'&$readini[$i] & @CRLF & 'value' & $readini[$i])
Next
EndIf
Do
Until GUIGetMsg() = -3
Func Recursion($current, $flag)
Local $i = 0, $j = 0
If Not FileExists($inipath) Then
IniWrite($inipath, 'section', 'key', 1)
$inivalue = IniRead($inipath, 'section', 'key', '')
Else
$inivalue = IniRead($inipath, 'section', 'key', '')
$inivalue += 1
IniWrite($inipath, 'section', 'key', $inivalue)
$inivalue = IniRead($inipath, 'section', 'key', '')
EndIf
If $flag = '' Then $item1 = GUICtrlCreateTreeViewItem($current, $hTreeView)
Local $search = FileFindFirstFile($current & "\*.*")
While 1
Dim $file = FileFindNextFile($search)
If @error Or StringLen($file) < 1 Then ExitLoop
If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
If $flag <> '' Then $item1 = $flag
$item2 = GUICtrlCreateTreeViewItem($file, $item1)
$i += 1
ReDim $array2[$i + 1]
$array2[$i] = $item2
IniWrite($inipath, 'section1', 'key' & $i, $array2[$i])
Recursion($current & "\" & $file, $item2)
Else
If $flag <> '' Then $item1 = $flag
$item3 = GUICtrlCreateTreeViewItem($file, $item1)
$j += 1
ReDim $array3[$j + 1]
$array3[$j] = $item3
IniWrite($inipath, 'section2', 'key' & $j, $array3[$j])
EndIf
WEnd
Return
EndFunc ;==>Recursion 回复 6# 3mile
研究了你的代码 但是还是有个地方想请教下
代码的62行 case -451
这个451数字 你是如何得到的?
不懂 想请教下 谢谢
页:
[1]