找回密码
 加入
搜索
查看: 2482|回复: 9

迴圈的問題 while

[复制链接]
发表于 2009-2-24 22:59:08 | 显示全部楼层 |阅读模式
各位大大  你們好
我有一個很滿難搞的需求
要如何能讓我的button2能動作
我有想過hotkey 不過如果我開二個程式就會造成有問題了



Dim $szIPADDRESS = @IPAddress1
Dim $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted,$nPORT
Dim $msg, $recv
Dim $Form1, $Input1, $Button1, $Button2, $YY,$progress
Global $percent = 0, $direction = 1 , $iTimerProgress,$inetProgress
dim  $str1,$str2,$str3,$str4
dim $r,$r1,$r2,$r3


dim $filelog = @ScriptDir & "\OP.txt"
FileOpen($filelog,1)
$Form1 = GUICreate ("Server ", 200, 90, 150, 115)
$Input1 = GUICtrlCreateInput("9000", 16, 16, 121, 21)
$Button1 = GUICtrlCreateButton("B1", 144, 16, 43, 25, 0)
$Button2 = GUICtrlCreateButton("B2", 144, 40, 43, 25, 0)
GUICtrlSetBkColor($Button1, 0xFF0000)     ; Green
GUISetState(@SW_SHOW)



GUISetState()
While 1
        $msg = GUIGetMsg()
        Switch $msg
                Case  $GUI_EVENT_CLOSE
                        Exit
                case $Button1
                     netst()
                case $Button2
                        $YY = 'Y'
                        
        EndSwitch
WEnd

Func netst()
        GUICtrlSetBkColor($Button1, 0x00FF00)     ; Green
        GUICtrlSetData($Button1, "執行中")
        $YY = 'Y'
        TCPShutdown()
        $nPORT = GUICtrlRead($Input1)
        TCPStartup()
        $MainSocket = TCPListen($szIPADDRESS, $nPORT)
        If $MainSocket = -1 Then Exit
        $ConnectedSocket = -1
        Do
                $ConnectedSocket = TCPAccept($MainSocket)
        Until $ConnectedSocket <> -1
        
        Do
                $recv = TCPRecv($ConnectedSocket, 2048)
                If @error Then
                        If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
                        TCPShutdown()
                        TCPStartup()
                        $MainSocket = TCPListen($szIPADDRESS, $nPORT)
                        Do
                                $ConnectedSocket = TCPAccept($MainSocket)
                        Until $ConnectedSocket <> -1
                EndIf
                
                If $recv <> "" Then
                        FileWriteLine($filelog,"GET <<  " & _NowTime(5))
                        FileWriteLine($filelog, $recv)
                        
                        
                        
                        $strchk = StringRegExp($recv,'NPMsg',1) 
                        if @error = 0  then 
                        Strs($recv)
                        TCPSend($ConnectedSocket, $str3)
                        FileWriteLine($filelog, "SEND >>  " & _NowTime(5))
                        FileWriteLine($filelog, $str3)
                        FileWriteLine($filelog,  "-------------------------"&@CRLF)
                        ;_DebugOut("SEND >>  " & _NowTime(5) & @CRLF & $str3& @CRLF & "-------------------------"&@CRLF)
                        EndIf
                        
                EndIf
        Until $YY <> 'Y'
        
        If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
        If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)
        TCPShutdown()
EndFunc   ;==>netst


[ 本帖最后由 c9992 于 2009-2-26 07:57 编辑 ]

本帖子中包含更多资源

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

×
发表于 2009-2-24 23:09:44 | 显示全部楼层
加以下代码到EndIf和Until $YY <> 'Y'之间

$msg = GUIGetMsg()
If $msg = $Button2 Then $YY = 'Y'
 楼主| 发表于 2009-2-25 01:55:42 | 显示全部楼层
他走到 do Until 出不來了按鈕都失效了
他不是在gui的那個while中
发表于 2009-2-25 02:26:47 | 显示全部楼层
第一个do until是可以去除的.
另外,关于你这代码可以尝试用TCP事件模式的那个UDF;
再有就是函数间的参数传递问题,当Button2被按下时,$YY的值应当要传递到netst()
发表于 2009-2-25 04:32:57 | 显示全部楼层
原帖由 c9992 于 2009-2-25 01:55 发表
他走到 do Until 出不來了按鈕都失效了
他不是在gui的那個while中



所有的While 1下面都要有
$msg = GUIGetMsg()
If $msg = $Button2 Then $YY = 'Y'

你的代码不全,不知道怎么帮你改
大概给你一个例子:

$msg = GUIGetMsg()
Select
   Case $msg = $Button1
        While 1
           ; 
        WEnd
   Case $msg = $Button2
   Case Else
EndSelect


