Simple WinAPI GUI library

User projects written in or related to FreeBASIC.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI GUI library

Post by cavelamb »

Okay.

Trying to learn some of this Windows programming stuff...

I have a stripped down template (from above) up and running with an EditBox,
and menus,
and it can load a text file into the editbox.

So, how to I access the text in the Editbox from FreeBasic?

What kind of magic Windows spell would return a single line of text?


Code: Select all

'===============================================================================
' Ed-02.bas
' Simple Text Editor
' Created on September 09, 2014
' Latest change 4/14/21
'===============================================================================

#Include "E:\Freebasic\WinGUI\WinGUI.bi"
#Include "E:\Freebasic\WinGUI\WinDialogs.bi"

Dim Shared As HWND Window_Main, Box_TxtIn, Box_TxtOut
Dim Shared As HFONT hFont
Dim As MSG msg
Dim Shared As String file, filter, text
Dim As Long x0, y0, x1, y1


'--------------------------------------------------------------------------------
Declare Sub FileSave()

'Filter for LoadSaveDialog:
filter = "Basic Files (*.bas)" + Chr(0) + "*.bas" + Chr(0) _
					+ "Text Files (*.txt)" + Chr(0) + "*.txt" + Chr(0)_
					+ "All Files (*.*)" + Chr(0) + "*.*" + Chr(0, 0)

Sub FileNew()
	
	If Len(EditBox_GetText(Box_TxtIn)) > 0 Then
		If MessageBox(0, "Save text?", "Save", MB_YESNO Or MB_ICONQUESTION) = IDYES Then FileSave()
		EditBox_SetText(Box_TxtIn, "")
	End If
	file = ""
	SetWindowText(Window_Main, "New File")	
	
End Sub


Sub FileOpen()
	
	Dim As String Buffer
	
	If Len(EditBox_GetText(Box_TxtIn)) > 0 Then
		If MessageBox(0, "Save text?", "Save", MB_YESNO Or MB_ICONQUESTION) = IDYES Then FileSave()
	End If
	EditBox_SetText(Box_TxtIn, "")
	file = LoadSaveDialog(, filter)
	
	If file <> "" Then
		Open file For Binary As #1
		Buffer = Space(LOF(1))
		Get #1,, Buffer
		Close #1
		EditBox_SetText(Box_TxtIn, Buffer)
		SetWindowText(Window_Main, file)
	End If
		
End Sub

Sub FileSave()
	
	file = LoadSaveDialog(1, filter) 
	If file <> "" Then
		Open file For Binary As #1
		Put #1,, EditBox_GetText(Box_TxtIn)
		Close #1
		SetWindowText(Window_Main, file)
	End If
		
End Sub

Sub FileExit()
		
	If Len(EditBox_GetText(Box_TxtIn)) > 0 Then
		If MessageBox(0, "Save text?", "Save", MB_YESNO Or MB_ICONQUESTION) = IDYES Then FileSave()
	End If
	
	End

End Sub

Sub Strip()
	
'hedit1 is the handle of the edit control.
'for putting the text :
'setwindowtext(hedit1,text)
'For getting the text :
'getwindowtext(hedit1,text,25) ''here size limit = 25
	
'	getwindowtext(hBox_TxtIn,text,25)
	
'	MessageBox(0, "Found: ", "Info", MB_OK Or MB_ICONINFORMATION)
	
	
	
End Sub

Sub Info()

	MessageBox(0, "Simple text editor, created in FreeBasic", "Info", MB_OK Or MB_ICONINFORMATION)
								
End Sub

Sub CreateWindow_Main()
	'Main Window with menu and text editor (Font: Courier New)
	
	Dim As HMENU hMenu, hFile, hEdit, hStrip, hHelp
	
	Window_Main = Window_New(100, 100, 800, 500, "Text Editor")
	
	hMenu = CreateMenu()
	hEdit = CreateMenu()
	hFile = CreateMenu()
   hStrip= CreateMenu()
	hHelp = CreateMenu()

	MenuTitle(hMenu, hFile, "File")
	MenuTitle(hMenu, hEdit, "Edit")
   MenuTitle(hMenu, hStrip,"Strip")
	MenuTitle(hMenu, hHelp, "Help")

	MenuItem(hFile, 1, "New")
	MenuItem(hFile, 2, "Open")
	MenuItem(hFile, 3, "Save")
	MenuItem(hFile, 4, "Exit")
	MenuItem(hEdit, 5, "Undo")
	MenuItem(hEdit, 6, "Cut")
	MenuItem(hEdit, 7, "Copy")
	MenuItem(hEdit, 8, "Paste")
	MenuItem(hEdit, 9, "Delete")
	MenuItem(hStrip,10,"Strip")  
	MenuItem(hHelp, 11,"Info")
	
	SetMenu(Window_Main, hMenu )
	
	Box_TxtIn = Editor_New(10, 10, 780, 480, "",, Window_Main)
	hFont = Control_Createfont("Courier New")
	Control_SetFont(Box_TxtIn, hFont)
	
