Jump to content

richedit streamtofile adds newline every time


Go to solution Solved by Nine,

Recommended Posts

  • Solution
Posted

Seems that Windows automatically add a new paragraph at the end of the document.  But it can be easily removed before writing the rich edit to the file.  You could create a new ticket in Bug Tracker and ask the devs to modify _GUICtrlRichEdit_StreamToFile in order to delete the new added paragraph at the end.

In the mean time, you will need to replace the UDF callback with your own.  Here how you could do it  :

#include <GUIConstants.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", True)

$__g_pGRC_StreamToFileCallback = DllCallbackRegister("__GCR_StreamToFileCallbackEx", "dword", "long_ptr;ptr;long;ptr")

Example()

Func Example()
  Local $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1)
  Local $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
      BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
  Local $idBtnIn = GUICtrlCreateButton("In", 270, 310, 40, 30)
  Local $idBtnOut = GUICtrlCreateButton("Out", 170, 310, 40, 30)
  GUISetState(@SW_SHOW)

  _GUICtrlRichEdit_SetText($hRichEdit, "First paragraph")
  While True
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
      Case $idBtnOut
        _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf")
      Case $idBtnIn
        _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf")
    EndSwitch
  WEnd
  _GUICtrlRichEdit_Destroy($hRichEdit)
EndFunc   ;==>Example

Func __GCR_StreamToFileCallbackEx($hFile, $pBuf, $iBuflen, $pQbytes)
  Local $tQbytes = DllStructCreate("long", $pQbytes)
  DllStructSetData($tQbytes, 1, 0)
  Local $tBuf = DllStructCreate("char[" & $iBuflen & "]", $pBuf)
  Local $s = StringReplace(DllStructGetData($tBuf, 1), "\par" & @CRLF & "}", @CRLF & "}")
  FileWrite($hFile, $s)
  DllStructSetData($tQbytes, 1, StringLen($s))
  Return 0
EndFunc   ;==>__GCR_StreamToFileCallbackEx

ps. Not much testing in there and I did not test all the options,  I'll let you do that if you will...

Posted
10 minutes ago, Nine said:

Seems that Windows automatically add a new paragraph at the end of the document.  But it can be easily removed before writing the rich edit to the file.  You could create a new ticket in Bug Tracker and ask the devs to modify _GUICtrlRichEdit_StreamToFile in order to delete the new added paragraph at the end.

In the mean time, you will need to replace the UDF callback with your own.  Here how you could do it  :

#include <GUIConstants.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", True)

$__g_pGRC_StreamToFileCallback = DllCallbackRegister("__GCR_StreamToFileCallbackEx", "dword", "long_ptr;ptr;long;ptr")

Example()

Func Example()
  Local $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1)
  Local $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
      BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
  Local $idBtnIn = GUICtrlCreateButton("In", 270, 310, 40, 30)
  Local $idBtnOut = GUICtrlCreateButton("Out", 170, 310, 40, 30)
  GUISetState(@SW_SHOW)

  _GUICtrlRichEdit_SetText($hRichEdit, "First paragraph")
  While True
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
      Case $idBtnOut
        _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf")
      Case $idBtnIn
        _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf")
    EndSwitch
  WEnd
  _GUICtrlRichEdit_Destroy($hRichEdit)
EndFunc   ;==>Example

Func __GCR_StreamToFileCallbackEx($hFile, $pBuf, $iBuflen, $pQbytes)
  Local $tQbytes = DllStructCreate("long", $pQbytes)
  DllStructSetData($tQbytes, 1, 0)
  Local $tBuf = DllStructCreate("char[" & $iBuflen & "]", $pBuf)
  Local $s = StringReplace(DllStructGetData($tBuf, 1), "\par" & @CRLF & "}", @CRLF & "}")
  FileWrite($hFile, $s)
  DllStructSetData($tQbytes, 1, StringLen($s))
  Return 0
EndFunc   ;==>__GCR_StreamToFileCallbackEx

ps. Not much testing in there and I did not test all the options,  I'll let you do that if you will...

Thanks for the help, added ticket to bugtracker :)

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...