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