-- TIME_STAMP 2013-10-28 10:07:54 local vcl = require "vcl" -- Objekt Rechner local oCalc = { operation = '', firstVal = 0, secondVal = 0, add = function(self) return (self.firstVal + self.secondVal) end, sub = function(self) return (self.firstVal - self.secondVal) end, mul = function(self) return (self.firstVal * self.secondVal) end, div = function(self) local res if self.secondVal == 0 then res = '' else res = (self.firstVal / self.secondVal) end return res end, valid = function(self) if not tonumber(self.firstVal) or not tonumber(self.secondVal) then return 'Error - no value' end end } -- Objekt Button Class local buttonDefault = { caption = '', name = '', id = nil, done = false, SetFocus = function(self) self.id:SetFocus() end, Free = function(self) self.id:Free() end, SetButton = function(self, _form, _caption, ...) self.id = vcl.Button(_form) self.id._ = {left = 0, top = 0, width = 30, height = 30, caption = _caption} self.id.OnClick = "ClickButton" self.caption = _caption self.name = self.id.name if arg then local tArg = arg[1] self.id._ = tArg end end, New = function(self, obj) obj = obj or {} setmetatable(obj, self) self.__index = self return obj end } -- GUI erstellen mainForm = vcl.Form("mainForm") mainForm._ = { caption = "Rechner", position="poDesktopCenter", height=235, width=155, cursor="crHourGlass", color = 0xFFF8F0 } mainForm.Image = "C:/Users/BugFix/Pictures/ICON/Icons-ico/Computer/Windows 95 - Arbeitsplatz.ico" -- Label für Anzeige erstellen local label1 = vcl.Label(mainForm) local label2 = vcl.Label(mainForm) label1._ = {autoSize=false, alignment='taRightJustify', layout='tlCenter', caption='', left=10, top=10, height=20, width=135, color=0xE0FFFF} label2._ = {autoSize=false, alignment='taRightJustify', layout='tlCenter', caption='', left=10, top=30, height=20, width=135, color=0xE0FFEE} -- Digit-Button erstellen, named index "d0...d9" local iLeft, iTop, tBtn, index = 10, 55, {} for i = 9,0,-1 do if i % 3 == 0 then iLeft = 80 iTop = iTop +35 end index = tostring('d'..i) tBtn[index] = buttonDefault:New() tBtn[index]:SetButton(mainForm, tostring(i), {left=iLeft, top=iTop}) iLeft = iLeft - 35 end tBtn.d0.id._ = {width = 65, left = 10} tBtn.dot = buttonDefault:New() tBtn.dot:SetButton(mainForm, ',', {left=iLeft+35, top=iTop}) -- Operation-Button erstellen iLeft = 10 iTop = 55 local tOps = {{'back','back',0,0},{'div','/',35,0},{'mul','x',70,0},{'clear','C',105,0},{'sub','-',105,35},{'add','+',105,70},{'calc','=',105,105}} for i = 1,#tOps do tBtn[tOps[i][1] ] = buttonDefault:New() tBtn[tOps[i][1] ]:SetButton(mainForm, tOps[i][2], {left=iLeft+tOps[i][3], top=iTop+tOps[i][4]}) end tBtn.calc.id.height = 65 -- Event-Funktionen function ClickButton(Sender) local set = function(_l, _v) _l.caption = _v _l:Refresh() end local get = function(_l) return _l.caption end local s = Sender.caption local g if ('0123456789'):find(s) then if oCalc.done then set(label2, '') oCalc.done = false end set(label2, get(label2)..s) elseif ('%+%-x/'):find(s) then oCalc.operation = s oCalc.firstVal = get(label2) if oCalc.done then oCalc.done = false set(label1, get(label1)..' '..s) else set(label1, get(label2)..' '..s) end set(label2, '') elseif s == ',' then set(label2, get(label2)..'.') elseif s == 'back' then g = get(label2) if g == '' then return end set(label2, g:sub(1, #g-1)) elseif s == 'C' then set(label2, '') elseif s == '=' then local op = oCalc.operation set(label1, get(label1)..' '..get(label2)) oCalc.secondVal = get(label2) if op == '+' then set(label2, oCalc:add() ) elseif op == '-' then set(label2, oCalc:sub() ) elseif op == 'x' then set(label2, oCalc:mul() ) elseif op == '/' then set(label2, oCalc:div() ) end oCalc.done = true end end function onCloseQueryEventHandler(Sender) return true end -- GUI anzeigen mainForm:ShowModal() -- Speicher freigeben mainForm:Free()