Jump to content

Recommended Posts

Posted

Hello, i would like to know if it's possible to save a rich text from a richeditbox to some kind of file

Thanks

 

The code i use:

#NoTrayIcon

#include <Misc.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <GuiEdit.au3>
#include <String.au3>
#include <GuiRichEdit.au3>

$WIDTH = 500
$HEIGHT = 350
$EXT1 = "|Text Document (*.txt)"
$EXT2 = "|All Files(*.*)"
$EXT = $EXT1 & $EXT2
$SETTINGS = "Settings.ini"

Global $FILESAVE, $FILEOPEN = "", $SAVES, $SAVE, $FILEOPENED = 0


$MAINGUI = GUICreate("Text Editor", $WIDTH, $HEIGHT, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE)

;$EDITBOX = GUICtrlCreateEdit("", 0, 0, $WIDTH, $HEIGHT - 20, -1)
 $EDITBOX = _GUICtrlRichEdit_Create($MAINGUI, "This is a test.", 10, 10, $WIDTH - 20, $HEIGHT - 40, _
                  BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

$FILE = GUICtrlCreateMenu("File")

$NEW = GUICtrlCreateMenuItem("New", $FILE)
$OPEN = GUICtrlCreateMenuItem("Open...", $FILE)
$SAVE = GUICtrlCreateMenuItem("Save", $FILE)
$SAVEAS = GUICtrlCreateMenuItem("Save As...", $FILE)
$PRINT = GUICtrlCreateMenuItem("Print...", $FILE)

GUICtrlCreateMenuItem("", $FILE)

$EXIT = GUICtrlCreateMenuItem("Exit", $FILE)

$EDIT = GUICtrlCreateMenu("Edit")
$UNDO = GUICtrlCreateMenuItem("Undo", $EDIT)
$FONT = GUICtrlCreateMenuItem("Font test", $EDIT)





GUISetState(@SW_SHOW & $GUI_ACCEPTFILES)

While 1
    $Msg = GUIGetMsg()
    Select

        Case $Msg = $GUI_EVENT_CLOSE
            If _GUICtrlEdit_CanUndo($EDITBOX) Then
                $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
                If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
                    SaveAs()
                    _GUICtrlRichEdit_Destroy($hRichEdit)
                    Exit
                EndIf
                If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
                    FileDelete($FILEOPEN)
                    FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
                    Exit
                EndIf
                If $SAVECHANGES = 7 Then
                    ExitLoop
                    Exit
                EndIf
            EndIf
            If Not _GUICtrlEdit_CanUndo($EDITBOX) Then Exit

        Case $Msg = $OPEN
            $FILEOPEN = FileOpenDialog("Open", $FILEOPEN, $EXT)
            $TEXT = FileRead($FILEOPEN, FileGetSize($FILEOPEN))
            _GUICtrlRichEdit_ReplaceText($EDITBOX, $TEXT)
            $FILEOPENED = 1

        Case $Msg = $NEW
            If _GUICtrlEdit_CanUndo($EDITBOX) Then
                $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
                If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
                    SaveAs()
                EndIf
                If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
                    FileDelete($FILEOPEN)
                    FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
                EndIf
                If $SAVECHANGES = 7 Then GUICtrlSetData($EDITBOX, "")
                If $SAVECHANGES = 2 Then Sleep(1)
            EndIf
            If Not _GUICtrlEdit_CanUndo($EDITBOX) Then GUICtrlSetData($EDITBOX, "")
            $FILEOPENED = 0

        Case $Msg = $EXIT
            If _GUICtrlEdit_CanUndo($EDITBOX) Then
                $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
                If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
                    SaveAs()
                    _GUICtrlRichEdit_Destroy($hRichEdit)
                    Exit
                EndIf
                If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
                    FileDelete($FILEOPEN)
                    FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
                    Exit
                EndIf
                If $SAVECHANGES = 7 Then ExitLoop
                If $SAVECHANGES = 2 Then
                    Sleep(1)
                EndIf
            EndIf
            If Not _GUICtrlEdit_CanUndo($EDITBOX) Then Exit

        Case $Msg = $SAVEAS
            GUICtrlSetState($EDITBOX, $GUI_DISABLE)
            SaveAs()
            $FILEOPENED = 1
            GUICtrlSetState($EDITBOX, $GUI_ENABLE)

        Case $Msg = $SAVE And $FILEOPENED = 1
            FileDelete($FILEOPEN)
            FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
            _GUICtrlEdit_EmptyUndoBuffer($EDITBOX)

        Case $Msg = $SAVE And $FILEOPENED = 0
            GUICtrlSetState($EDITBOX, $GUI_DISABLE)
            SaveAs()
            $FILEOPENED = 1
            GUICtrlSetState($EDITBOX, $GUI_ENABLE)

        Case $Msg = $SAVE And $FILEOPENED = 0
            GUICtrlSetState($EDITBOX, $GUI_DISABLE)
            SaveAs()
            $FILEOPENED = 1
            GUICtrlSetState($EDITBOX, $GUI_ENABLE)

        Case $Msg = $PRINT
            $PRINTMSGBOX = MsgBox(35, "Text Editor", "Are you sure you want to print this page?")
            Select

                Case $PRINTMSGBOX = 6
                    If FileExists("Print.txt") Then
                        FileDelete("Print.txt")
                    EndIf
                    FileWrite("Print.txt", GUICtrlRead($EDITBOX))
                    $PRINTFILE = "Print.txt"
                    _FilePrint($PRINTFILE)
                    FileDelete($PRINTFILE)
            EndSelect

        Case $Msg = $UNDO
            _GUICtrlEdit_Undo($EDITBOX)

        Case _GUICtrlEdit_CanUndo($EDITBOX) And $SAVES = 1
            GUICtrlSetState($SAVE, $GUI_ENABLE)
            $SAVES = 0

        Case Not _GUICtrlEdit_CanUndo($EDITBOX) And $SAVES = 0
            GUICtrlSetState($SAVE, $GUI_DISABLE)
            $SAVES = 1
         Case $Msg = $FONT
            _GUICtrlRichEdit_SetFont($EDITBOX, 15, "Times New Roman")

    EndSelect
WEnd
Exit

Func SaveAs()
    $FILESAVEAS = FileSaveDialog("Save As", "", "Text Document (*.txt)|All Files (*.*)")
    If Not @error Then
        $STRING = StringSplit($FILESAVEAS, ".")
        If $STRING[0] = 1 Then
            FileDelete($FILESAVEAS)
            FileWrite($FILESAVEAS & ".txt", _GUICtrlRichEdit_GetText($EDITBOX))
        Else
            FileWrite($FILESAVEAS, _GUICtrlRichEdit_GetText($EDITBOX))
        EndIf
    EndIf
EndFunc

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...