-
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
-
Jan 30th, 2023, 11:07 AM
#2
Fanatic Member
Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir
-
Feb 1st, 2023, 08:41 AM
#3
Hyperactive Member
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?
-
Feb 1st, 2023, 11:27 AM
#4
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.
-
Feb 3rd, 2023, 10:34 PM
#5
Hyperactive Member
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.
-
Mar 2nd, 2023, 05:27 AM
#6
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!
-
Mar 15th, 2023, 05:44 PM
#7
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.
-
Mar 21st, 2023, 11:13 PM
#8
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:
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.
-
Mar 22nd, 2023, 04:46 AM
#9
Lively Member
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
-
Mar 22nd, 2023, 08:36 AM
#10
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.
-
Mar 22nd, 2023, 09:32 AM
#11
Lively Member
Re: VB6 RichEdit (riched20.dll) lightweight Unicode Textbox (only code, no OCX requir
VB6 in French
richTest1.GetSpellingSuggestions("word") return the same Error -> 430
-
Mar 22nd, 2023, 10:28 AM
#12
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...
-
Mar 23rd, 2023, 03:13 AM
#13
Lively Member
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
-
Jul 24th, 2023, 06:58 PM
#14
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).
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:
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:
As usual you can download this latest update from the first post above.
Last edited by VanGoghGaming; Aug 12th, 2023 at 03:55 AM.
-
Sep 21st, 2023, 10:39 PM
#15
Lively Member
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
Edit the post and add a new attachment
Last edited by smileyoufu; Sep 22nd, 2023 at 04:17 AM.
-
Sep 21st, 2023, 10:55 PM
#16
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.
Last edited by VanGoghGaming; Oct 13th, 2023 at 10:31 AM.
-
Sep 22nd, 2023, 04:35 AM
#17
Lively Member
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!
-
Jul 5th, 2024, 02:24 AM
#18
Hyperactive Member
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
Hi, I wonder if we could have possibility to add row numbers (not only on paragraph) in the same way we use this functionality in the Word Office. Thank you.
"VB code is practically pseudocode" - Tanner Helland
"When you do things right, people won't be sure you've done anything at all" - Matt Groening
"If you wait until you are ready, it is almost certainly too late" - Seth Godin
"Believe nothing you hear, and only one half that you see" - Edgar Allan Poe
-
Jul 5th, 2024, 07:46 AM
#19
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
I haven't used Word or any other Office products for more than a decade but I'm pretty sure Word uses something fancier than this simple RichEdit control that is distributed for free with Windows. Anyway the RichEdit control can only add numbers and bullets to paragraphs so if you don't want to make each line a new paragraph then you need to come up with a custom solution. An idea would be to add a left margin to your RichEdit text box and then draw line numbers directly on the hDC of the RichEdit but this is easier said than done especially when you want to scroll the text box up and down...
-
Jul 6th, 2024, 02:12 PM
#20
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
This code is useful if you want to display some simple text.
If we want a richer form of content, use.Webbrowser, 10-30 MB size of Google kernel display web pages can beA lot of CEF. DLL, those may have reached 300 megabytes, 500 megabytes.
Microsoft's latest webview 2 runtime takes up 700 megabytes. I don't know why Microsoft is doing this. It's so huge. Will the installation take up three gigabytes?
-
Jul 23rd, 2024, 08:09 PM
#21
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
how to delete every txt by vb6 code?
like this:123456 vbcrlf +abcdefg
how to delete "3"
how to delete vbcrlf
how to delete "d"
How to get the color and font size of any character?
Is this the way to take the length of all the text? And then get the color of the second (or twentieth) character? Font size, is it bold?
How to delete any line of text, the nth character, insert characters and modify characters by code?
-
Jul 24th, 2024, 06:35 AM
#22
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
Select the character you want and replace the selection with a null string to delete it.
As for your second question, select a character and press Ctrl-D to bring up the font dialog and look at the code that retrieves the currently selected font attributes, size and color.
Last edited by VanGoghGaming; Jul 24th, 2024 at 06:38 AM.
-
Jul 25th, 2024, 01:18 AM
#23
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
use SELECT START,LENGTH ,CAN USE ON STANDARD textbox,how to select text by rtf format?
what's api for select text?and insert text? or get select text?
-
Jul 25th, 2024, 01:47 AM
#24
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
It is done in the same way for a Rich TextBox as for a Standard TextBox. There is no rtf syntax involved, everything can be done through Rich Edit Messages. Take a look at the source code of this project if you want to learn more.
-
Aug 10th, 2024, 10:24 PM
#25
Fanatic Member
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
Originally Posted by VanGoghGaming
It is done in the same way for a Rich TextBox as for a Standard TextBox. There is no rtf syntax involved, everything can be done through Rich Edit Messages. Take a look at the source code of this project if you want to learn more.
hDC = hDC: .hdcTarget = hDC ' Prepare the document for printing
.rcPage.Top = 0: .rcPage.Left = 0: .rcPage.Right = cxPhysWidth: .rcPage.Bottom = cxPhysHeight ' Set page rectangle to physical page size in twips.
.rc.Left = 0: .rc.Right = cxPhysWidth: .rc.Top = cyPhysOffset: .rc.Bottom = cxPhysHeight - cyPhysOffset ' Set th
Hello, I print on A5 paper, I modify the above parameters, printing always the last column of characters on the right can not be displayed. What is the reason? Can I use the code to automatically scale the print to the paper size? Thank you, experts
if i save to rtf?the used notepate.exe to print well ok? thanks ?can you fixed ?
NOW
SET .rc.Right = cxPhysWidth-4*cyPhysOffset
WORK OK
Last edited by xxdoc123; Aug 11th, 2024 at 12:36 AM.
-
Aug 13th, 2024, 02:40 AM
#26
Fanatic Member
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
fontsize=10.8
if I change one word fontsize =5 used SetSelectionFontAttributes and then I restored the string to its original size=10.8 .I found the size will bigger than 10.8
-
Aug 30th, 2024, 02:04 PM
#27
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
SetSelectionFontAttributes currently uses "Long" for the font size. Guess I didn't figure people would use fractional values for the font size. Change the type to "Currency" for an accurate conversion.
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
|