Code: Select all
'
' Freebasic test code, 16-03-2024, 23-03-2024
' calculator gui, by frank bruebach
#include "windows.bi"
' Constants for controls
Const IDC_NUM1 = 1001
Const IDC_NUM2 = 1002
Const IDC_RESULT = 1003
Const IDC_ADD = 2001
Const IDC_SUBTRACT = 2002
Const IDC_MULTIPLY = 2003
Const IDC_DIVIDE = 2004
Const IDC_CALCULATE = 2005
Function WndProc(ByVal hWnd As HWND, ByVal uMsg As UINT, ByVal wParam As WPARAM, ByVal lParam As LPARAM) As LRESULT
Select Case uMsg
Case WM_CREATE
' Create edit boxes for numbers
CreateWindowEx(0, "EDIT", "", WS_CHILD Or WS_VISIBLE Or WS_BORDER Or ES_MULTILINE Or ES_AUTOHSCROLL, 50, 50, 100, 20, hWnd, IDC_NUM1, GetModuleHandle(Null), ByVal Null)
CreateWindowEx(0, "EDIT", "", WS_CHILD Or WS_VISIBLE Or WS_BORDER Or ES_MULTILINE Or ES_AUTOHSCROLL, 200, 50, 100, 20, hWnd, IDC_NUM2, GetModuleHandle(Null), ByVal Null)
' Create radio buttons for operations
CreateWindowEx(0, "BUTTON", "+", WS_CHILD Or WS_VISIBLE Or BS_AUTORADIOBUTTON, 50, 100, 50, 20, hWnd, IDC_ADD, GetModuleHandle(Null), ByVal Null)
CreateWindowEx(0, "BUTTON", "-", WS_CHILD Or WS_VISIBLE Or BS_AUTORADIOBUTTON, 100, 100, 50, 20, hWnd, IDC_SUBTRACT, GetModuleHandle(Null), ByVal Null)
CreateWindowEx(0, "BUTTON", "*", WS_CHILD Or WS_VISIBLE Or BS_AUTORADIOBUTTON, 150, 100, 50, 20, hWnd, IDC_MULTIPLY, GetModuleHandle(Null), ByVal Null)
CreateWindowEx(0, "BUTTON", "/", WS_CHILD Or WS_VISIBLE Or BS_AUTORADIOBUTTON, 200, 100, 50, 20, hWnd, IDC_DIVIDE, GetModuleHandle(Null), ByVal Null)
' Create button for calculation
CreateWindowEx(0, "BUTTON", "=", WS_CHILD Or WS_VISIBLE Or BS_AUTORADIOBUTTON, 250, 100, 50, 20, hWnd, IDC_CALCULATE, GetModuleHandle(Null), ByVal Null)
' Create edit box for result
CreateWindowEx(0, "EDIT", "", WS_CHILD Or WS_VISIBLE Or WS_BORDER Or ES_MULTILINE Or ES_READONLY, 50, 150, 250, 20, hWnd, IDC_RESULT, GetModuleHandle(Null), ByVal Null)
' Set default operation to addition
SendDlgItemMessage(hWnd, IDC_ADD, BM_SETCHECK, BST_CHECKED, 0)
Case WM_COMMAND
If HIWORD(wParam) = BN_CLICKED Then
dim operation As String
' Get selected operation
Select Case LOWORD(wParam)
Case IDC_ADD
operation = "+"
Case IDC_SUBTRACT
operation = "-"
Case IDC_MULTIPLY
operation = "*"
Case IDC_DIVIDE
operation = "/"
Case IDC_CALCULATE
Dim num1 As Double
Dim num2 As Double
Dim result As Double
dim as string snum1=space(21)
dim as string snum2=space(21)
dim as string sresult=space(21)
GetDlgItemText(hWnd, IDC_NUM1, strptr(snum1), 20)
GetDlgItemText(hWnd, IDC_NUM2, strptr(snum2), 20)
num1= val(snum1)
num2= val(snum2)
' Perform calculation based on selected operation
Select Case operation
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
If num2 <> 0 Then
result = num1 / num2
Else
Print "Cannot divide by zero!", , "Error"
Exit Function
End If
case else
''default addition otherwise result = zero
result = num1 + num2
End Select
sresult=str(result)
' Display result
SetDlgItemText(hWnd, IDC_RESULT, Strptr(sresult))
End Select
End If
Case WM_CLOSE
DestroyWindow(hWnd)
Case WM_DESTROY
PostQuitMessage(0)
Case Else
Return DefWindowProc(hWnd, uMsg, wParam, lParam)
End Select
Return 0
End Function
Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInstance As HINSTANCE, ByVal lpCmdLine As LPSTR, ByVal nCmdShow As Integer) As Integer
Dim wc As WNDCLASSEX
Dim hWnd As HWND
Dim msg As MSG
' Register window class
wc.cbSize = SizeOf(WNDCLASSEX)
wc.style = CS_HREDRAW Or CS_VREDRAW
wc.lpfnWndProc = @WndProc
wc.cbClsExtra = 0
wc.cbWndExtra = 0
wc.hInstance = hInstance
wc.hIcon = LoadIcon(Null, IDI_APPLICATION)
wc.hIconSm = LoadIcon(Null, IDI_APPLICATION)
wc.hCursor = LoadCursor(Null, IDC_ARROW)
wc.hbrBackground = GetStockObject(WHITE_BRUSH)
wc.lpszMenuName = Null
wc.lpszClassName = StrPtr("InputMaskClass")
If RegisterClassEx(@wc) = 0 Then
Print "Window Registration Failed!", , "Error"
Function = 0
Exit Function
End If
' Create the window
hWnd = CreateWindowEx(0, StrPtr("InputMaskClass"), StrPtr("Input Mask Example"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, Null, Null, hInstance, Null)
If hWnd = 0 Then
Print "Window Creation Failed!", , "Error"
Function = 0
Exit Function
End If
' Show and update the window
ShowWindow(hWnd, nCmdShow)
UpdateWindow(hWnd)
' Message loop
While GetMessage(@msg, Null, 0, 0) > 0
TranslateMessage(@msg)
DispatchMessage(@msg)
Wend
Function = msg.wParam
End Function
WinMain(GetModuleHandle(Null), Null, Command(), SW_SHOWNORMAL)