End Sub


'Main:

CreateWindow_Main()

Do
	WaitEvent(Window_Main, msg)

	Select Case msg.hwnd
	
     Case Window_Main
        
      Select Case msg.message
		 Case WM_COMMAND	'Menu commands
		    Select Case msg.wParam
		    	Case 1
	                 FileNew()
		    	Case 2
	                 FileOpen()
		    	Case 3
	                 FileSave()
		    	Case 4
	                 FileExit()
		    	Case 5
	                 EditBox_Undo(Box_TxtIn)
		    	Case 6
	                 EditBox_Cut(Box_TxtIn)
		    	Case 7
	                 EditBox_Copy(Box_TxtIn)
		    	Case 8
	                 EditBox_Paste(Box_TxtIn)
		    	Case 9
						  EditBox_Clear(Box_TxtIn)
		    	Case 10
						  Strip()
		    	Case 11		 			    		
						  Info()
           End Select
           
        Case Else
					'Resize editor if window size is changed
	        Window_GetSize(Window_Main, x0, y0, x1, y1)
	        
            Control_Resize(Box_TxtIn, 20, 20, x1 - 30, y1 - 30)  
            
      End Select
      
  End Select
  
Loop Until Window_Event_Close(Window_Main, msg)

Control_Deletefont(hFont)

End


Last edited by cavelamb on Apr 14, 2021 19:11, edited 3 times in total.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI GUI library

Post by cavelamb »

Form a different example, clicking on and highlighting text in the edit box...

But how to access from code?

Code: Select all

 Case btn1
      Select Case msg.message
        ' If left mouse button was pressed in button area then
        ' check if is edit box text = "". If it is then set
        ' the edit box text to "Type text here"
        Case WM_LBUTTONDOWN
          ' When button is pressed set the text
          ' of button to "pressed"
          SetWindowText( btn1, "Clicked!" )
         
          Dim As ZString*1024 txt
         
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Simple WinAPI GUI library

Post by SARG »

hedit1 is the handle of the edit control.

for putting the text :
setwindowtext(hedit1,text)

For getting the text :
getwindowtext(hedit1,text,25) ''here size limit = 25
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI GUI library

Post by cavelamb »

Hey SARG,

Cool.
Thanks for the help.

I'm looking for end of line (CR/LF) and blank lines, so

I guess I'll have to pull text from the edit box into a string and then parse the string...
then trim the string to remove the parts used up and refill the string...
What keeps track of position in the Edit Box?
That sounds do-able, but awful messy.

OR?

Get one character at a time?
test for CR
build working string

Slow, but simpler?
Oh, yes, How do I detect end of text in the edit box?

I see
If Len(EditBox_GetText(Box_TxtIn)) > 0 Then
Which tells if the box is empty or not.

Does fetching characters from the box actually remove them from the box?
Or just move a pointer?

However....

So far, no joy using the handle...

This line keeps causing an error 42: Variable not declared, hBox_TxtIn in 'getwindowtext(hBox_TxtIn,text,25)'

getwindowtext(hBox_TxtIn,text,25)


Did I not do something right?
Obviously?
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI GUI library

Post by cavelamb »

For what its worth, this is what I'm trying to do...

Code: Select all

Sub Strip()
	
'hedit1 is the handle of the edit control.
'for putting the text :
'setwindowtext(hedit1,text)
'For getting the text :
'getwindowtext(hedit1,text,25) ''here size limit = 25

Dim As String StrWork
	
Read the imput file	
	getwindowtext(hBox_TxtIn,StrWork,25)

