Simple WinAPI Library (Modified)

User projects written in or related to FreeBASIC.
Post Reply
Lothar Schirm
Posts: 462
Joined: Sep 28, 2013 15:08
Location: Germany

Simple WinAPI Library (Modified)

Post by Lothar Schirm »

As announced here (viewtopic.php?t=24617&start=60), a modified version of my library can be downloaded at FreeBASIC Portal (https://www.freebasic-portal.de/downloa ... t-403.html).

This version does not use the Windows System Dialog Box (window class "#32770"), so programming a GUI is in line with the standard procedure (registering a window class and using a window callback function (WndProc)). The syntax of the already existing library has been taken largely so that coding a GUI should be fairly simple and clear.

Code example: a simple calculator (enter two numbers, add, subtract, divide or multiply and show the result):

Code: Select all

#Include "WinLib.bi"


Dim Shared As HWND MainWindow, Text_a, Text_b, Text_c, Button_Plus, Button_Minus, Button_Mult, Button_Div


Sub Calculate(ByVal method As String)
	'Get a and b from Text boxes, calculate and set result c into Text box
	
	Dim As Single a, b, c
	
	a = Val(TextBox_GetText(Text_a))
	b = Val(TextBox_GetText(Text_b))
	
	
	Select Case method
		Case "+"
			c = a + b
		Case "-"
			c = a - b
		Case "*"
			c = a * b
		Case "/"
			c = a / b
	End Select
	
	TextBox_SetText(Text_c, Str(c))
	
End Sub
														

Function WndProc(ByVal hWnd As HWND, ByVal message As UINT, ByVal wParam As WPARAM, _
                 ByVal lParam As LPARAM ) As LRESULT
  
	Select Case message
	
		Case WM_CREATE 
	
			Var Label_a = Label_New		(10, 10, 90, 20, "Number 1:",, hWnd)  
			Text_a = TextBox_New			(100, 10, 100, 20, "0",, hWnd)	
			Var Label_b = Label_New		(10, 40, 90, 20, "Number 2:",, hWnd)
			Text_b = TextBox_New			(100, 40, 100, 20, "0",, hWnd)	
			Var Label_c = Label_New		(10, 70, 90, 20, "Result:",, hWnd)
			Text_c = TextBox_New			(100, 70, 100, 20, "0", ES_READONLY, hWnd)
			Button_Plus = Button_New	(210, 10, 20, 20, "+",, hWnd)
			Button_Minus = Button_New	(240, 10, 20, 20, "-",, hWnd)
			Button_Mult = Button_New	(210, 40, 20, 20, "*",, hWnd)
			Button_Div = Button_New		(240, 40, 20, 20, "/",, hWnd)
			
		Case WM_COMMAND
		
			Select Case lParam
				Case Button_Plus
					Calculate("+")
				Case Button_Minus
					Calculate("-")
				Case Button_Mult
					Calculate("*")
				Case Button_Div
					Calculate("/")
			End Select
			
		Case WM_DESTROY
			
			PostQuitMessage(0)
			Exit Function
			
	End Select
	
	Return DefWindowProc(hWnd, message, wParam, lParam)    
    
End Function


RegisterWindowClass("WindowClass", @WndProc)
MainWindow = Window_New("WindowClass", 200, 200, 300, 150, "Simple Calculator")
RunMessageLoop()

End
Last edited by Lothar Schirm on Oct 15, 2023 14:53, edited 1 time in total.
srvaldez
Posts: 3513
Joined: Sep 25, 2005 21:54

Re: Simple WinAPI Library (Modified)

Post by srvaldez »

thank you Lothar Schirm :D
works as advertised
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Simple WinAPI Library (Modified)

Post by RNBW »

srvaldez wrote: Oct 15, 2023 13:40 thank you Lothar Schirm :D
works as advertised
Yes, agreed. I'll check out some of the code I wrote using the old library.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI Library (Modified)

Post by cavelamb »

thanks, Lothar.
I found it now. :)
Post Reply