Jump to content

Simple calculator


Recommended Posts

Simple calculator

Perform basic arithmetic operations directly from the numeric keypad. 

Easy copy and paste (data transfer) between different applications.

 

Advanced Features   (and the reason I did it)

The calculator also includes an checkbox that enables you to apply offsets to your calculations.
For instance, using the "+23" offset will add 23 to your result, making it perfect for scenarios where you need to
adjust the position of graphical elements in a user interface (GUI).
Imagine you want to reposition existing elements by 23 pixels on the Y-axis, you can easily recalculate the new coordinates by adding an offset.

 

SciTE_eSlQc.gif


SimpleCalculator.au3

; https://www.autoitscript.com/forum/topic/211838-simple-calculator/
;----------------------------------------------------------------------------------------
; Title...........: SimpleCalculator.au3
; Description.....: Simple Calculator GUI
; AutoIt Version..: 3.3.16.1   Author: ioa747
; Note............: Testet in Win10 22H2
;----------------------------------------------------------------------------------------
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Description=Simple Calculator GUI
#AutoIt3Wrapper_Res_Fileversion=0.0.0.1
#AutoIt3Wrapper_Res_ProductName=SimpleCalculator.au3
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <EditConstants.au3>

_CalcGUI()

;--------------------------------------------------------------------------------------------------------------------------------
Func _CalcGUI()
    #Region ### START Koda GUI section ###

    Local $hCalcGui = GUICreate("🧮 Simple Calculator", 355, 75, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))

    Local $Cmd_Get = GUICtrlCreateButton(">>", 3, 4, 30, 30, $BS_ICON)
    GUICtrlSetTip(-1, "Get")

    Local $Id_Input = GUICtrlCreateInput("", 35, 5, 285, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

    Local $Cmd_PasteIn = GUICtrlCreateButton("", 322, 4, 30, 30, $BS_ICON)
    GUICtrlSetImage(-1, "shell32.dll", -261)
    GUICtrlSetTip(-1, "Paste")

    Local $Cmd_Set = GUICtrlCreateButton("<<", 3, 39, 30, 30, $BS_ICON)
    GUICtrlSetTip(-1, "Set")

    Local $Id_Output = GUICtrlCreateLabel("", 35, 40, 265, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetBkColor(-1, 0xE1E1E1)

    Local $Cmd_CopyOut = GUICtrlCreateButton("", 322, 39, 30, 30, $BS_ICON)
    GUICtrlSetImage(-1, "shell32.dll", -135)
    GUICtrlSetTip(-1, "Copy")

    Local $Id_AutoCalc = GUICtrlCreateInput("", 186, 40, 115, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetState(-1, $GUI_HIDE)
    Local $id_Checkbox = GUICtrlCreateCheckbox("", 305, 40, 17, 17)
    GUICtrlSetTip(-1, "Auto Calc")

    ; Set focus to the $Id_Input control.
    GUICtrlSetState($Id_Input, $GUI_FOCUS)

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    Local $nMsg, $hActiveWin, $sActiveTitle, $sOut, $sAuto, $ClipBack

    Local $iSlp = 10 ;Sleep between copy-paste, increase if necessary * <-

    ;**********************************
    While 1
        If WinGetHandle("[ACTIVE]") <> $hActiveWin And WinGetHandle("[ACTIVE]") <> $hCalcGui Then
            $hActiveWin = WinGetHandle("[ACTIVE]")
            $sActiveTitle = WinGetTitle($hActiveWin)
            ConsoleWrite("$sActiveTitle=" & $sActiveTitle & @CRLF) ;***
        EndIf

        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $id_Checkbox
                $sOut = GUICtrlRead($Id_Input)
                If _IsChecked($id_Checkbox) Then
                    GUICtrlSetState($Id_AutoCalc, $GUI_SHOW)
                    GUICtrlSetPos($Id_Output, 35, 40, 145, 28)
                    $sAuto = GUICtrlRead($Id_AutoCalc)
                    $sOut = GUICtrlRead($Id_Input)
                    GUICtrlSetData($Id_Output, Execute("(" & $sOut & ") " & $sAuto))
                    ControlFocus($hCalcGui, "", $Id_AutoCalc)
                Else
                    GUICtrlSetState($Id_AutoCalc, $GUI_HIDE)
                    GUICtrlSetPos($Id_Output, 35, 40, 265, 28)
                    $sOut = Execute($sOut)
                    GUICtrlSetData($Id_Output, $sOut)
                    ControlFocus($hCalcGui, "", $Id_Input)
                EndIf

            Case $Id_Input, $Id_AutoCalc
                $sOut = GUICtrlRead($Id_Input)
                If GUICtrlRead($id_Checkbox) = $GUI_CHECKED Then
                    $sAuto = GUICtrlRead($Id_AutoCalc)
                    $sOut = GUICtrlRead($Id_Input)
                    GUICtrlSetData($Id_Output, Execute("(" & $sOut & ") " & $sAuto))
                Else
                    $sOut = Execute($sOut)
                    GUICtrlSetData($Id_Output, $sOut)
                EndIf
                GUICtrlSetState($Cmd_Set, $GUI_FOCUS)

            Case $Cmd_PasteIn
                GUICtrlSetData($Id_Input, ClipGet())

            Case $Cmd_CopyOut
                ClipPut(GUICtrlRead($Id_Output))

            Case $Cmd_Get
                $ClipBack = ClipGet() ; backup clip data
                ; Add new data to the clipboard.
                Sleep($iSlp)
                WinActivate($hActiveWin)
                Send("^c") ; copy
                Sleep($iSlp)
                GUICtrlSetData($Id_Input, ClipGet())
                ClipPut($ClipBack) ; restore clip data

                GUICtrlSetData($Id_Output, "")

                If GUICtrlRead($id_Checkbox) = $GUI_CHECKED Then
                    $sAuto = GUICtrlRead($Id_AutoCalc)
                    $sOut = GUICtrlRead($Id_Input)
                    GUICtrlSetData($Id_Output, Execute("(" & $sOut & ") " & $sAuto))
                    GUICtrlSetState($Cmd_Set, $GUI_FOCUS)
                Else
                    ControlFocus($hCalcGui, "", $Id_Input)
                    WinActivate($hCalcGui)
                EndIf

            Case $Cmd_Set
                $sOut = GUICtrlRead($Id_Output)
                If $sOut Then
                    $ClipBack = ClipGet() ; backup clip data
                    ; Add new data to the clipboard.
                    ClipPut($sOut)
                    Sleep($iSlp)
                    WinActivate($hActiveWin)
                    Send("^v") ; paste
                    ClipPut($ClipBack) ; restore clip data
                EndIf

        EndSwitch

;~      Sleep(50)
    WEnd
    ;**********************************

EndFunc   ;==>_CalcGUI
;--------------------------------------------------------------------------------------------------------------------------------
Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked
;--------------------------------------------------------------------------------------------------------------------------------

Please, every comment is appreciated!
leave your comments and experiences here!
Thank you very much  :)

 

Edited by ioa747
add .gif

I know that I know nothing

Link to comment
Share on other sites

This code provides a simple graphical interface for a calculator, allowing operations such as copy, paste, and automatic calculation. However, it lacks comments to explain the different sections and functions. Additionally, user input validation is missing, which could lead to errors if non-numeric data is entered. Better code organization with explanatory comments and the addition of validation mechanisms would enhance its readability and robustness.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...