|
-
Jan 30th, 2023, 10:48 AM
#1
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):

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:

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:

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:

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; Oct 13th, 2023 at 10:30 AM.
Reason: Updated version with new features
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|