Results 1 to 17 of 17

Thread: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX required)

  1. #1

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

    Thumbs up VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX required)

    This is my attempt to implement a lightweight wrapper class for the RichEdit control (riched20.dll as well as its newer version msftedit.dll) which has Unicode support, RTF capabilities (Rich Text Format) 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) as well as new ones specific to the RichEdit control:

    Properties:

    • hWnd (read-only)
    • Left
    • Top
    • Width
    • ScaleWidth (read-only)
    • Height
    • ScaleHeight (read-only)
    • AutoDetectURL (various forms of URLs are detected automatically when typed in the RichEdit textbox after this property has been enabled)
    • BackColor
    • Container
    • Enabled
    • Font
    • ForeColor
    • Locked
    • MaxLength
    • MultiLine (default is "True", needs to be set to "False" BEFORE the RichEdit control is created if you wish the RichEdit control to be in Single Line mode)
    • PasswordChar
    • PlaceholderText (MSFTEDIT only - the RichEdit control can display a grayed-out placeholder text when empty to provide cues for the user about its intended purpose)
    • PlainTextMode (default is "False" meaning the RichEdit control will be in full RTF mode)
    • SelLength
    • SelProtect (used to protect a range of text against accidental editing)
    • SelStart
    • SelText
    • SpellChecking (MSFTEDIT only - enable or disable spell checking (misspelled words will be underlined with red squiggly lines))
    • TabIndex
    • Tag
    • Text (default property for the RichEdit control)
    • TextRTF (loads or saves the RichEdit contents as a RTF file (byte array), 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)
    • GetCurrentLineInfo - retrieves useful information about the current position in the MultiLine RichEdit control (current line, curent char position, line length, is caret at beginning or end of line, paragraph alignment and indentation)
    • GetSpellingSuggestions - provides 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 RichEdit control (search forward, backward, case sensitive, whole word), the demo project also shows how to use the Find/Replace dialog to manage searches
    • InsertImage - Inserts an image at the current position (the image parameter can be a Picture object or a File name). If the image parameter is a Picture object then it can be optionally converted to another format (PNG, JPG, etc). Images loaded from files are left "as is"
    • Move - for later repositioning of the RichEdit control after it has been created on a Form
    • 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")
    • SetFocusRichEdit - Sets the focus to the RichEdit control
    • SetMargins - Sets horizontal and/or vertical margins (in Pixels) inside the RichEdit control
    • SetParagraphAttributes - Set the alignment (Left, Center, Right, Justify) and/or indentation of the current paragraph
    • SetSelectionColor - changes the background and/or text color of a single word or selected text
    • SetSelectionColorDialog - same as above, only it displays the "ShowColor" dialog for selecting the color
    • SetSelectionFontAttributes - changes the font attributes of the current selection (font name, size, bold, italic, underline, strikethrough), MSFTEDIT only -> can change underline styles and colors
    • SetSelectionFontDialog - same as above, only it displays the "ChooseFont" dialog for selecting the font attributes
    • SetSelectionHyperlink - MSFTEDIT only, turns the selection into a Hyperlink (also prompts for the destination URL)

    Events:

    • Change
    • ChangeProtectedText - occurs when an attempt is made to edit a range of protected text (can choose to allow or deny the change)
    • KeyDown
    • KeyPress
    • GotFocus
    • LostFocus
    • Click
    • LinkClick - occurs when the user clicks on a Hyperlink
    • 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: 1583