如果按照上面的写法,按下$Button1以后,所有其他按键全部失效,包括退出
这时候,你要在$Button1下面的While 1里,也加上$msg = GUIGetMsg()
也就是:
$msg = GUIGetMsg()
Select
   Case $msg = $Button1
        While 1
           ; 
           $msg = GUIGetMsg()
           Select 
                Case $msg = $Button2
                    ;
                Case Else
                    ;
              EndSelect
        WEnd
   Case $msg = $Button2
   Case Else
EndSelect



希望你看得懂。如果你嫌这个方法麻烦,可以考虑使用OnEnvent的模式,但那个也有那个的缺陷。主要看你程序是要做什么
发表于 2009-2-25 06:51:59 | 显示全部楼层

#Include <Date.au3>
Dim $szIPADDRESS = @IPAddress1
Dim $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted,$nPORT
Dim $msg, $recv
Dim $Form1, $Input1, $Button1, $Button2, $YY,$progress
Global $percent = 0, $direction = 1 , $iTimerProgress,$inetProgress
dim  $str1,$str2,$str3,$str4
dim $r,$r1,$r2,$r3


dim $filelog = @ScriptDir & "\OP.txt"
FileOpen($filelog,1)
$Form1 = GUICreate ("Server ", 200, 90, 150, 115)
$Input1 = GUICtrlCreateInput("9000", 16, 16, 121, 21)
$Button1 = GUICtrlCreateButton("B1", 144, 16, 43, 25, 0)
$Button2 = GUICtrlCreateButton("B2", 144, 40, 43, 25, 0)
GUICtrlSetBkColor($Button1, 0xFF0000)     ; Green
GUISetState(@SW_SHOW)



GUISetState()
While 1
        $msg = GUIGetMsg()
        Switch $msg
                Case  -3
                        Exit
                case $Button1
                        netst()
                case $Button2
                        GUICtrlSetData($Button1, "B1")
                        GUICtrlSetBkColor($Button1, 0xFF0000)
                        $YY = 'Y'
        EndSwitch
WEnd

Func netst()
        GUICtrlSetBkColor($Button1, 0x00FF00)     ; Green
        GUICtrlSetData($Button1, "執行中")
        $YY = 'Y'
        TCPShutdown()
        $nPORT = GUICtrlRead($Input1)
        TCPStartup()
        $MainSocket = TCPListen($szIPADDRESS, $nPORT)
        If $MainSocket = -1 Then Exit
        $ConnectedSocket = -1
        ;Do
                $ConnectedSocket = TCPAccept($MainSocket)
        ;Until $ConnectedSocket <> -1
        
        Do
                $msg = GUIGetMsg()
                If $msg = $Button2 Then
                        $YY = 'Y'
                        GUICtrlSetData($Button1, "B1")
                        GUICtrlSetBkColor($Button1, 0xFF0000)
                        ExitLoop
                EndIf
                If $msg = - 3 Then Exit
                $recv = TCPRecv($ConnectedSocket, 2048)
                If @error Then
                        If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
                        TCPShutdown()
                        TCPStartup()
                        $MainSocket = TCPListen($szIPADDRESS, $nPORT)
                        ;Do
                        ;        $ConnectedSocket = TCPAccept($MainSocket)
                        ;Until $ConnectedSocket <> -1
                        $ConnectedSocket = TCPAccept($MainSocket)
                EndIf
                
                If $recv <> "" Then
                        FileWriteLine($filelog,"GET <<  " & _NowTime(5))
                        FileWriteLine($filelog, $recv)
                        
                        
                        
                        $strchk = StringRegExp($recv,'NPMsg',1) 
                        if @error = 0  then 
                        ;Strs($recv)
                        TCPSend($ConnectedSocket, $str3)
                        FileWriteLine($filelog, "SEND >>  " & _NowTime(5))
                        FileWriteLine($filelog, $str3)
                        FileWriteLine($filelog,  "-------------------------"&@CRLF)
                        ;_DebugOut("SEND >>  " & _NowTime(5) & @CRLF & $str3& @CRLF & "-------------------------"&@CRLF)
                        EndIf
                        
                EndIf
        Until $YY <> 'Y'
        
        If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
        If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)
        TCPShutdown()
EndFunc   ;==>netst

大概地改了一下,希望是你要的效果,再有问题的话同LS,建议你发上完整代码.
 楼主| 发表于 2009-2-26 07:55:35 | 显示全部楼层

回复 6# ceoguang 的帖子

