Hello everyone, if you can help me with a small example

Windows specific questions.
Post Reply
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

Hello everyone, if you can help me with a small example

Post by Tolo68 »

I need a small example of properties and events, since I hardly find examples

A Form.bi file that creates a form with an OnClick and OnMouseMove event

A Button.bi file that creates a Button with an OnClick event and allows me to position it with Left and Top properties in the Form

It does not need to have more properties, so I will understand how the events work beforehand

All this with the this method, for example

OnClick as Sub
if OnClick then OnClick(this)

And let them run me the procedures...
Form1_click(), Form2_click()
Button1_click(), Button2_click() etc

Tell me which button I clicked, if there are several buttons

Thank you very much to all
Lothar Schirm
Posts: 438
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Hello everyone, if you can help me with a small example

Post by Lothar Schirm »

Events and properties are not so difficult to understand if you only need some simple controls. Example:

Code: Select all

'===============================================================================
' SimpleGUI(1).bas
' 18 March, 2023
'===============================================================================

#Include "windows.bi"

Dim Shared As HWND Btn1, Btn2, BtnClose, Edit1


Function WndProc(ByVal hWnd As HWND, ByVal Msg As UINT, ByVal wParam As WPARAM, _
                 ByVal lParam As LPARAM ) As LRESULT
  
 	Function = 0
	
	Select Case Msg
	
		Case WM_CREATE            
			Edit1 = CreateWindow("EDIT", "No button clicked up to now", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, 20, 20, 200, 20, hWnd, 0, 0, 0)
			Btn1 = CreateWindow("BUTTON", "Button 1", WS_CHILD Or WS_VISIBLE, 80, 50, 80, 20, hWnd, 0, 0, 0)
			Btn2 = CreateWindow("BUTTON", "Button 2", WS_CHILD Or WS_VISIBLE, 80, 80, 80, 20, hWnd, 0, 0, 0)
			BtnClose = CreateWindow("BUTTON", "Close", WS_CHILD Or WS_VISIBLE, 80, 110, 80, 20, hWnd, 0, 0, 0)
					
		Case WM_COMMAND
			Select Case HiWord(wParam)
				Case BN_CLICKED
					Select Case lParam
						Case Btn1
							SetWindowText(Edit1, "Button 1 was clicked")
						Case Btn2
							SetWindowText(Edit1, "Button 2 was clicked")
						Case BtnClose
							SendMessage(hWnd, WM_CLOSE, 0, 0)
				End Select
			End Select
					
		Case WM_DESTROY
			PostQuitMessage(0)
			Exit Function
			
	End Select
	
	Return DefWindowProc(hWnd, Msg, wParam, lParam)    
    
End Function


Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInstance As HINSTANCE, ByVal szCmdLine As zstring ptr, ByVal iCmdShow As integer) As integer    
     
	Dim Msg As MSG
	Dim wcls As WNDCLASS     
	Dim hWnd As HWND
	
	Function = 0
	 
	with wcls
		.style         = CS_HREDRAW or CS_VREDRAW
		.lpfnWndProc   = @WndProc
		.cbClsExtra    = 0
		.cbWndExtra    = 0
		.hInstance     = hInstance
		.hIcon         = LoadIcon(0, IDI_APPLICATION)
		.hCursor       = LoadCursor(0, IDC_ARROW)
		.hbrBackground = Cast(HBRUSH, COLOR_WINDOW)
		.lpszMenuName  = 0
		.lpszClassName = @"MainWindow"
	End with
				
	If(RegisterClass(@wcls) = FALSE) Then
		 MessageBox(0, "Failed to register wcls", "Error", MB_ICONERROR)
		 Exit Function
	End If
	
	'Fenster:
	hWnd = CreateWindowEx(0, @"MainWindow", "Simple Window", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 300, 200, 0, 0, hInstance, 0)
		
	ShowWindow(hWnd, iCmdShow)
	UpdateWindow(hWnd)
	 
	While( GetMessage(@Msg, 0, 0, 0) <> FALSE)    
		TranslateMessage(@Msg)
		DispatchMessage(@Msg)
	Wend
	
	Return Msg.wParam

End Function

WinMain(GetModuleHandle(0), 0, Command(), SW_NORMAL)

End
An other example using also a menu, a multiline editbox and a listbox: viewtopic.php?p=281398&hilit=windows+gu ... te#p281398

More simple example, using the System Dialogbox (Window class "#32770"):

Code: Select all

'===============================================================================
' SimpleGUI(2).bas
' May 18, 2023
'===============================================================================

#Include "windows.bi"

Dim As HWND MainWindow, Btn1, Btn2, BtnClose, Edit1
Dim As MSG msg

MainWindow = CreateWindow("#32770", "Simple Window", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 300, 200, 0, 0, 0, 0)
Edit1 = CreateWindow("EDIT", "No button clicked up to now", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, 20, 20, 200, 20, MainWindow, 0, 0, 0)
Btn1 = CreateWindow("BUTTON", "Button 1", WS_CHILD Or WS_VISIBLE, 80, 50, 80, 20, MainWindow, 0, 0, 0)
Btn2 = CreateWindow("BUTTON", "Button 2", WS_CHILD Or WS_VISIBLE, 80, 80, 80, 20, MainWindow, 0, 0, 0)
BtnClose = CreateWindow("BUTTON", "Close", WS_CHILD Or WS_VISIBLE, 80, 110, 80, 20, MainWindow, 0, 0, 0)

Do
  GetMessage(@msg, 0, 0, 0)
  TranslateMessage(@msg)
  DispatchMessage(@msg)
  
  Select Case msg.message
    Case WM_LBUTTONDOWN
			Select Case msg.hwnd
				Case Btn1
					SetWindowText(Edit1, "Button 1 was clicked")
				Case Btn2
					SetWindowText(Edit1, "Button 2 was clicked")
				Case BtnClose
					Exit Do
				End Select
  End Select
  
Loop Until msg.message = WM_COMMAND

End
Further examples: viewtopic.php?t=13451&hilit=easy+windows+api
Post Reply