Jump to content

Format for _GUICtrlRichEdit_StreamFromFile Or _GUICtrlRichEdit_SetText etc.


Recommended Posts

Hi,

if I have set the format in richedit with _GUICtrlRichEdit_SetFont this has only effect if I use _GUICtrlRichEdit_InsertText afterwards.

But it has no effect on _GUICtrlRichEdit_StreamFromFile_GUICtrlRichEdit_StreamFromVar Or _GUICtrlRichEdit_SetText etc.

What can I do to load text into richedit with the format I have set with _GUICtrlRichEdit_SetFont ?

Thanks for assistance

Cheers mike

Link to comment
Share on other sites

You could use _GUICtrlRichEdit_SetText like this:

_GUICtrlRichEdit_SetText($hRichEdit, '{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard This is some {\b bold} text.\par}This is some {\b bold} text.\par}')

For more information on RTF control words, see here.

Would the format you're aiming for always be the same?

Link to comment
Share on other sites

Just now, mike1950r said:

Hi Luke,

thanks for your answer.

Yes we use always the same format.

Cheers mike

I assume from your question the other day, you're loading the text from the Clipboard?

If it's always going to be the same format, you could do something like:

_GUICtrlRichEdit_SetText($hRichEdit, '{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard {\b ' & $sString & '}\par}{\b ' & $sString & '}\par}')

Where "$sString" is the text from the clipboard. The above would format all the text as bold.

You'd have to play around with the control words to get the format you desire.

Link to comment
Share on other sites

_GUICtrlRichEdit_StreamFromVar works:

_GUICtrlRichEdit_StreamFromVar($hRichEdit, '{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard {\b ' & $sString & '}\par}{\b ' & $sString & '}\par}')

As for _GUICtrlRichEdit_StreamFromFile you'd have to wrap the text within the file inside the code words if possible? TextFile.txt would look like this:

Quote

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard {\b This is text within a text file!}\par}{\b This is text within a text file!}\par}

 

Link to comment
Share on other sites

Hi again,

no it's not for clipboard.

it's for open file to richedit control.

the format is set at the start of richedit control and works fine when i use:

$sText = FileRead($sFilePath)
            
            If StringLen(_GUICtrlRichEdit_GetText($hGlo_WE_Control)) = 0 Then
                _GUICtrlRichEdit_InsertText($hGlo_WE_Control, $sText)
            Else
                _GUICtrlRichEdit_SetSel($hGlo_WE_Control, 0, - 1, True)
                _GUICtrlRichEdit_ReplaceText($hGlo_WE_Control, $sText)
            EndIf

that means for InsertText and ReplaceText the format is applied.

But i cannot get it working for StreamFromFile.

Cheers mike

Link to comment
Share on other sites

7 minutes ago, mike1950r said:

Hi again,

no it's not for clipboard.

it's for open file to richedit control.

the format is set at the start of richedit control and works fine when i use:

$sText = FileRead($sFilePath)
            
            If StringLen(_GUICtrlRichEdit_GetText($hGlo_WE_Control)) = 0 Then
                _GUICtrlRichEdit_InsertText($hGlo_WE_Control, $sText)
            Else
                _GUICtrlRichEdit_SetSel($hGlo_WE_Control, 0, - 1, True)
                _GUICtrlRichEdit_ReplaceText($hGlo_WE_Control, $sText)
            EndIf

that means for InsertText and ReplaceText the format is applied.

But i cannot get it working for StreamFromFile.

Cheers mike

I believe that's happening because formatting is saved to the text, not the control. When using _GUICtrlRichEdit_StreamFromFile, _GUICtrlRichEdit_StreamFromVar or _GUICtrlRichEdit_SetText you're replacing the text which removes the format.

For _GUICtrlRichEdit_StreamFromFile and _GUICtrlRichEdit_StreamFromVar:

Quote

Remarks

If text is selected, replaces selection, else replaces all text in the control.

There's probably a better solution that I don't know of, but for now:

9 minutes ago, Luke94 said:

_GUICtrlRichEdit_StreamFromVar works:

_GUICtrlRichEdit_StreamFromVar($hRichEdit, '{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard {\b ' & $sString & '}\par}{\b ' & $sString & '}\par}')

As for _GUICtrlRichEdit_StreamFromFile you'd have to wrap the text within the file inside the code words if possible? TextFile.txt would look like this:

 

Edit: Maybe setting the selection like you do before _GUICtrlRichEdit_ReplaceText and then using _GUICtrlRichEdit_StreamFromFile would keep the format?

Edited by Luke94
Link to comment
Share on other sites

6 minutes ago, Luke94 said:

I believe that's happening because formatting is saved to the text, not the control. When using _GUICtrlRichEdit_StreamFromFile, _GUICtrlRichEdit_StreamFromVar or _GUICtrlRichEdit_SetText you're replacing the text which removes the format.

I think you are right.

If text exists in richedit it's better to replace the text instead to set the text, cause it keeps the format.

But if richedit control is empty just use insert text which uses the format i have setup after creation of richedit control.
It's the same when you write into richedit control manually, then also the format is applied, which was setup at creation of richedit control.

Thanks for your help, which made me understand better how the format is applied.

cheers mike

Link to comment
Share on other sites

10 minutes ago, mike1950r said:

I think you are right.

If text exists in richedit it's better to replace the text instead to set the text, cause it keeps the format.

But if richedit control is empty just use insert text which uses the format i have setup after creation of richedit control.
It's the same when you write into richedit control manually, then also the format is applied, which was setup at creation of richedit control.

Thanks for your help, which made me understand better how the format is applied.

cheers mike

This works without using the RTF control words:

_GUICtrlRichEdit_StreamFromFile($hRichEdit, @ScriptDir & '\File.txt')
_GuiCtrlRichEdit_SetSel($hRichEdit, 0, -1)
_GuiCtrlRichEdit_SetCharColor($hRichEdit, 0xFF0000)
_GUICtrlRichEdit_ChangeFontSize($hRichEdit, +12)
_GUICtrlRichEdit_SetCharAttributes($hRichEdit, "+bo")

Edit: I haven't tested but I'm sure it'll work for all _GUICtrlRichEdit_StreamFromFile, _GUICtrlRichEdit_StreamFromVar and _GUICtrlRichEdit_SetText.

This selects all the text in the Rich Edit control before applying the formats.

Credits to @Melba23 here - his example uses AppendText but pointed me in the right direction lol.

Edited by Luke94
Link to comment
Share on other sites

Luke,

sorry if i keep on one more time.

There is just one thing, which drives me around. 😉

If i do not setup any fonttype, fontsize etc.

RichEdit Control uses a default one instead.

Where is this default font declared in the lib.

Normally this should already be in the function _GUICtrlRichEdit_Create.

Do you have any idea?

Cheers mike

Link to comment
Share on other sites

3 hours ago, mike1950r said:

Luke,

sorry if i keep on one more time.

There is just one thing, which drives me around. 😉

If i do not setup any fonttype, fontsize etc.

RichEdit Control uses a default one instead.

Where is this default font declared in the lib.

Normally this should already be in the function _GUICtrlRichEdit_Create.

Do you have any idea?

Cheers mike

Hi Mike,

I would assume it would use the default font of the actual form?

You can change the form font with GUISetFont.

I think by default it's MS Sans Serif, size 8.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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