這位大大
用這個方法是按鈕正常了
可是 client 關閉了就會不能產生新的連線
也是有問題 我後來覺的讓他按鈕失效就失效了
我從 TraySetToolTip("Server Port :"  & $nPORT)  去試別  要關閉的程式這樣比較方便
我也有想過使用 coproce  不過滿難看懂的 有時間我再試試看
謝謝大家囉
发表于 2012-9-11 16:47:18 | 显示全部楼层
回复 6# ceoguang
超版好,我也遇上一个类似问题:点击Button1后,Button2不起作用(可能是WHILE  WEND内的内容不起作用),如果不点Button1的情况下直接点Button1按钮,_Rundism()函数可完整执行,望指教,帮忙改下。
代码如下:
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <GuiListView.au3>
#include <GuiListBox.au3>
#include <string.au3>
#include <Constants.au3>
#include <WinAPI.au3>

Global $PageNum = 1, $LevelForm, $Levelgroup, $LevelView, $SelectMoreButton, $SelectNextButton, $SelectCancelButton, $Levelgroup2, $LevelLabel
Global $title = " Win7/8系统安装工具", $WinSize

$varhd = "C:"
#region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("WINDOWS7/8安装工具   制作:NDYZX", 408, 149)
$Label1 = GUICtrlCreateLabel("选择安装文件", 8, 8, 79, 17)
GUICtrlSetFont(-1, 9, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x008080)
$Input1 = GUICtrlCreateInput("C:", 88, 8, 249, 21)
$Label2 = GUICtrlCreateLabel("选择映像版本", 8, 40, 79, 17)
GUICtrlSetFont(-1, 9, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x008080)
$Combo2 = GUICtrlCreateCombo("1", 88, 40, 313, 25)
$Label3 = GUICtrlCreateLabel("选择安装分区", 8, 72, 79, 17)
GUICtrlSetFont(-1, 9, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x008080)
$Combo3 = GUICtrlCreateCombo("C:", 88, 72, 313, 25)
$Label4 = GUICtrlCreateLabel("选择启动分区", 8, 104, 79, 17)
GUICtrlSetFont(-1, 9, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x008080)
$Combo4 = GUICtrlCreateCombo($varhd, 88, 104, 81, 25)
$Button1 = GUICtrlCreateButton("浏览", 344, 8, 60, 21)
GUICtrlSetFont(-1, 9, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x008000)
$Button2 = GUICtrlCreateButton("开始安装", 328, 104, 75, 21)
GUICtrlSetFont(-1, 9, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x008000)
$Label5 = GUICtrlCreateLabel("注意:点击安装后,会格式化安装分区,请保存好资料。", 24, 128, 376, 20)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
GUICtrlSetColor(-1, 0xFF0000)

$ProButton2 = GUICtrlCreateEdit("", 1, 100, 407, 49)
GUICtrlSetFont(-1, 8, 500, 0, "微软雅黑")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetState($ProButton2, $gui_hide)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
;-----------------------以下为选择引导区-------------------------------------------

Local $getlist
$var = DriveGetDrive("FIXED")
Global $FileNum
If Not @error Then
        For $i = 1 To $var[0]
                $varhd = GUICtrlSetData($Combo4, StringUpper($var[$i]))
        Next
EndIf

;注,引导区的盘符为$getlist后面 要用到
;-----------------------以下为选择安装分区-------------------------------------------
$HDr = DriveGetDrive("FIXED")
Dim $sizeAll[$HDr[0] + 1]
Dim $sizeFre[$HDr[0] + 1]
Dim $SYS[$HDr[0] + 1]
Dim $xs[$HDr[0] + 1]
For $i = 1 To $HDr[0]
        $sizeAll[$i] = Int(DriveSpaceTotal($HDr[$i] & ""))
        $sizeFre[$i] = Int(DriveSpaceFree($HDr[$i] & ""))
        $SYS[$i] = DriveGetFileSystem($HDr[$i] & "")
Next

$xs2 = ""
For $i = 1 To $HDr[0]
        $xs[$i] = StringUpper($HDr[$i]) & "盘,格式:" & $SYS[$i] & ",总空间:" & $sizeAll[$i] & "M,剩余空间:" & $sizeFre[$i] & "M。"

        If $i > 1 Then
                $xs2 = $xs2 & $xs[$i] & "|"
        EndIf
        
