Winfbe, RichEdit text style

Windows specific questions.
Post Reply
AngFerreiras
Posts: 6
Joined: Dec 10, 2022 5:10

Winfbe, RichEdit text style

Post by AngFerreiras »

Hi,im practicing with winfbe, Im trying to implement functionality to a RichEdit control using buttons and hotkeys, basically i want to be able to select text and change the characteristics of the selected text, like: size, bold/italic, font type.

By now I can only do it in a general way using some code like:
editor.richedit1.Font = New wfxFont("Segoe UI", 9, FontStyles.Bold, FontCharset.Ansi)

But i had been unable to find in the documentation or examples to code what i want.

Is someone here that knows how to do this or know where can i find examples or tutorials that could help me?
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: Winfbe, RichEdit text style

Post by PaulSquires »

As an example, you can do something like the following to change the selected text to red, bold, and 14pt font size.

Code: Select all

   '// Set up the CHARFORMAT structure
   dim as CHARFORMAT cfm
   cfm.cbSize = sizeof(cfm)

   '// Set the new effects
   cfm.dwEffects   = CFE_BOLD
   cfm.crTextColor = BGR(255,0,0)   ' red
   cfm.dwMask      = CFM_BOLD or CFM_COLOR

   '// Set the new format
   RichEdit_SetCharFormat( frmMain.RichEdit1.hWindow, SCF_SELECTION, cast(LPARAM, @cfm) )

   '// Let's also increase the font size of the selected text
   RichEdit_SetFontSize( frmMain.RichEdit1.hWindow, 14 )
AngFerreiras
Posts: 6
Joined: Dec 10, 2022 5:10

Re: Winfbe, RichEdit text style

Post by AngFerreiras »

Thanks, I used your example to search in the winfbx documentation and found the other options related to the CHARFORMATW2 structure, im going to study the content and se how i implement it into my code.
custard375
Posts: 2
Joined: Mar 30, 2023 12:08

Re: Winfbe, RichEdit text style

Post by custard375 »

Hello,
Yes, I can assist you with implementing the functionality you desire for your RichEdit control using buttons and hotkeys.

To change the characteristics of the selected text, you can use the RichTextBox.SelectionFont property to retrieve the current font of the selected text, and then modify its properties as needed.

Here's an example of how to set the font size of the selected text when a button is clicked:

vbnet
Copy code

Code: Select all

Private Sub btnFontSize_Click(sender As Object, e As EventArgs) Handles btnFontSize.Click
    Dim size As Integer = 12 ' set the desired font size here
    If richedit1.SelectionLength > 0 Then ' check if any text is selected
        Dim currentFont As Font = richedit1.SelectionFont
        If currentFont IsNot Nothing Then ' check if selected text has a font assigned
            richedit1.SelectionFont = New Font(currentFont.FontFamily, size, currentFont.Style)
        Else ' if no font is assigned, use the default font of the control
            richedit1.SelectionFont = New Font(richedit1.Font.FontFamily, size)
        End If
    End If
End Sub
Similarly, you can modify the bold and italic properties of the selected text by setting the RichTextBox.SelectionFont.Style property accordingly.

vbnet
Copy code

Code: Select all

Private Sub btnBold_Click(sender As Object, e As EventArgs) Handles btnBold.Click
    If richedit1.SelectionLength > 0 Then ' check if any text is selected
        Dim currentFont As Font = richedit1.SelectionFont
        If currentFont IsNot Nothing Then ' check if selected text has a font assigned
            Dim newStyle As FontStyle = currentFont.Style Xor FontStyle.Bold ' toggle the bold style
            richedit1.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, newStyle)
        Else ' if no font is assigned, use the default font of the control
            Dim newStyle As FontStyle = richedit1.Font.Style Xor FontStyle.Bold
            richedit1.SelectionFont = New Font(richedit1.Font.FontFamily, richedit1.Font.Size, newStyle)
        End If
    End If
End Sub

Private Sub btnItalic_Click(sender As Object, e As EventArgs) Handles btnItalic.Click
    If richedit1.SelectionLength > 0 Then ' check if any text is selected
        Dim currentFont As Font = richedit1.SelectionFont
        If currentFont IsNot Nothing Then ' check if selected text has a font assigned
            Dim newStyle As FontStyle = currentFont.Style Xor FontStyle.Italic ' toggle the italic style
            richedit1.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, newStyle)
        Else ' if no font is assigned, use the default font of the control
            Dim newStyle As FontStyle = richedit1.Font.Style Xor FontStyle.Italic
            richedit1.SelectionFont = New Font(richedit1.Font.FontFamily, richedit1.Font.Size, newStyle)
        End If
    End If
End Sub
You can modify the above code to set the font type by replacing the currentFont.FontFamily property with the desired font family name.

Regarding hotkeys, you can handle the KeyDown or KeyUp events of the RichEdit control and check if the Ctrl key is pressed along with the desired key. For example, to toggle bold when the user presses Ctrl+B, you can add the following code to the KeyDown event handler:

vbnet
Copy code

Code: Select all

Private Sub richedit1_KeyDown(sender As Object, e As KeyEventArgs) Handles richedit1.KeyDown
    If e.Control AndAlso e.KeyCode = Keys.B Then ' check if Ctrl+B is pressed
        e.Handled = True ' prevent the default behavior of the control
        btnBold
Hope so it will help you all the best.
Last edited by fxm on Apr 06, 2023 8:20, edited 1 time in total.
Reason: Added code tags.
Post Reply