Voici une petite calculatrice, qui était mon premier programme en AutoIt.
C'est une version très minimaliste, car il y a encore beaucoup de chose à améliorer:
- Gérer plus de 2 nombres.
- Gérer les expression numériques.
- Ajouter des fonctions mathématiques. (Mode Scientifique)
- Ajouter un menu Options. (Les couleurs, le nombre de chiffres après la virgule...)
Code : Tout sélectionner
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
; // Centrage de la GUI à l'écran //
$Pos_Largeur = (@DesktopWidth - 168)/2
$Pos_Hauteur = (@DesktopHeight - 238)/2
Local $CalcText1 = ""
Local $CalcText2 = ""
Local $Nombre = 1
Local $Operation = ""
$GUI = GUICreate("Calculatrice", 168, 238, $Pos_Largeur, $Pos_Hauteur)
GUISetBkColor(0x33CCFF, $GUI)
$NoFocus = GUICtrlCreateLabel("", 0, 0, 0, 0)
; // Bouton Difference //
$ButtonDifference = GUICtrlCreateButton("-", 128, 160, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
GUICtrlSetState($NoFocus, $GUI_FOCUS)
; // Bouton Addition //
$ButtonAddition = GUICtrlCreateButton("+", 128, 200, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
; // Bouton Division //
$ButtonDivision = GUICtrlCreateButton("/", 128, 80, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
; // Bouton Multiplication //
$ButtonMultiplication = GUICtrlCreateButton("*", 128, 120, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
; // Bouton 08 //
$Button08 = GUICtrlCreateButton("8", 48, 80, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 09 //
$Button09 = GUICtrlCreateButton("9", 88, 80, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 04 //
$Button04 = GUICtrlCreateButton("4", 8, 120, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 05 //
$Button05= GUICtrlCreateButton("5", 48, 120, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 06 //
$Button06 = GUICtrlCreateButton("6", 88, 120, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 07 //
$Button07 = GUICtrlCreateButton("7", 8, 80, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 00 //
$Button00 = GUICtrlCreateButton("0", 8, 200, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 01 //
$Button01 = GUICtrlCreateButton("1", 8, 160, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 02 //
$Button02 = GUICtrlCreateButton("2", 48, 160, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton 03 //
$Button03 = GUICtrlCreateButton("3", 88, 160, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton Virgule
$ButtonVirgule = GUICtrlCreateButton(".", 48, 200, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
; // Bouton Egale //
$ButtonEqual = GUICtrlCreateButton("=", 88, 200, 32, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
; // Bouton AC //
$ButtonAC = GUICtrlCreateButton("AC", 8, 40, 72, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
; // Boutton OFF //
$ButtonOff = GUICtrlCreateButton("OFF", 88, 40, 72, 32)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetColor(-1, 0x000080)
; // Zone de Texte //
$TextBox = GUICtrlCreateInput("", 8, 8, 152, 26, 2)
GUICtrlSetFont(-1, 16, 1, "Arial")
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)
While 1
$Controle = GUIGetMsg()
Switch $Controle
Case $GUI_EVENT_CLOSE
Exit
Case $ButtonOff
Exit
Case $Button00
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "0"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "0"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button01
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "1"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "1"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button02
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "2"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "2"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button03
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "3"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "3"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button04
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "4"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "4"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button05
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "5"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "5"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button06
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "6"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "6"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button07
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "7"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "7"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button08
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "8"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "8"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button09
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "9"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "9"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $Button00
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "0"
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "0"
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $ButtonVirgule
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Nombre = 1 Then
$CalcText1 = $CalcText1 & "."
GUICtrlSetData($TextBox, $CalcText1)
Else
$CalcText2 = $CalcText2 & "."
GUICtrlSetData($TextBox, $CalcText2)
EndIf
Case $ButtonAC
GUICtrlSetState($NoFocus, $GUI_FOCUS)
GUICtrlSetData($TextBox, "")
Local $CalcText1 = ""
Local $CalcText2 = ""
Local $Nombre = 1
Local $Operation = ""
Case $ButtonAddition
GUICtrlSetState($NoFocus, $GUI_FOCUS)
Local $Operation = 1
Local $Nombre = 2
GUICtrlSetData($TextBox, "")
Case $ButtonDifference
GUICtrlSetState($NoFocus, $GUI_FOCUS)
Local $Operation = 2
Local $Nombre = 2
GUICtrlSetData($TextBox, "")
Case $ButtonMultiplication
GUICtrlSetState($NoFocus, $GUI_FOCUS)
Local $Operation = 3
Local $Nombre = 2
GUICtrlSetData($TextBox, "")
Case $ButtonDivision
GUICtrlSetState($NoFocus, $GUI_FOCUS)
Local $Operation = 4
Local $Nombre = 2
GUICtrlSetData($TextBox, "")
Case $ButtonEqual
GUICtrlSetState($NoFocus, $GUI_FOCUS)
If $Operation = 1 Then Local $Resultat = $CalcText1 + $CalcText2
If $Operation = 2 Then Local $Resultat = $CalcText1 - $CalcText2
If $Operation = 3 Then Local $Resultat = $CalcText1 * $CalcText2
If $Operation = 4 Then Local $Resultat = $CalcText1 / $CalcText2
Local $Arround = Round($Resultat, 2)
GUICtrlSetData($TextBox, $Arround)
EndSwitch
WEnd