Что нового

900 if endif как сократить?

dimcomp

Новичок
Сообщения
66
Репутация
0
Доброго дня.
Есть такой код:

Код:
if GUICtrlRead($PButton[5]) = "1" then
    ;код5
endif

if GUICtrlRead($PButton[6]) = "1" then
    ;код6
endif

; .....................

if GUICtrlRead($PButton[30]) = "1" then
    ;код30
endif

if GUICtrlRead($PButton[5]) = "2" then
    ;код5
endif

if GUICtrlRead($PButton[6]) = "2" then
    ;код6
endif

; .....................

if GUICtrlRead($PButton[30]) = "2" then
    ;код30
endif

; .....................

if GUICtrlRead($PButton[5]) = "30" then
    ;код5
endif

if GUICtrlRead($PButton[6]) = "30" then
    ;код6
endif

; .....................

if GUICtrlRead($PButton[30]) = "30" then
    ;код30
endif


Можно ли его как-то сократить?
 

filautdinov

Знающий
Сообщения
96
Репутация
9
Как то так
Код:
For $i=1 To 900
	if GUICtrlRead($PButton[$i]) = "1" then
    ;код5
	endif
	$i +=1
Next
 

InnI

AutoIT Гуру
Сообщения
4,922
Репутация
1,432
dimcomp
У вас на кнопке меняется текст от 1 до 30? И при любом значении от 1 до 30 нужно выполнить один и тот же код?
Код:
if GUICtrlRead($PButton[5]) then ; любой текст кнопки (не пустая строка)
    ;код5
endif
 
Автор
D

dimcomp

Новичок
Сообщения
66
Репутация
0
У вас на кнопке меняется текст от 1 до 30? И при любом значении от 1 до 30 нужно выполнить один и тот же код?
Да, текст меняется на кнопках, и очерёдность исполнения кода зависит от цифры, чем меньше цифра, тем раньше исполнится код, т.е. первом делом исполняется код где значение =1, затем 2 и т.д. (цифры на кнопках не повторяются)


Добавлено:
Сообщение автоматически объединено:

код разный в зависимости от кнопки. Поэтому не вариант.
 

InnI

AutoIT Гуру
Сообщения
4,922
Репутация
1,432
dimcomp
Код:
Global $PButton[10]

GUICreate("")
For $i = 5 To 9
  $PButton[$i] = GUICtrlCreateButton("", $i * 20, $i * 20, 20, 20)
Next
GUISetState()

GUICtrlSetData($PButton[5], "3")
GUICtrlSetData($PButton[6], "5")
GUICtrlSetData($PButton[7], "1")
GUICtrlSetData($PButton[8], "2")
GUICtrlSetData($PButton[9], "4")

MsgBox(0, "", "start")

$j = 1
While $j <= 5
  For $i = 5 To 9
    If GUICtrlRead($PButton[$i]) = $j Then
      Call("Code" & $i)
      ExitLoop
    EndIf
  Next
  $j += 1
WEnd

Do
Until GUIGetMsg() = -3

Func Code5()
  ConsoleWrite("5")
EndFunc

Func Code6()
  ConsoleWrite("6")
EndFunc

Func Code7()
  ConsoleWrite("7")
EndFunc

Func Code8()
  ConsoleWrite("8")
EndFunc

Func Code9()
  ConsoleWrite("9")
EndFunc
 
Автор
D

dimcomp