' filter the input file some
' save changed text to output file...	

	EditBox_SetText(Box_TxtOut, StrWork
End Sub
So I guess what I need to do is define the handle?
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Simple WinAPI GUI library

Post by SARG »

Remove the 'h' from hBox_TxtIn -> Box_TxtIn

I guess that you don't completly understand what you are doing. Go forward step by step, try small things before fighting the big boss. ;-)
Anyway ask and I'll help you.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI GUI library

Post by cavelamb »

I guess that you don't completly understand what you are doing. Go forward step by step, try small things before fighting the big boss. ;-)
Anyway ask and I'll help you.
Truer words were never spoken...

Thanks
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Simple WinAPI GUI library

Post by cavelamb »

oR?
Could it be that this quick and dirty approach to windoze
"Easy Windows API Tutorial"
doesn't properly register components??
Lothar Schirm
Posts: 437
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Simple WinAPI GUI library

Post by Lothar Schirm »

I am working on a modification of this Windows library. It is not based on the Windows system dialog box (Windows class "#32770"), it is possible to register a Windows class with predefined standard parameters, and it is necessary to write a callback function (WndProc). Code example:

Code: Select all

'===============================================================================
' WinLib_GUI.bas
' Template with Menu, Textbox, Editor, Buttons, Listbox
'===============================================================================

#Include "WinLib.bi"

Dim Shared As HMENU hMenu, hFile, hHelp
Dim Shared As HWND MainWindow, Text1, Text2, List1, Button1, Button2


Function WndProc(ByVal hWnd As HWND, ByVal message As UINT, ByVal wParam As WPARAM, _
                 ByVal lParam As LPARAM ) As LRESULT
  
  Dim As HFONT Font
  Dim As Integer i
  Dim As String text
  
	Select Case message
	
		Case WM_CREATE            
			
			'Menü:
			hMenu = CreateMenu()
			hFile = CreateMenu()
			hHelp = CreateMenu()
			MenuTitle(hMenu, hFile, "File")
			MenuTitle(hMenu, hHilfe, "Help")
			MenuItem(hFilei, 1, "New")
			MenuItem(hFile, 2, "Open")
			MenuItem(hFile, 3, "Save")
			MenuItem(hFile, 4, "Quit")
			MenuItem(hHelp, 5, "?")
			SetMenu(hwnd, hMenu)			
			
			'Controls:
  		Var Label1 = Label_New	(20, 20, 200, 20, "Enter a text here:", hWnd) 
			Text1 = TextBox_New			(20, 50, 200, 20, "Text ...",, hWnd)
			Button1 = Button_New		(60, 80, 100, 20, "Copy", hWnd)
			Text2 = TextEditor_New	(20, 120, 300, 200, "Enter text!",, hWnd)
			Button2 = Button_New		(340, 200, 100, 20, "Copy", hWnd)
			List1 = ListBox_New			(20, 350, 200, 200,, hWnd)			
		 
			'Font for the editor:
			Font = Control_Createfont("Courier New", 16, 8)
      Control_Setfont(Text2, Font)
			TextBox_SetText(Text2, "Please write a text here!") 
			
			'Fill listbox:
			For i = 0 To 20
				ListBox_AddString(List1, "Item No. " & Str(i))
			Next
		
		Case WM_COMMAND
		
			Select Case LoWord(wParam)
				'Menü:
				Case 1
					MessageBox(0, "New file ...", "File", 0)
				Case 2
					MessageBox(0, "Open ...", "File", 0)
				Case 3
					MessageBox(0, "Save ...", "File", 0)
				Case 4
					SendMessage(hWnd, WM_CLOSE, 0, 0)
				Case 5
					MessageBox(0, "I am sorry, I cannot help you!", "Help", 0)
			End Select
			
			Select Case lParam
			
				Case Button1
					'Copy text from Text1 to console:
					text = TextBox_GetText(Text1)
					Print text
					
				Case Button2
					'Copy text from Text2 to console:
					text = TextBox_GetText(Text2)
					Print text
					
				Case List1
					If HiWord(wParam) = LBN_SELCHANGE Then
						'Copy selected index and text to console:
						i = ListBox_GetCurSel(List1)
						text = ListBox_GetText(List1, i)
						Print i; Space(1); text
					End If
					 
			End Select
			
		Case WM_PAINT
		
		Case WM_SIZE            
			
		Case WM_KEYDOWN
			If(LoByte(wParam) = 27) Then PostMessage(hWnd, WM_CLOSE, 0, 0)
					
		Case WM_DESTROY
			PostQuitMessage(0)
			Exit Function
			
	End Select
	
	Return DefWindowProc(hWnd, message, wParam, lParam)    
    
End Function

RegisterWindowClass("WindowClass", @WndProc)
MainWindow = Window_New("WindowClass", 10, 10, 500, 650, "Windows GUI")
RunMessageLoop()

End
Is there any interest in this modified library? If yes, I will upload it on FreeBASIC Portal during the next week.
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Simple WinAPI GUI library

Post by RNBW »

Hi Lothar
I'd certainly be interested.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Simple WinAPI GUI library

Post by srvaldez »

yes, me too
Lothar Schirm
Posts: 437
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Simple WinAPI GUI library

Post by Lothar Schirm »

Ok, I am still preparing some code examples, I will open a new project site in this forum for that library after uploading.
Lothar Schirm
Posts: 437
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Simple WinAPI GUI library

Post by Lothar Schirm »

Modified library: see viewtopic.php?t=32421
Post Reply