Skip to content

Commit 7a90adc

Browse files
authored
AutoHotKey : GUI Calculator (#300)
* Create GUICalculator.ahk * Upload GUICalculator.gif * Upload GUICalculator_TestMode.gif * Update README.md * Update /README.md * Delete /GithubDashboard/ * Delete /images/
1 parent 2659389 commit 7a90adc

File tree

7 files changed

+416
-128
lines changed

7 files changed

+416
-128
lines changed

AutoHotKey/GUICalculator.ahk

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
; GUI Calculator
2+
; 2024.01.13.
3+
4+
5+
6+
; Test mode
7+
8+
Test := false
9+
10+
11+
; Set GUI
12+
13+
if (Test = true)
14+
{
15+
Gui, Add, Text, x2 y2 w200 h20 vDisplayText, ; Display box
16+
Gui, Add, Text, x2 y22 w200 h20 vDebugText, ; Debug box
17+
}
18+
else
19+
{
20+
Gui, Font, s20
21+
Gui, Add, Text, x12 y9 w180 h40 vDisplayText, ; Display box
22+
}
23+
24+
Gui, Font, s16
25+
Gui, Add, Button, x2 y49 w50 h50 gBtn, 7
26+
Gui, Add, Button, x52 y49 w50 h50 gBtn, 8
27+
Gui, Add, Button, x102 y49 w50 h50 gBtn, 9
28+
Gui, Add, Button, x152 y49 w50 h50 gBtn, +
29+
30+
Gui, Add, Button, x2 y99 w50 h50 gBtn, 4
31+
Gui, Add, Button, x52 y99 w50 h50 gBtn, 5
32+
Gui, Add, Button, x102 y99 w50 h50 gBtn, 6
33+
Gui, Add, Button, x152 y99 w50 h50 gBtn, -
34+
35+
Gui, Add, Button, x2 y149 w50 h50 gBtn, 1
36+
Gui, Add, Button, x52 y149 w50 h50 gBtn, 2
37+
Gui, Add, Button, x102 y149 w50 h50 gBtn, 3
38+
Gui, Add, Button, x152 y149 w50 h50 gBtn, *
39+
40+
Gui, Add, Button, x2 y199 w50 h50 gBtn cRed, C
41+
Gui, Add, Button, x52 y199 w50 h50 gBtn, 0
42+
Gui, Add, Button, x102 y199 w50 h50 gBtn, =
43+
Gui, Add, Button, x152 y199 w50 h50 gBtn, /
44+
45+
Gui, Show, w204 h250, Calculator ; Control the window size
46+
47+
48+
; Decalre variables
49+
50+
Num1 := ""
51+
Num2 := ""
52+
Operator := ""
53+
Answer := 0
54+
JustAnswered := false
55+
EMPTY := ""
56+
57+
return
58+
59+
60+
Btn:
61+
62+
CurrentText := A_GuiControl ; A_GuiControl : Get the text on the each button
63+
64+
if (CurrentText = "C") ; `C` : Reset
65+
{
66+
GuiControl,, DisplayText,
67+
Num1 := ""
68+
Num2 := ""
69+
Answer := 0
70+
Operator := ""
71+
JustAnswered := false
72+
DisplayText := ""
73+
}
74+
else if (CurrentText = "=") ; `=` : Do operation
75+
{
76+
if (Num2 != EMPTY and Operator != EMPTY) ; "" can be used only with `:=`!
77+
{
78+
Num1 := Num1 + 0 ; Convert `Num1`, `Num2` from String to Integer
79+
Num2 := Num2 + 0
80+
81+
if (Operator = "+")
82+
Answer := Num1 + Num2
83+
else if (Operator = "-")
84+
Answer := Num1 - Num2
85+
else if (Operator = "*")
86+
Answer := Num1 * Num2
87+
else ; The same with `else if (Operator = "/")`
88+
Answer := Num1 / Num2
89+
90+
GuiControl,, DisplayText, % Answer ; <Answer>
91+
;~ MsgBox, % "Answer : " Answer ; Ok
92+
Num1 := Answer ; Succeed `Answer` through `Num1`
93+
Num2 := "" ; and be ready to get `Num2` for the new operation
94+
Operator := ""
95+
JustAnswered := true
96+
}
97+
; Do nothing when `Num2` and `Operator` are empty
98+
}
99+
else if InStr("+-*/", CurrentText) ; `+-*/` : Determine the kind of operation
100+
{
101+
if (Num1 != EMPTY)
102+
{
103+
Operator := CurrentText
104+
JustAnswered := false
105+
if (Num2 != EMPTY)
106+
Num2 := ""
107+
108+
GuiControl,, DisplayText, % DisplayText := Num1 " " Operator " " ; Append `Operator` into `DisplayText`
109+
}
110+
; Do nothing when `Num1` is still empty
111+
}
112+
else ; `0~9` : Get a number
113+
{
114+
if (Operator = EMPTY)
115+
{
116+
if (JustAnswered)
117+
{
118+
Num1 := ""
119+
Answer := 0
120+
JustAnswered := false
121+
DisplayText := ""
122+
}
123+
124+
Num1 .= CurrentText
125+
}
126+
else
127+
Num2 .= CurrentText
128+
129+
GuiControl,, DisplayText, % DisplayText .= CurrentText ; Append `CurrentText` into `DisplayText`
130+
}
131+
132+
if (Test = true)
133+
GuiControl,, DebugText, % CurrentText " / " JustAnswered " / " Num1 " " Operator " " Num2 " = " Answer
134+
135+
return
136+
137+
138+
GuiClose:
139+
140+
ExitApp

AutoHotKey/Images/GUICalculator.gif

282 KB
Loading
734 KB
Loading

AutoHotKey/README.md

+191-29
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,209 @@
1-
# My Auto Hot Key Practice
1+
# [My Auto Hot Key Practice](../README.md#my-auto-hot-key-practice)
2+
3+
Boys be ambitious …… !
4+
25

36
### \<List>
7+
8+
- [GUI Calculator (2024.01.13)](#gui-calculator-20240113)
49
- [Hello World (2022.09.03)](#hello-world-20220903)
510
- [Infinity (2021.12.07)](#infinity-20211207)
611

712

13+
## [GUI Calculator (2024.01.13)](#list)
14+
15+
- A Practice of GUI coding with *AutoHotKey*
16+
- Can choose Real / Test mode
17+
18+
![GUI Calculator](./Images/GUICalculator.gif)
19+
&nbsp;&nbsp;
20+
![GUI Calculator (Test Mode)](./Images/GUICalculator_TestMode.gif)
21+
22+
- Future Improvements
23+
- Improved design: Add colors, fonts, etc.
24+
- When `A opr. B opr.` is entered, immediately provide calculation results of `A opr. B`
25+
26+
- Code
27+
<details>
28+
<summary>GUICalculator.ahk</summary>
29+
30+
```ahk
31+
; Test mode
32+
33+
Test := false
34+
```
35+
```ahk
36+
; Set GUI
37+
38+
if (Test = true)
39+
{
40+
Gui, Add, Text, x2 y2 w200 h20 vDisplayText, ; Display box
41+
Gui, Add, Text, x2 y22 w200 h20 vDebugText, ; Debug box
42+
}
43+
else
44+
{
45+
Gui, Font, s20
46+
Gui, Add, Text, x12 y9 w180 h40 vDisplayText, ; Display box
47+
}
48+
49+
Gui, Font, s16
50+
Gui, Add, Button, x2 y49 w50 h50 gBtn, 7
51+
Gui, Add, Button, x52 y49 w50 h50 gBtn, 8
52+
Gui, Add, Button, x102 y49 w50 h50 gBtn, 9
53+
Gui, Add, Button, x152 y49 w50 h50 gBtn, +
54+
55+
Gui, Add, Button, x2 y99 w50 h50 gBtn, 4
56+
Gui, Add, Button, x52 y99 w50 h50 gBtn, 5
57+
Gui, Add, Button, x102 y99 w50 h50 gBtn, 6
58+
Gui, Add, Button, x152 y99 w50 h50 gBtn, -
59+
60+
Gui, Add, Button, x2 y149 w50 h50 gBtn, 1
61+
Gui, Add, Button, x52 y149 w50 h50 gBtn, 2
62+
Gui, Add, Button, x102 y149 w50 h50 gBtn, 3
63+
Gui, Add, Button, x152 y149 w50 h50 gBtn, *
64+
65+
Gui, Add, Button, x2 y199 w50 h50 gBtn cRed, C
66+
Gui, Add, Button, x52 y199 w50 h50 gBtn, 0
67+
Gui, Add, Button, x102 y199 w50 h50 gBtn, =
68+
Gui, Add, Button, x152 y199 w50 h50 gBtn, /
69+
70+
Gui, Show, w204 h250, Calculator ; Control the window size
71+
```
72+
```ahk
73+
; Decalre variables
74+
75+
Num1 := ""
76+
Num2 := ""
77+
Operator := ""
78+
Answer := 0
79+
JustAnswered := false
80+
EMPTY := ""
81+
82+
return
83+
```
84+
```ahk
85+
Btn:
86+
87+
CurrentText := A_GuiControl ; A_GuiControl : Get the text on the each button
88+
89+
if (CurrentText = "C") ; `C` : Reset
90+
{
91+
GuiControl,, DisplayText,
92+
Num1 := ""
93+
Num2 := ""
94+
Answer := 0
95+
Operator := ""
96+
JustAnswered := false
97+
DisplayText := ""
98+
}
99+
else if (CurrentText = "=") ; `=` : Do operation
100+
{
101+
if (Num2 != EMPTY and Operator != EMPTY) ; "" can be used only with `:=`!
102+
{
103+
Num1 := Num1 + 0 ; Convert `Num1`, `Num2` from String to Integer
104+
Num2 := Num2 + 0
105+
106+
if (Operator = "+")
107+
Answer := Num1 + Num2
108+
else if (Operator = "-")
109+
Answer := Num1 - Num2
110+
else if (Operator = "*")
111+
Answer := Num1 * Num2
112+
else ; The same with `else if (Operator = "/")`
113+
Answer := Num1 / Num2
114+
115+
GuiControl,, DisplayText, % Answer ; <Answer>
116+
;~ MsgBox, % "Answer : " Answer ; Ok
117+
Num1 := Answer ; Succeed `Answer` through `Num1`
118+
Num2 := "" ; and be ready to get `Num2` for the new operation
119+
Operator := ""
120+
JustAnswered := true
121+
}
122+
; Do nothing when `Num2` and `Operator` are empty
123+
}
124+
else if InStr("+-*/", CurrentText) ; `+-*/` : Determine the kind of operation
125+
{
126+
if (Num1 != EMPTY)
127+
{
128+
Operator := CurrentText
129+
JustAnswered := false
130+
if (Num2 != EMPTY)
131+
Num2 := ""
132+
133+
GuiControl,, DisplayText, % DisplayText := Num1 " " Operator " " ; Append `Operator` into `DisplayText`
134+
}
135+
; Do nothing when `Num1` is still empty
136+
}
137+
else ; `0~9` : Get a number
138+
{
139+
if (Operator = EMPTY)
140+
{
141+
if (JustAnswered)
142+
{
143+
Num1 := ""
144+
Answer := 0
145+
JustAnswered := false
146+
DisplayText := ""
147+
}
148+
149+
Num1 .= CurrentText
150+
}
151+
else
152+
Num2 .= CurrentText
153+
154+
GuiControl,, DisplayText, % DisplayText .= CurrentText ; Append `CurrentText` into `DisplayText`
155+
}
156+
157+
if (Test = true)
158+
GuiControl,, DebugText, % CurrentText " / " JustAnswered " / " Num1 " " Operator " " Num2 " = " Answer
159+
160+
return
161+
```
162+
```ahk
163+
GuiClose:
164+
165+
ExitApp
166+
```
167+
</details>
168+
169+
8170
## [Hello World (2022.09.03)](#list)
9171

10172
- It is so simple but somewhat crazy that `LButton` always calls the message box ……
11173

12-
![Hello World](Images/HelloWorld.PNG)
174+
![Hello World](Images/HelloWorld.PNG)
13175

14-
#### `HelloWorld.ahk`
176+
#### `HelloWorld.ahk`
15177

16-
```ahk
17-
LButton::
18-
MsgBox, Hello World!
178+
```ahk
179+
LButton::
180+
MsgBox, Hello World!
19181
20-
return
21-
```
182+
return
183+
```
22184

23185
## [Infinity (2021.12.07)](#list)
24186

25187
- Welcome to `Auto Hot Key` world of mystery!
26188

27-
#### `Infinity.ahk`
28-
29-
```ahk
30-
; Test 1
31-
if (1 == 2)
32-
{
33-
MsgBox, True
34-
}
35-
else MsgBox, False
36-
```
37-
> False
38-
39-
```ahk
40-
; Test 2
41-
if (11111111111111111111111 == 22222222222222222222222)
42-
{
43-
MsgBox, True
44-
}
45-
else MsgBox, False
46-
```
47-
> True
189+
#### `Infinity.ahk`
190+
191+
```ahk
192+
; Test 1
193+
if (1 == 2)
194+
{
195+
MsgBox, True
196+
}
197+
else MsgBox, False
198+
```
199+
> False
200+
201+
```ahk
202+
; Test 2
203+
if (11111111111111111111111 == 22222222222222222222222)
204+
{
205+
MsgBox, True
206+
}
207+
else MsgBox, False
208+
```
209+
> True

GithubDashboard/README.md

-7
This file was deleted.

0 commit comments

Comments
 (0)