Results 1 to 13 of 13

Thread: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX required)

  1. #1

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Thumbs up VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX required)

    This is my attempt to implement a lightweight wrapper class for the RichEdit control (riched20.dll) which has Unicode support and comes by default with any version of Windows (only tested in Win10 though). I have tried to include most of the usual properties, methods and events of a regular VB6 textbox (some are left out, there's certainly room for improvement here):

    Properties:

    • hWnd (read-only)
    • Left
    • Top
    • Width
    • ScaleWidth (read-only)
    • Height
    • ScaleHeight (read-only)
    • BackColor
    • Enabled
    • Font
    • ForeColor
    • Locked
    • MaxLength
    • PasswordChar
    • PlaceholderText (MSFTEDIT only - the Rich Edit control can display a grayed-out placeholder text when empty to provide cues for the user about its intended purpose)
    • SelLength
    • SelStart
    • SelText
    • SpellChecking (MSFTEDIT only - enable or disable spell checking (misspelled words will be underlined with red squiggly lines))
    • TabIndex
    • Tag
    • Text (default property)
    • TextRTF (loads or saves the Rich Edit contents as a RTF file, optionally includes the whole contents or only the current selection)
    • Visible

    Methods:

    • CreateRichEdit - creates and places the RichEdit control on the parent form at the specified position (optional MultiLine/SingleLine and PlainText/RTFText)
    • GetCaretInfo - retrieves useful information about the current position in the MultiLine Rich Edit (current line, curent char position, line length, is caret at beginning or end of line)
    • GetSpellingSuggestions (provide a list of spelling suggestions for the selected word and optionally populate the context menu with them in order to replace the current selection)
    • GetTextFromRange - retrieves a chunk of text of a specified length from the specified position
    • Find - takes advantage of the Find capabilities of the Rich Edit control (search forward, backward, case sensitive, whole word), the demo project also shows how to use the Find/Replace dialog to manage searches
    • Move - for later repositioning
    • PrintRichEdit - prints the contents of the RichEdit control to the default printer (optionally include a FileName if the default printer is "Microsoft Save to PDF")
    • SetSelectionAlignment - aligns the current selection or paragraph (Left, Center, Right or Justify (default))
    • SetSelectionColor - changes the background and/or text color of a single word or selected text
    • SetSelectionFontAttributes - changes the font attributes of the current selection (font name, size, bold, italic, underline, strikethrough), MSFTEDIT only -> can change underline styles and colors.
    • SetFocus

    Events:

    • Change
    • KeyDown
    • KeyPress
    • GotFocus
    • LostFocus
    • Click
    • SelectionChange - occurs when the user selects some text

    The attached sample project contains a regular textbox (for comparison), a multi-line RichEdit and a single-line RichEdit (the Chinese text in the screenshot below is only for testing purposes to showcase the Unicode capabilities of the Rich Edit control):

    Name:  RichEditTest.jpg
Views: 285
Size:  41.2 KB

    The main form loads a text file which contains some Google-translated Chinese and Russian text (for testing purposes). This Unicode text is placed in the regular textbox as well as the multi-line RichEdit:

    Code:
    Private Sub Form_Load()
    Dim sTest As String, lLineFirstCharIndex As Long, lLineLength As Long, ctlControl As Control, lLabelIndex As Long
        InitPublicVariables False: Randomize: Me.Tag = Me.Caption
        cmbChangeSelectionAlignment.AddItem "Align Selection or Paragraph - Left": cmbChangeSelectionAlignment.AddItem "Align Selection or Paragraph - Right"
        cmbChangeSelectionAlignment.AddItem "Align Selection or Paragraph - Center": cmbChangeSelectionAlignment.AddItem "Align Selection or Paragraph - Justify"
        ReadFile "RichEditTest.txt", sTest
        Set richTest1 = New clsRichEdit20
        Set richTest2 = New clsRichEdit20
        richTest1.CreateRichEdit Me, lblRichEdit1.Left, txtTest.Top, txtTest.Width, txtTest.Height, True, , MSFTEDIT_MODERN, mnuContextMenu, mnuContextOptions, mnuSpellingSuggestions
        richTest2.CreateRichEdit Me, lblRichEdit2.Left, txtTest.Top, txtTest.Width, lblRichEdit2.Height, , , MSFTEDIT_MODERN
        richTest1.TabIndex = 4: richTest2.TabIndex = 5
        txtTest = sTest
        richTest1 = sTest: richTest1.SelStart = 120
        richTest1.GetCaretInfo , , lLineFirstCharIndex, , lLineLength
        richTest1.SpellChecking = True
        richTest2.PlaceholderText = "Type something here...": richTest2.SpellChecking = True
        Show
        cmbChangeSelectionAlignment.ListIndex = 3
    End Sub
    Clicking on the "Set Selection Font Attributes" button opens a "Choose Font" dialog where you can select a custom font for the current selection in the multi-line RichEdit (also showcases the new underline styles and underline colors):

    Code:
    Private Sub cmdFontChange_Click()
    Dim cfChooseFont As TCHOOSEFONT, lfLogFont As LOGFONT, fdFontDesc As FONTDESC, hFont As Long, iid As UUID, objFont As IFont
        With cfChooseFont
            .lStructSize = LenB(cfChooseFont)
            .hWndOwner = Me.hWnd
            .lpLogFont = VarPtr(lfLogFont)
            .flags = CF_SCREENFONTS Or CF_EFFECTS
        End With
        If ChooseFont(cfChooseFont) Then
            hFont = CreateFontIndirect(lfLogFont)
            hFont = DeleteObject(hFont)
            IIDFromString IID_IFont, iid
            With fdFontDesc
                .cbSizeofstruct = LenB(fdFontDesc)
                .sWeight = lfLogFont.lfWeight
                .fItalic = lfLogFont.lfItalic
                .fUnderline = lfLogFont.lfUnderline
                .fStrikethrough = lfLogFont.lfStrikeOut
                .sCharset = lfLogFont.lfCharSet
                .lpstrName = VarPtr(lfLogFont.lfFaceName(0))
                .cySize = -lfLogFont.lfHeight * 72 / GetDeviceCaps(Me.hDC, LOGPIXELSY)
            End With
            If OleCreateFontIndirect(fdFontDesc, iid, objFont) = 0 Then
                richTest1.SetSelectionFontAttributes objFont, , , , cfChooseFont.rgbColors, CFU_UNDERLINEDOUBLE, CFU_BLUE
                objFont.ReleaseHfont objFont.hFont
                richTest1.SetFocus
            End If
        End If
    End Sub
    Selecting some text from the multi-line RichEdit places it in the single-line RichEdit. Selecting some text from the single-line RichEdit randomly changes the selection background and text color and additionally uses the clipboard for sending Unicode text to the regular TextBox control:

    Code:
    Private Sub richTest1_SelectionChange(SelText As String)
        richTest2 = SelText
    End Sub
    
    Private Sub richTest2_Click()
    Dim sClipboard As String
        richTest2.SetSelectionColor Int(Rnd * &HFFFFFF), Int(Rnd * &HFFFFFF)
        sClipboard = ClipboardW
        ClipboardW = richTest2.SelText
        SendMessageVal txtTest.hWnd, WM_PASTE, 0&, 0&
        ClipboardW = sClipboard
    End Sub
    Also an interesting tidbit is that if you paste some Unicode text in the regular textbox it will be displayed correctly (this was rather unexpected). Any other Unicode operations don't work with the regular textbox (like loading text from a file or typing it directly). Naturally all these work just fine for the RichEdit controls.

    The demo project also shows how to print the contents of the MultiLine Rich Edit (works with the "Microsoft Save to PDF" printer as well), save the contents as RTF to be used in other word processors (such as WordPad), set selection alignment (Left, Center, Right and Justify) and also set Unicode captions to many VB6 intrinsic controls (Form, CommandButton, CheckBox, OptionButton, Frame and Label):

    Save as RTF:

    Name:  RichEditRTF.jpg
Views: 279
Size:  31.2 KB

    Code:
    Private Sub cmdPrint_Click()
        richTest1.PrintRichEdit
    End Sub
    
    Private Sub cmdSaveRTF_Click()
    Dim sFileName As String
        sFileName = AppPathW(, False) & "RichEdit.rtf"
        WriteFile sFileName, richTest1.TextRTF
        MsgBoxW "Rich Edit has been saved as RTF to: " & sFileName, vbOKOnly Or vbInformation
    End Sub
    
    Private Sub cmdTest_Click()
        cCapW(cmdTest) = richTest2
        cCapW(lblRichEdit2) = richTest2
        MsgBoxW cCapW(lblRichEdit2)
    End Sub
    Print as PDF:

    Name:  RichEditPDF.jpg
Views: 290
Size:  29.3 KB

    The main form also contains a context menu (PopupMenu) which responds independently to whichever RichEdit control currently has the focus and provides standard functionality such as Undo, Redo, Cut, Copy, Paste, Delete and Select All. Although the PopupMenu needs to be created on the main form, its functionality is contained inside the RichEdit class so that it can work independently with multiple RichEdit controls. In the latest update I have added "Find and Replace" functionality to the PopupMenu as well. The last item in the PopupMenu is a "Spelling Suggestions" list for the currently selected word:

    Name:  RichEditSpellCheckSuggestions.jpg
Views: 222
Size:  36.2 KB

    There are many API functions, types and constants used in this project. In order to avoid explicitly declaring all of them, I have used Bruce McKinney's Windows API type library (included in the sample project, downloaded from https://classicvb.net/hardweb/mckinney2a.htm). This is entirely optional but it certainly saves a lot of time copy-pasting declarations.

    Sample RichEdit Project: RichEditTest.zip (Updated)
    Last edited by VanGoghGaming; Mar 21st, 2023 at 10:53 PM.

  2. #2
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    Espaņa
    Posts
    397

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    good job

  3. #3
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    300

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    goot job
    will u use RichEdit v4.1 (RICHEDIT50W in msftedit.dll) next update?

  4. #4

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Red face Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    Hey mate, glad you like it. You can use the newer "msftedit.dll" yourself if you want although I doubt it would make any difference for the limited functionality provided by this sample project. Just change this line of code:

    Code:
    Private Const RICHEDIT_DLL As String = "riched20.dll", RICHEDIT_CLASS As String = "RichEdit20W"
    to:

    Code:
    Private Const RICHEDIT_DLL As String = "msftedit.dll", RICHEDIT_CLASS As String = "RichEdit50W"
    and you're good to go, the rest of the code seems to work exactly the same. The only difference I've seen is that the Chinese text seems to be a little more "spaced out" vertically in the sample project. That's about it.

  5. #5
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    300

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    https://learn.microsoft.com/en-us/wi...-edit-controls
    it seems rich edit v4.1 has some more good features than v2.0, such as simple tables, more text styles etc.

  6. #6

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Cool Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    Yeah well, as the title says, this is a "lightweight" attempt at a Unicode Textbox not a replacement for M$ Word!
    In fact, the difficulty grows exponentially the more bells and whistles are added... Just finished updating the current version and added a few new features:

    - TextRTF property returns/sets the RTF contents of the RichEdit control (can be saved as a RTF file which can be loaded into Wordpad or Word). It works with the whole contents of the RichEdit (default) or only the current selection.

    - PrintRichEdit method prints the contents of the RichEdit control to the default printer (can specify an optional FileName if the default printer is "Microsoft Save to PDF").

    - SetSelectionAlignment method aligns the paragraph containing the current selection (can be one of PFA_LEFT, PFA_CENTER, PFA_RIGHT or PFA_JUSTIFY (default)).

    - SetSelectionFont method changes the font attributes of the current selection (font name, size, bold, italic, underline, strikethrough)

    Updated the test project to showcase these new additions. It can be downloaded from the first post above!

  7. #7

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Cool Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    Just added another update to the "Rich Edit Demo Project" showcasing the various features added along the way. Fixed some bugs with the font selection and also implemented the "Choose Font" dialog to easily change font attributes of the current selection in the MultiLine Rich Edit. The demo also shows how to change the text alignment (Left, Center, Right, Justify), print the Rich Edit contents as PDF and save its contents as a RTF file.

    This update also takes advantage of the "Find" capabilities of the Rich Edit control (search forward, backward, case sensitive, whole word) and showcases the "Find/Replace" common dialog for managing text searches. It can be downloaded as usual from the first post above.

  8. #8

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Lightbulb Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    The newest update includes "Spell Checking" capabilities for the Rich Edit control along with replacement suggestions for the misspelled words built into the PopupMenu. Misspelled words are underlined with red squiggly lines. The spelling suggestions are offered in the currently selected language. If you have multiple languages installed (usually can switch between them with the "Alt-Shift" hotkey) then the spelling suggestions will change automatically to the new language. This can be overridden in the "SpellChecking" property if only one language is preferred for the suggestions:

    Code:
    richTest1.SpellChecking("en-US") = True
    The PopupMenu also supports Unicode captions in order to accommodate all languages. Just select a word and right-click in order to test this new feature:

    Name:  RichEditSpellCheckSuggestions.jpg
Views: 176
Size:  36.2 KB

    The SingleLine Rich Edit control in the above screenshot also features a "Placeholder" text which disappears once you start typing in it. This can offer helpful cues to the user as to what he or she is supposed to type in there.

    Last but not least, this update features new underline styles and colors. In the above screenshot the words "Footfalls" and "memory" are using a double-underline in blue color for example. There are many more underline styles to choose from including, dashes, dots, wave lines, etc and all can be colored differently.

  9. #9
    Lively Member
    Join Date
    Sep 2016
    Posts
    81

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    Hi,
    There is a problem with Subclass.
    Crash on rightclick with selected text => Err 430 WndProc

    Regards

  10. #10

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Question Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    Ugh, that's a very generic error that doesn't help much with debugging, (Class doesn't support Automation (Error 430)). The program works absolutely fine over here, both in IDE and as a compiled EXE.

    There may be a problem with the "ISpellChecker" interface exposed by the "OLEEXP" type library. What languages do you have installed on your computer? Try this piece of code separately (in "Form_Click" for example) and see if it returns a list of suggestions for you:

    Code:
    Dim sSuggestions() As String, i As Long
        sSuggestions = richTest1.GetSpellingSuggestions("word")
        For i = LBound(sSuggestions) To UBound(sSuggestions)
            Debug.Print sSuggestions(i)
        Next i
    You may try different strings for "word", depending on what languages you have installed.

  11. #11
    Lively Member
    Join Date
    Sep 2016
    Posts
    81

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    VB6 in French
    richTest1.GetSpellingSuggestions("word") return the same Error -> 430

  12. #12

    Thread Starter
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    538

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    "ISpellChecker" requires minimum Windows 8, otherwise I don't know why you get that automation error...

  13. #13
    Lively Member
    Join Date
    Sep 2016
    Posts
    81

    Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir

    Hi VanGoghGaming,
    OK i am on Win7.
    I'll try on Win10.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width