Size:  41.2 KB

    The main form loads a text file (for testing purposes and places it in a multi-line RichEdit. Selecting some text from the multi-line RichEdit places it in the single-line RichEdit.

    Code:
    Private Sub richTest1_SelectionChange(SelText As String)
        richTest2 = SelText
    End Sub
    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 paragraph indentation and alignment (Left, Center, Right and Justify) and insert an image:

    Save as RTF:

    Name:  RichEditRTF.jpg
Views: 1553
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
    Print as PDF:

    Name:  RichEditPDF.jpg
Views: 1579
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 as Font effects (bold, italic, underline, strikethrough, shadow, capital letters, small capital letters, text color, text background color), protect a range of text and insert a hyperlink with a friendly URL name. The last item in the PopupMenu is a "Spelling Suggestions" list for the currently selected word:

    Name:  RichEditSpellCheckSuggestions.jpg
Views: 1503
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)

  2. #2
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    EspaƱa
    Posts
    508

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

    good job

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

    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
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,393

    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
    323

    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
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,393

    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
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,393

    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
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,393

    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: 1387
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
    94

    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
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,393

    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
    94

    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
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,393

    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
    94

    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

  14. #14

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

    Thumbs up Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir

    The newest update includes some bug fixes (it will no longer crash when "SpellChecking" is attempted on older versions of Windows such as Win 7 and below) as well as some new features. Highlights include:

    * InsertImage - Inserts an image at the current position (the image parameter can be a Picture object or a File name). If the image parameter is a Picture object (which is inherently a bitmap) then it can be optionally converted to another format (PNG, JPG, etc) before being inserted in the RichEdit textbox. Images loaded from files are left "as is" (supports BMP, GIF, JPG, PNG and TIFF images).

    * SetParagraphAttributes - Set the alignment (Left, Center, Right, Justify) and/or indentation of the current paragraph.

    * SetSelectionHyperlink - turns the selection into a Hyperlink (also prompts for the destination URL). Hovering the mouse over the hyperlink will display a tooltip with the destination URL. You can choose what happens when the user clicks the link in the "LinkClick" event (such as opening the URL in the default browser).

    Name:  RichEditFriendlyURL.png
Views: 1065
Size:  78.4 KB

    New Font Effects (bold, italic, underline, strikethrough, shadow, capital letters, small capital letters, text color, text background color) have been added to the context menu. In this screenshot you can see the currently selected word being displayed in bold, italic, small capital letters:

    Name:  RichEditFontEffects.jpg
Views: 1068
Size:  72.4 KB

    The next screenshot shows a list of spelling suggestions as a replacement for one "intentionally" misspelled word as well as a "yellow marker" effect used as text background color:

    Name:  RichEditSpellingSuggestions.png
Views: 1067
Size:  49.9 KB

    As usual you can download this latest update from the first post above.
    Last edited by VanGoghGaming; Aug 12th, 2023 at 03:55 AM.

  15. #15
    Lively Member
    Join Date
    Oct 2014
    Posts
    93

    Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir

    Hello VanGoghGaming!

    May I ask if cRichEdit supports Simplified Chinese. I added Chinese to the txt file, but cRichEdit shows garbled code.



    RichEditTest- Add Simplified Chinese-File.zip


    Name:  cRichEditError.jpg
Views: 683
Size:  100.7 KB

    Edit the post and add a new attachment
    Last edited by smileyoufu; Sep 22nd, 2023 at 04:17 AM.

  16. #16

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

    Cool Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir

    Hey mate, you failed to produce a valid attachment, maybe you want to edit your post and fix that. Try previewing before hitting submit.

    Anyway the RichEdit doesn't care about what type of Chinese characters you put into it (simplified, complicated or otherwise). The problem here is that you added the Chinese to the text file when you should have completely replaced the contents. There is a "ReadFile" function in "Form_Load" that reads the contents of this file and at the moment the contents are all in English as you can see in the screenshots above.

    The "ReadFile" function has an optional parameter that specifies the "CodePage" for the contents it's about to read. By default it is set to "Autodetect_All" which works fine as long as all the characters are in the same language. If you are going to read a mix of characters then you need to provide the proper "CodePage" parameter in the call to "ReadFile":

    Code:
    ReadFile "RichEditTest.txt", sTest, CP_Unicode_UTF_8
    An alternative to that is to use a Text Editor that knows how to save the file in UTF-8 format (adding a BOM character at the beginning) and then "Autodetect_All" will work fine. You can also type the characters directly into the RichEdit control or copy-paste them from another document.

  17. #17
    Lively Member
    Join Date
    Oct 2014
    Posts
    93

    Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir

    Hello VanGoghGaming!

    The issue has been resolved using the code "ReadFile" RichEditTest. txt ", sTest, CP_Unicode UTF_8", and simplified Chinese can now be displayed normally. Thank you very much!

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