jwwlchen 发表于 2013-7-20 13:05:43

请帮忙看下为什么选中菜单项后程序卡住了??

选中设置->提取制定行信息后卡住了


#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>

Local $iMemo
Local Enum $idNew = 1000, $Extract


Local $hGUI, $hEdit, $hMain

$hGUI = GUICreate("Log 分析工具   --version: 1.0", 400, 300)

$hMain = _GUICtrlMenu_CreateMenu()
$hEdit = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_InsertMenuItem($hEdit, 0, "提取制定行信息", $Extract)
_GUICtrlMenu_InsertMenuItem($hMain, 1, "设置", 0, $hEdit)

_GUICtrlMenu_SetMenu($hGUI, $hMain)
GUISetState()
       
While 1
        GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then
                Exit
        EndIf       
WEnd       


Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
                #forceref $hWnd, $iMsg, $ilParam
                Switch _WinAPI_LoWord($iwParam)
                        Case $Extract
                                Extract_Line()                       
                EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc

Func Extract_Line()
        $Combo1 = GUICtrlCreateCombo("", 72, 20, 177, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
        $Button1 = GUICtrlCreateButton("浏览...", 280, 10, 65, 33)
        $label0=GUICtrlCreateLabel("请点击浏览选择LOG文件存放目录",72,60,220,100)
        $Line = GUICtrlCreateInput("", 72, 90, 100, 20)
        $Button_1= GUICtrlCreateButton ("Go", 300, 150,50, 40)
        $label1=GUICtrlCreateLabel("请输入要处理的行数",72,120,220,100)
        $label2=GUICtrlCreateLabel(" 2 正数第二行",72,140,220,100)
        $label3=GUICtrlCreateLabel("-2 倒数第二行",72,160,220,100)

        While 1

                $msg = GUIGetMsg()
                Select
                        Case $msg = $GUI_EVENT_CLOSE
                                Exit (255)
                        Case $msg = $Button1
                                $userpath = FileSelectFolder("选择一个位置", "")
                                If $userpath <> "" Then
                                        GUICtrlSetData($Combo1, "")
                                        GUICtrlSetData($Combo1, $userpath, $userpath)
                                EndIf       
                                GUICtrlSetState($Line, $GUI_FOCUS)
                        Case $msg = $Button_1
                                Go($Combo1,$Line)
                EndSelect
        WEnd
EndFunc

Func GO($Combo1,$Line)
        $LogFile = GUICtrlRead ($Combo1)
        $LogLine = GUICtrlRead ($Line)
       
        If 0 == FileExists($LogFile & "\*.log") And 0 == FileExists($LogFile & "\*.txt") Then
                MsgBox(16, "错误", $LogFile & " 没有发现 LOG 或 TXT 文件,请重新选择")
        ElseIf $LogLine < 1 And $LogLine >= 0 Then
                MsgBox(16, "错误", "行数为空,请重新输入要处理的行数")
                GUICtrlSetState($Line, $GUI_FOCUS)
        Else
                Write_Info($LogFile,$LogLine)
        EndIf       
EndFunc

Func Write_Info($LogFile,$LogLine)
        If FileExists (@ScriptDir & "\" & "out.log") Then FileDelete (@ScriptDir & "\" & "out.log")
       
        $search = FileFindFirstFile($LogFile & "\*.*")

        If $search = -1 Then
                MsgBox(0, "错误", "没有文件/目录 匹配搜索")
                Exit
        EndIf
       
        While 1
                $file = FileFindNextFile($search)
                If @error Then ExitLoop
       
                $CountLines = _FileCountLines($LogFile & "\" & $file)
       
                If $LogLine < 0 Then
                        $Data = FileReadLine($LogFile & "\" & $file, $CountLines + $LogLine + 1)
                EndIf
       
                If $LogLine > 0 Then
                        $Data = FileReadLine($LogFile & "\" & $file, $LogLine)
                EndIf
       
                $outlog = FileOpen("out.log", 9)
                FileWrite($outlog, $Data & @CRLF)
                FileClose($outlog)
        WEnd

        FileClose($search)

        MsgBox(64, "", "所有数据已导入到 " & @CRLF _
                                        & @ScriptDir & "\" & "OUT.LOG")
        ShellExecute("out.log")
       
EndFunc       

seniors 发表于 2013-7-20 19:06:49

改用事件模式吧

user3000 发表于 2013-7-20 21:41:30

回复 1# jwwlchen

你的代码有两个循环(完全没有必要的), 但没有退出内层循环的操作,程序当然会假死了.
而且不应该在点击菜单后才建立控件,否则岂不是多次点击会反复建立相同的控件?
在你代码基础上作了相应修改, 自己比对吧.#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>

Local $iMemo
Local Enum $idNew = 1000, $Extract


Local $hGUI, $hEdit, $hMain

$hGUI = GUICreate("Log 分析工具   --version: 1.0", 400, 300)
$Combo1 = GUICtrlCreateCombo("", 72, 20, 177, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$Button1 = GUICtrlCreateButton("浏览...", 280, 10, 65, 33)
$label0 = GUICtrlCreateLabel("请点击浏览选择LOG文件存放目录", 72, 60, 220, 100)
$Line = GUICtrlCreateInput("", 72, 90, 100, 20)
$Button_1 = GUICtrlCreateButton("Go", 300, 150, 50, 40)
$label1 = GUICtrlCreateLabel("请输入要处理的行数", 72, 120, 220, 100)
$label2 = GUICtrlCreateLabel(" 2 正数第二行", 72, 140, 220, 100)
$label3 = GUICtrlCreateLabel("-2 倒数第二行", 72, 160, 220, 100)
$hMain = _GUICtrlMenu_CreateMenu()
$hEdit = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_InsertMenuItem($hEdit, 0, "提取制定行信息", $Extract)
_GUICtrlMenu_InsertMenuItem($hMain, 1, "设置", 0, $hEdit)

_GUICtrlMenu_SetMenu($hGUI, $hMain)
_set_controls_state(0)
GUISetState()
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        Exit (255)
                Case $msg = $Button1
                        $userpath = FileSelectFolder("选择一个位置", "")
                        If $userpath <> "" Then
                                GUICtrlSetData($Combo1, "")
                                GUICtrlSetData($Combo1, $userpath, $userpath)
                        EndIf
                        GUICtrlSetState($Line, $GUI_FOCUS)
                Case $msg = $Button_1
                        Go($Combo1, $Line)
        EndSelect
WEnd


Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg, $ilParam
        Switch _WinAPI_LoWord($iwParam)
                Case $Extract
                                _set_controls_state(1)
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func GO($Combo1, $Line)
        $LogFile = GUICtrlRead($Combo1)
        $LogLine = GUICtrlRead($Line)

        If 0 == FileExists($LogFile & "\*.log") And 0 == FileExists($LogFile & "\*.txt") Then
                MsgBox(16, "错误", $LogFile & " 没有发现 LOG 或 TXT 文件,请重新选择")
        ElseIf $LogLine < 1 And $LogLine >= 0 Then
                MsgBox(16, "错误", "行数为空,请重新输入要处理的行数")
                GUICtrlSetState($Line, $GUI_FOCUS)
        Else
                Write_Info($LogFile, $LogLine)
        EndIf
EndFunc   ;==>GO

Func Write_Info($LogFile, $LogLine)
        If FileExists(@ScriptDir & "\" & "out.log") Then FileDelete(@ScriptDir & "\" & "out.log")

        $search = FileFindFirstFile($LogFile & "\*.*")

        If $search = -1 Then
                MsgBox(0, "错误", "没有文件/目录 匹配搜索")
                Exit
        EndIf

        While 1
                $file = FileFindNextFile($search)
                If @error Then ExitLoop

                $CountLines = _FileCountLines($LogFile & "\" & $file)

                If $LogLine < 0 Then
                        $Data = FileReadLine($LogFile & "\" & $file, $CountLines + $LogLine + 1)
                EndIf

                If $LogLine > 0 Then
                        $Data = FileReadLine($LogFile & "\" & $file, $LogLine)
                EndIf

                $outlog = FileOpen("out.log", 9)
                FileWrite($outlog, $Data & @CRLF)
                FileClose($outlog)
        WEnd

        FileClose($search)

        MsgBox(64, "", "所有数据已导入到 " & @CRLF _
                       & @ScriptDir & "\" & "OUT.LOG")
        ShellExecute("out.log")

EndFunc   ;==>Write_Info
Func _set_controls_state($iFlag = 1)
        Local $state
        If $iFlag Then
                $state = $gui_show
        Else
                $state = $gui_hide
        EndIf
        For $i = $Combo1 To $label3
                GUICtrlSetState($i, $state)
        Next
EndFunc   ;==>_set_controls_state

jwwlchen 发表于 2013-7-21 13:19:19

回复 3# user3000


    谢谢您的解答,我的意思是把几个功能整合到一个菜单中,点击相应的功能后
下面显示对应的功能控件,如下图,这样的方案有什么比较好的解决思路吗? 请帮忙指教

user3000 发表于 2013-7-21 18:02:46

回复 4# jwwlchen
那可以考虑用分页标签,把它们整合到同一个GUI里,你这个菜单也可以省去了!
自己参考帮助: GUICtrlCreateTab

jwwlchen 发表于 2013-7-22 07:59:57

回复 5# user3000


    Thanks for your great help!
页: [1]
查看完整版本: 请帮忙看下为什么选中菜单项后程序卡住了??