Bonjour à tous, donc le principe de cet UDF est de générer des formulaires HTML de la même façon qu'un Interface autoit afin, notament de pouvoir utiliser Koda pour générer ces dernier (on y revient dans l'exemple)
donc les fonctions prises en charge sont:
-Button
-Checkbox
-Password
-Submit
-Radio
-File
-TextBox
-Label
-Combo
-Edit
ces dernière sont strictement semblable aux fonction GUICtrl... sauf que:
a) elle prennent un premier paramètre supplémentaire qui est le Formulaire de base (renvoyé par FormCreate)
b) les style sont en fait des classes CSS
c) le dernier paramètre qui est normalement exstyle devient le nom de l'élément dans le formulaire
d) elle commencent par FormCtrl à la place de GUICtrl
e) il faut finir par FormEnd histoire de clore les balises
f) le nom du formulaire est du type METHOD@TARGET (post ou get vers la page de traitement)
► Afficher le texteExemple de script
Code : Tout sélectionner
$Form1 = FormCreate("post@Form1", 360, 138, 192, 124)
FormCtrlCreateLabel($Form1, "age:", 8, 8, 25, 17, "", "Label1")
FormCtrlCreateTextBox($Form1, "", 56, 8, 73, 21, "", "Input1")
FormCtrlCreateLabel($Form1, "sexe:", 8, 32, 29, 17, "", "Label2")
FormCtrlCreateRadio($Form1, "H", 56, 32, 17, 17, "", "Radio1")
FormCtrlCreateLabel($Form1, "homme", 80, 32, 38, 17, "", "Label3")
FormCtrlCreateRadio($Form1, "F", 56, 56, 17, 17, "", "Radio1")
FormCtrlCreateLabel($Form1, "Femme", 80, 56, 38, 17, "", "Label4")
FormCtrlCreateEdit($Form1, "--votre slogan ici--", 200, 8, 153, 129, "", "Edit1")
FormCtrlCreateCombo($Form1, "Français|Anglais|Arabe|Allemand|Autre", 56, 80, 73, 25, "", "Combo1")
FormCtrlCreateLabel($Form1, "Langue:", 8, 80, 43, 17, "", "Langue")
FormCtrlCreateSubmit($Form1, "Envoyer", 8, 112, 188, 22)
FormEnd($Form1)
clipPut($form1)
► Afficher le texte
Code : Tout sélectionner
$Form1 = GUICreate("post@Form1", 360, 138, 192, 124)
$Label1 = GUICtrlCreateLabel("age:", 8, 8, 25, 17)
$Input1 = GUICtrlCreateInput("", 56, 8, 73, 21)
$Label2 = GUICtrlCreateLabel("sexe:", 8, 32, 29, 17)
$Radio1 = GUICtrlCreateRadio("H", 56, 32, 17, 17)
$Label3 = GUICtrlCreateLabel("homme", 80, 32, 38, 17)
$Radio2 = GUICtrlCreateRadio("F", 56, 56, 17, 17)
$Label4 = GUICtrlCreateLabel("Femme", 80, 56, 38, 17)
$Edit1 = GUICtrlCreateEdit("", 200, 8, 153, 129)
GUICtrlSetData(-1, "--votre slogan ici--")
$Combo1 = GUICtrlCreateCombo("", 56, 80, 73, 25)
GUICtrlSetData(-1, "Français|Anglais|Arabe|Allemand|Autre")
$Langue = GUICtrlCreateLabel("Langue:", 8, 80, 43, 17)
$Submit = GUICtrlCreateButton("Envoyer", 8, 112, 188, 22)
Donc le convertisseur est compris dans l'UDF et la fonction se nomme
KodaConverter($text)
où $texte est du code autoit d'entrée
UDF
► Afficher le texteCode de l'UDF
Code : Tout sélectionner
global $Form_Pattern = '<form action="$ACTION$" method="$METHOD$" style="overflow:hidden;position:$POSITION$;top:$TOP$;left:$LEFT$;width:$WIDTH$;height:$HEIGHT$" class="$STYLE$">$CONTENT$</form>'
global $Widht_Height_Position_Class = 'style="position:absolute;top:$TOP$;left:$LEFT$;width:$WIDTH$;height:$HEIGHT$" class="$STYLE$" name="$NAME$"'
global $Form_Ctrl_Input_Pattern = '<input type="$TYPE$" value="$TEXT$" ' & $Widht_Height_Position_Class & ' />'
global $Form_Ctrl_Label_Pattern = '<span ' & $Widht_Height_Position_Class & '>$TEXT$</span>'
global $Form_Ctrl_Combo_Pattern = '<select ' & $Widht_Height_Position_Class & '><option>$TEXT$</option></select>'
global $Form_Ctrl_Edit_Pattern = '<textarea ' & $Widht_Height_Position_Class & '>$TEXT$</textarea>'
#region Form creation
func FormCreate($cible, $width, $height, $left = 0, $top = 0, $style = "", $position = "absolute")
if $left = default then $left = 0
if $top = default then $top = 0
if $style = default then $style = ""
if $position = default then $position = "absolute"
$a = StringSplit($cible, "@")
$METHOD = $a[1]
$ACTION = $a[2]
$output = $Form_Pattern
$output = stringReplace($output, "$ACTION$", $ACTION)
$output = StringReplace($output, "$METHOD$", $METHOD)
$output = StringReplace($output, "$POSITION$" , $position)
Top_Left_Style_Width_Height($output, $left ,$top ,$width, $height, $style)
return $output
EndFunc
func FormEnd(byref $Form)
$Form = StringReplace($Form, "$CONTENT$", "")
return $form
EndFunc
#endregion
#region Input Aliases
func FormCtrlCreateButton(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "button", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
func FormCtrlCreateCheckBox(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "checkbox", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
func FormCtrlCreatePassword(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "password", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
func FormCtrlCreateSubmit(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "submit", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
func FormCtrlCreateRadio(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "radio", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
func FormCtrlCreateFile(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "file", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
func FormCtrlCreateTextBox(byref $Form, $text, $left, $top, $width = default, $height = 20, $style = "", $name = "FormElement")
FormCtrlCreateInput($Form, "text", $text, $left, $top, $width, $height, $style, $name)
return $form
EndFunc
#endregion
#region <span> balise
func FormCtrlCreateLabel(byref $Form, $text, $left, $top, $width = default, $height = 14, $style = "", $name = "FormElement")
if $width = default then $width = StringLen($text) * 8
if $height = default then $height = 22
if $style = default then $style = ""
if $name = default then $name = "FormElement"
$output = $Form_Ctrl_Label_Pattern
$output = StringReplace($output, "$TEXT$" , $text)
$output = StringReplace($output, "$NAME$" , $name)
Top_Left_Style_Width_Height($output, $left ,$top ,$width, $height, $style)
$Form = StringReplace($Form, "$CONTENT$", "$CONTENT$" & @CRLF & $output)
return $form
EndFunc
#endregion
#region <select> balise
func FormCtrlCreateCombo(ByRef $Form, $text, $left, $top, $width = 50, $height = 22, $style = "", $name= "FormElement")
if $width = default then $width = StringLen($text) * 8
if $height = default then $height = 22
if $style = default then $style = ""
if $name = default then $name = "FormElement"
$output = $Form_Ctrl_Combo_Pattern
$output = StringReplace($output, "$TEXT$" , StringReplace($text, "|", "</option><option>"))
$output = StringReplace($output, "$NAME$" , $name)
Top_Left_Style_Width_Height($output, $left ,$top ,$width, $height, $style)
$Form = StringReplace($Form, "$CONTENT$", "$CONTENT$" & @CRLF & $output)
return $form
EndFunc
#endregion
#region <textarea> balise
func FormCtrlCreateEdit(ByRef $Form, $text, $left, $top, $width = 50, $height = 22, $style = "", $name= "FormElement")
if $width = default then $width = StringLen($text) * 8
if $height = default then $height = 22
if $style = default then $style = ""
if $name = default then $name = "FormElement"
$output = $Form_Ctrl_Edit_Pattern
$output = StringReplace($output, "$TEXT$" , $text)
$output = StringReplace($output, "$NAME$" , $name)
Top_Left_Style_Width_Height($output, $left ,$top ,$width, $height, $style)
$Form = StringReplace($Form, "$CONTENT$", "$CONTENT$" & @CRLF & $output)
return $form
EndFunc
#endregion
#region KodaConverter
func KodaConverter($text)
$guinamelist = StringRegExp($text, '(.*?) = GUICreate.*?', 3) ;OK
$guiname = $guinamelist[UBound($guinamelist) - 1]
$text = StringRegExpReplace($text, '(?i)\$Submit = GUICtrlCreateButton(.*?)', "FormCtrlCreateSubmit$1") ;Pas OK
$text = StringRegExpReplace($text, ", BitOR\((.*?)\)", "") ;OK
$text = StringRegExpReplace($text, '(.*?)".*?"(.*?)\r?\nGUICtrlSetData\(-1, "(.*?)"\)', '$1"$3"$2') ;OK
$text = StringRegExpReplace($text, '\$(.*?) = GUICtrl(.*?)\)', 'GUICtrl$2, "", "$1")') ;OK
$text = StringReplace($text, "GUI", "Form") ;OK
$text = StringReplace($text, "\r\n", @crlf) ;OK
$text = StringReplace($text, "(", "(" & $guiname & ", ") ;OK
$text = StringReplace($text, "FormCtrlCreateInput", "FormCtrlCreateTextBox")
$text = StringRegExpReplace($text, 'FormCreate\((.*?), (.*?)\)', "FormCreate($2)") ;OK
$text &= @crlf & "FormEnd(" & $guiname & ")";OK
return $text
EndFunc
#endregion
#region helper (internal)
func Top_Left_Style_Width_Height(byref $output, $left, $top, $width, $height, $style)
$output = StringReplace($output, "$WIDTH$" , $width)
$output = StringReplace($output, "$HEIGHT$", $height)
$output = StringReplace($output, "$TOP$" , $top)
$output = StringReplace($output, "$LEFT$" , $left)
$output = StringReplace($output, "$STYLE$" , $style)
EndFunc
func FormCtrlCreateInput(byref $Form, $type, $text, $left, $top, $width = default, $height = 22, $style = "", $name = "FormElement")
if $width = default then $width = StringLen($text) * 8
if $height = default then $height = 22
if $style = default then $style = ""
if $name = default then $name = "FormElement"
$output = $Form_Ctrl_Input_Pattern
$output = StringReplace($output, "$TEXT$" , $text)
$output = StringReplace($output, "$NAME$" , $name)
$output = StringReplace($output, "$TYPE$" , $type)
Top_Left_Style_Width_Height($output, $left ,$top ,$width, $height, $style)
$Form = StringReplace($Form, "$CONTENT$", "$CONTENT$" & @CRLF & $output)
EndFunc
#endregion

► Afficher le texte
Code : Tout sélectionner
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "HTMLgui.au3"
#include <array.au3>
#include <IE.au3>
#include <file.au3>
Global $T = TimerInit()
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Convertisseur", 627, 384, 263, 386)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Edit1 = GUICtrlCreateEdit("", 0, 0, 625, 177)
GUICtrlSetData(-1, ";Code généré par Koda")
$Edit2 = GUICtrlCreateEdit("", 0, 176, 625, 177)
GUICtrlSetData(-1, StringFormat(";Aperçu de code\r\n;(après transformation)"))
$MyButton1 = GUICtrlCreateButton("HTML vers presse-papier", 0, 352, 148, 30, BitOR($BS_NOTIFY,$BS_FLAT))
GUICtrlSetOnEvent(-1, "MyButton1Click")
$MyButton2 = GUICtrlCreateButton("HTML vers fichier...", 152, 352, 148, 30, BitOR($BS_NOTIFY,$BS_FLAT))
GUICtrlSetOnEvent(-1, "MyButton2Click")
$MyButton3 = GUICtrlCreateButton("Aperçu dans le navigateur", 304, 352, 148, 30, BitOR($BS_NOTIFY,$BS_FLAT))
GUICtrlSetOnEvent(-1, "MyButton3Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Func Edit1Change()
if TimerDiff($T) > 1000 then
GUICtrlSetData($edit2, KodaConverter2(guictrlread($edit1)))
$T = TimerInit()
EndIf
EndFunc
Func Form1Close()
Exit
EndFunc
Func MyButton1Click()
ClipPut(Generate())
EndFunc
Func MyButton2Click()
$f = FileSaveDialog("Enregistrer sous...", "", "fichier HTML (*.html)")
if StringRight($f, 5) <> ".html" then $f &= ".html"
FileWrite($f, "<html>" & @crlf & "<head></head>" & @crlf & "<body>" & @crlf & Generate() & @crlf & "</body>" & @crlf & "</html>")
EndFunc
Func MyButton3Click()
$f = _TempFile()
if StringRight($f, 5) <> ".html" then $f &= ".html"
FileWrite($f, "<html><head></head><body>" & Generate() & "</body></html>")
_IECreate($f)
EndFunc
func KodaConverter2($text)
;dérivée de la fonction de l'UDF pour une transformation plus precise au but recherché
;par exemple utilisation de la valeur de retour à la place du byref qui n'est pas géré par Call
;un peu plus tard
;on retrouve le nom du formulaire
$guinamelist = StringRegExp($text, '(.*?) = GUICreate.*?', 3)
if not IsArray($guinamelist) then return "format incorect"
$guiname = $guinamelist[UBound($guinamelist) - 1]
;ligne de type #include ou #region
$text = StringRegExpReplace($text, "\#.*", @crlf)
;la boucle infini qui ne nous concerne pas
$text = StringRegExpReplace($text, "(?s)While 1(.*?)WEnd", @crlf)
;le bouton nommé "submit" n'est pas un bouton mais un bouton d'envoi, en HTML c'est pas pareil, on le gère donc
$text = StringRegExpReplace($text, '(?i)\$Submit = GUICtrlCreateButton(.*?)', "FormCtrlCreateSubmit$1")
;les bitor contiennent les "style" windows, innutile pour nous qui utilisons le CSS (à ajouter manuellement, même si une correspondance pourrait se faire à coup d'huile de coude et d'heure de boulot)
$text = StringRegExpReplace($text, ", BitOR\((.*?)\)", "")
;koda à tendance à mettre des GUICtrlSetData juste en dessous plutôt que de définir l'attribut, on remet à sa place
$text = StringRegExpReplace($text, '(.*?)".*"(.*?)\r?\nGUICtrlSetData\(-1,.*?"(.*?)"\)', '$1"$3"$2')
;le nom n'est plus la variable dans laquelle on stock un ID mais un paramètre, on modifie la chaine pour
$text = StringRegExpReplace($text, '\$(.*?) = GUICtrl(.*?)\)', 'GUICtrl$2, "", "$1")')
;les fonctions se ressemblent sauf qu'elles commencent par "Form" à la place de "GUI" sinon
$text = StringReplace($text, "GUI", "Form")
;on insert le premier paramètre aux fonction qui est la variable du formulaire
$text = StringReplace($text, "(", "(" & $guiname & ", ")
;Input <> TextBox, une input contient tout et n'importe quoi (dont les boutons par exemple) alors qu'une textbox est ce que l'on cherche
$text = StringReplace($text, "FormCtrlCreateInput", "FormCtrlCreateTextBox")
;le stringreplace deux ligne plus haut est aveugle et a remplacé dans FormCreate ce qu'il ne fallait pas faire, on corige
$text = StringRegExpReplace($text, 'FormCreate\((.*?), (.*?)\)', "FormCreate($2)")
;le byref ne marchera pas à cause des call, on utilise plutôt la valeur de retour
$text = StringRegExpReplace($text, 'FormCtrl(.*?)', $guiname & " = FormCtrl$1")
;et on ajoute la fonction formend qui finalise le formulaire
$text &= @crlf & $guiname & " = FormEnd(" & $guiname & ")"
;ça c'est le "faiseur de ménage" qui supprime de l'affichage toutes les lignes qui seront de toute façon ignoré par le parseur
$ret = StringRegExp($text, '(\$.+ ?= ?.+)', 3)
$text = _ArrayToString($ret, @crlf)
return $text
EndFunc
func Generate()
;lecture ligne à ligne
$lignes = StringSplit(guictrlread($edit2), @crlf)
for $i = 1 to $lignes[0]
;retrait des whitespace de type tabulation, ils doivent être remplacé par des caractères HTML correspondant
while StringInStr($lignes[$i], " ")
$lignes[$i] = StringReplace($lignes[$i], " ", "")
WEnd
;obtention de la fonction et de la variable dans quoi stocker la valeur de retour
$funcName = StringRegExp($lignes[$i], "(?:\$.*? ?= ?)?(.*?)\(", 3)
$var = StringRegExp($lignes[$i], "(\$.*?) ?= ?", 3)
if IsArray($funcName) then
;parsing des arguments
$arg = StringRegExp($lignes[$i], "\((.*?)\)", 3)
$args = StringRegExp($arg[0], '((?:".*?")|(?:\$\w+)|(?:\d+))', 3)
;transformation en un format type stringsplit (j'ai commencer avec ça et la flemme de changer le switch, vous allez comprendre)
_ArrayInsert($args, 0, ubound($args))
;retrait des guillemets capturé
for $j = 1 to $args[0]
$args[$j] = StringReplace($args[$j], '"', '')
;juste quelques "\r\n" qui sont inutilisé, on les remplace par un "vrai" retour chariot + saut de ligne
$args[$j] = StringReplace($args[$j], "\r", @cr)
$args[$j] = StringReplace($args[$j], "\n", @lf)
Next
;traitement et génération du code
if StringLeft($args[1], 1) = "$" then $args[1] = Eval(stringreplace($args[1] , "$", ""))
Switch $args[0]
case 1
$v = Call($funcName[0], $args[1])
case 2
$v = Call($funcName[0], $args[1], $args[2])
case 3
$v = Call($funcName[0], $args[1], $args[2], $args[3])
case 4
$v = Call($funcName[0], $args[1], $args[2], $args[3], $args[4])
case 5
$v = Call($funcName[0], $args[1], $args[2], $args[3], $args[4], $args[5])
case 6
$v = Call($funcName[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6])
case 7
$v = Call($funcName[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7])
case 8
$v = Call($funcName[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8])
case 9
$v = Call($funcName[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8], $args[9])
EndSwitch
;assignation de la valeur de retour (si elle existe)
if IsArray($var) and IsDeclared("v") then
$varname = stringreplace($var[0], "$", "")
Assign($varname, $v)
EndIf ;conclusion: autoit n'aime pas la reflection, tout ce bordel pour executer un foutu morceau de script, lol
EndIf
Next
return Eval($varname)
EndFunc
while True
sleep(100)
Edit1Change()
WEnd