Новичок
Сообщения
66
Репутация
0
InnI, или у меня не работает, или не правильно объяснил. В общем вот код

Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $px = 5
Global $py = 0
Global $PButton[$px]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 235, 170, 192, 124)
$PButton[0] = GUICtrlCreateButton("", 40, 56, 51, 49)
$PButton[1] = GUICtrlCreateButton("", 96, 56, 51, 49)
$PButton[2] = GUICtrlCreateButton("", 152, 56, 51, 49)
$PButton[3] = GUICtrlCreateButton("Сбросить", 40, 128, 161, 25)
$PButton[4] = GUICtrlCreateButton("Старт", 40, 12, 161, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
	Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $PButton[0]
		    if GUICtrlRead($PButton[0]) = "" then 
		    GUICtrlSetData($PButton[0], $py+1)
			$py=$py+1
			Endif
		Case $PButton[1]
		    if GUICtrlRead($PButton[1]) = "" then
            GUICtrlSetData($PButton[1], $py+1)
			$py=$py+1
			Endif
        Case $PButton[2]
		    if GUICtrlRead($PButton[2]) = "" then
            GUICtrlSetData($PButton[2], $py+1)	
			$py=$py+1
			Endif
		Case $PButton[3]	
		    GUICtrlSetData($PButton[0], "")
			GUICtrlSetData($PButton[1], "")
			GUICtrlSetData($PButton[2], "")
			$py = 0
		Case $PButton[4]	
		    start()
    EndSwitch
WEnd

Func start()

if GUICtrlRead($PButton[0]) = "1" then
mousemove(45, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[1]) = "1" then
mousemove(101, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[2]) = "1" then
mousemove(157, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[0]) = "2" then
mousemove(45, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[1]) = "2" then
mousemove(101, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[2]) = "2" then
mousemove(157, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[0]) = "3" then
mousemove(45, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[1]) = "3" then
mousemove(101, 61)
sleep(1000)
endif

if GUICtrlRead($PButton[2]) = "3" then
mousemove(157, 61)
sleep(1000)
endif
Exit
Endfunc


таких квадратов будет штук 30...
Case бы тоже сократить :-[
 

InnI

AutoIT Гуру
Сообщения
4,922
Репутация
1,432
dimcomp
Сократить ваш код можно циклом. Массив кнопок у вас есть. Теперь нужно сопоставить кнопки с функциями. Проще всего это сделать по индексу кнопки в массиве
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $px = 5
Global $py = 0
Global $PButton[$px]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 235, 170, 192, 124)
$PButton[0] = GUICtrlCreateButton("", 40, 56, 51, 49)
$PButton[1] = GUICtrlCreateButton("", 96, 56, 51, 49)
$PButton[2] = GUICtrlCreateButton("", 152, 56, 51, 49)
$PButton[3] = GUICtrlCreateButton("Сбросить", 40, 128, 161, 25)
$PButton[4] = GUICtrlCreateButton("Старт", 40, 12, 161, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $PButton[0] To $PButton[2]
            if GUICtrlRead($Msg) = "" then
                $py += 1
                GUICtrlSetData($Msg, $py)
            Endif
        Case $PButton[3]
            For $i = 0 To 2
              GUICtrlSetData($PButton[$i], "")
            Next
            $py = 0
        Case $PButton[4]
            start()
    EndSwitch
WEnd

Func start()
  Local $Num = 1
  While $Num <= 3
    For $i = 0 To 2
      If GUICtrlRead($PButton[$i]) = $Num Then
        Call("Btn" & $i)
        ExitLoop
      EndIf
    Next
    $Num += 1
  WEnd
Endfunc

Func Btn0()
    ConsoleWrite("0" & @CRLF)
    mousemove(45, 61)
    sleep(1000)
EndFunc

Func Btn1()
    ConsoleWrite("1" & @CRLF)
    mousemove(101, 61)
    sleep(1000)
EndFunc

Func Btn2()
    ConsoleWrite("2" & @CRLF)
    mousemove(157, 61)
    sleep(1000)
EndFunc
 
Автор
D

dimcomp

Новичок
Сообщения
66
Репутация
0
К сожалению после обфускации скрипт не работает, я так понимаю проблема в Call, можно ли это как-то обойти?
 
A

Alofa

Гость
dimcomp сказал(а):
... можно ли это как-то обойти?
Только путем Изучения непознанного.

Ну или так:
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $px = 5
Global $py = 0
Global $PButton[$px]

$Form1 = GUICreate("Form1", 235, 170, 192, 124)
$PButton[0] = GUICtrlCreateButton("", 40, 56, 51, 49)
$PButton[1] = GUICtrlCreateButton("", 96, 56, 51, 49)
$PButton[2] = GUICtrlCreateButton("", 152, 56, 51, 49)
$PButton[3] = GUICtrlCreateButton("Сбросить", 40, 128, 161, 25)
$PButton[4] = GUICtrlCreateButton("Старт", 40, 12, 161, 25)
GUISetState(@SW_SHOW)

While 1
	$iMsg = GUIGetMsg()
	Switch $iMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $PButton[0] To $PButton[2]
			If GUICtrlRead($iMsg) = "" Then
				$py += 1
				GUICtrlSetData($iMsg, $py)
			EndIf
		Case $PButton[3]
			GUICtrlSetData($PButton[0], "")
			GUICtrlSetData($PButton[1], "")
			GUICtrlSetData($PButton[2], "")
			$py = 0
		Case $PButton[4]
			start()
	EndSwitch
WEnd

Func start()
	Local $aCoord[][] = [[45, 61], [101, 61], [157, 61]]
	For $i = 1 To 3
		For $j = 0 To UBound($aCoord) - 1
			If GUICtrlRead($PButton[$j]) = $i Then
				MouseMove($aCoord[$j][0], $aCoord[$j][1])
				Sleep(1000)
			EndIf
		Next
	Next
	Exit
EndFunc   ;==>start
 
Автор
D

dimcomp

Новичок
Сообщения
66
Репутация
0
Только путем Изучения непознанного.
Смотрел примеры, помогло #Obfuscator_Parameters= /CF=0
Пробовал ещё #Obfuscator_Ignore_Funcs=_FuncName вместо _FuncName так и не понял чего указывать указал так #Obfuscator_Ignore_Funcs=Call - не работает. Или как-то по другому нужно было. В примерах не нашёл(
 
Верх