Next
GUICtrlSetState($Combo3, $gui_hide)
$fq3 = GUICtrlCreateCombo($xs[1], 88, 72, 313, 25)
GUICtrlSetData(-1, $xs2, $xs[1])
While 1
        $nmsg = GUIGetMsg()
        Switch $nmsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1 ;本句柄为点击浏览并找到WIM文件后显示WIM里WIM8的版本
                        Global $Image[20], $LevelInfo[20], $LevelDiscrip[20], $MINOR[20]
                        GUICtrlSetData($Combo2, "")                        
                        $wim = FileOpenDialog("请选择指定Windows安装文件的Wim映像", "", "Windows映像文件(*.wim)|所有文件(*.*)", 1 + 2, "install.wim")                        
                        GUICtrlSetData($Input1, $wim)
                        GUICtrlSetData($Combo2, "")
                        FileDelete(EnvGet("Temp") & '\wiminfo.txt')
                        
                        _RunDos('"' & @TempDir & '\imagex.exe"' & ' /info "' & $wim & '" >"' & @TempDir & '\wiminfo.txt"')
                        $txt = FileRead(EnvGet("Temp") & "\wiminfo.txt")
                        ClipPut($txt)
                        Local $txt = ClipGet()
                        $svar = StringRegExp($txt, '<IMAGE INDEX="(\V|">)', 3)
                        $var = StringRegExp($txt, '<DISPLAYNAME>(\V+)</DISPLAYNAME>', 3)
                        If @error Then
                                MsgBox(4096, "", "错误, 读取列表文件失败.")
                        Else
                                For $i = 0 To UBound($var) - 1
                                        For $i = 0 To UBound($svar) - 1
                                                GUICtrlSetData($Combo2, $svar[$i] & "." & $var[$i])
                                        Next
                                Next
                                _GUICtrlComboBox_SetEditText($Combo2, '正在获取信息,稍后从下拉列表中选择。')
                        ClipPut ( "" ) 
                        EndIf
                        
                Case $Button2;
                
                        GUICtrlSetState($Label1, $GUI_HIDE)
                        GUICtrlSetState($Input1, $GUI_HIDE)
                        GUICtrlSetState($Button1, $GUI_HIDE)
                        GUICtrlSetState($Label2, $GUI_HIDE)
                        GUICtrlSetState($Combo2, $GUI_HIDE)
                        GUICtrlSetState($Label3, $GUI_HIDE)
                        GUICtrlSetState($Combo3, $GUI_HIDE)
                        GUICtrlSetState($Label4, $GUI_HIDE)
                        GUICtrlSetState($Button2, $GUI_HIDE)
                        GUICtrlSetState($Label3, $GUI_HIDE)
                        GUICtrlSetState($Combo4, $GUI_HIDE)
                        
                        $getlist = GUICtrlRead($Combo4);为引导盘变量
                        $q = GUICtrlRead($fq3) ;本名为读取框中文本这里作为安装按钮的示例,正式出来要换掉
                        $W = StringLeft($q, 2) ;为安装分区变量。截取 文本前两位为安装位置
                        $x = GUICtrlRead($Input1) ;为WIM文件路径
                        $y = GUICtrlRead($Combo2)
                        Local $T = StringRegExpReplace(StringRegExpReplace($y, '^(.{1,3}).*', '$1'), '\D', '');为wim文件映像编号,正则提取字符串的前三位数字。                        
                        _Rundism()                        
                        Sleep(3000)
                        ExitLoop
        EndSwitch
WEnd
Run(@ComSpec & " /c " & 'pecmd shut r', "", @SW_HIDE)
Func _Rundism()
$Label1 = GUICtrlCreateLabel("正在安装", 8, 8, 79, 17)
$ProButton2 = GUICtrlCreateEdit("", 1, 22, 407, 149,BitOR(0x00200000,0x0800))
GUISetState(@SW_SHOW)
GUICtrlSetData($ProButton2,"安装进程...",' ')

          ;$foo = Run("@TempDir & '\imagex.exe' & ' /apply ' & $x & ' ' & $T & ' ' & $W & '\'", @ScriptDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) 
           $foo = Run('qll.bat',"", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
        While 1
                $linea = StdoutRead($foo)
                If @error Then ExitLoop
                Dim $ProgressA,$ProgressB,$ImageXProgress
                If StringStripWS($linea,8)<>"" Then
                        GUICtrlSetData($ProButton2,@CRLF&$linea,' ')
                        If StringInStr(StringStripCR($linea),"Applying progress") Then
                                $ProgressA=StringSplit(StringStripCR($linea),"]")
                                $ProgressB=StringSplit($ProgressA[1],"[")
                                $ImageXProgress=StringStripCR(StringStripWS($ProgressB[2],8))
                        EndIf
                EndIf        
        WEnd                
EndFunc   ;==>_Rundism

Func _RunDos($sCommand)
        Return RunWait(@ComSpec & " /C """ & $sCommand & """", "", @SW_HIDE)
EndFunc   ;==>_RunDos
发表于 2012-9-11 19:24:55 | 显示全部楼层
回复 8# ndyzx
帮助里查找GUIOnEventMode
发表于 2012-9-11 20:20:59 | 显示全部楼层
回复 9# ceoguang
帮助里内容少,笨不好理解,针对上例能不能具体提点,先谢了。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-30 01:34 , Processed in 0.085443 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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