-
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.
-
Mar 7th, 2025, 11:59 AM
#28
Hyperactive Member
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
A very good work. I would have two notes though. I ran the project trying to add an image using the insert image method, but it had no effect under Windows 7 (no error, but no effect). Second, it is not very clear what the role of that PictureBox container is. Don't we have other approach? Could this project be turned into a UserControl that works with cRichEdit.cls? It is not clear to me if there are many implications for such a change. On the other hand, that PictureBox container requires a parent form, but Add("VB.PictureBox" in CreateContainer cannot be added within the UserControl_Initialize event but most likely on Show().
"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
-
Mar 7th, 2025, 01:24 PM
#29
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
I haven't looked at this project in a long time. For me it was a learning experience and I decided to share it here. At first there was no PictureBox, the RichEdit was slapped directly onto the form and it worked just fine there with one minor hiccup, namely it would completely ignore keyboard navigation from the user. For example pressing "Tab" would skip the RichEdit entirely and move the focus to the next control on the form.
The PictureBox (which acts solely as a container) addresses this issue and manages focus events so the RichEdit can react to "Tab" as well as mouse clicks in and out of it. I have later learned that without the PictureBox one would have to implement the IOleInPlaceActiveObject interface for this purpose. To be honest I'm still not clear how this clunky interface works albeit I haven't spent any time on it since the PictureBox already does all this.
Regarding image insertion, this is done using the EM_INSERTIMAGE message which requires at least Windows 8 (which nobody uses anyway so might as well say Windows 10). The same applies to the SpellChecking feature.
I don't think there would be any issues turning this into a UserControl so go nuts with it if you so desire!
-
Mar 7th, 2025, 06:13 PM
#30
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
Also if you insist on using Windows 7, you can still insert images in the RichEdit control although the process is a bit more involved than simply sending a message. Fortunately Copilot (which wasn't available at the time I developed this project) comes to the rescue. I haven't tested this but at first glance it very much looks like working code:
how would you insert images in a RichEdit control on older Windows versions that do not support EM_INSERTIMAGE?
To insert images into a RichEdit control on older Windows versions that do not support the EM_INSERTIMAGE message, you can use the OLE (Object Linking and Embedding) mechanism. This involves creating an IOleObject to represent the image and inserting it into the RichEdit control. Here's a step-by-step guide:
Initialize OLE: Ensure that OLE is initialized in your application. (this is redundant since VB6 already does this automatically)
Create an IStorage object: This will be used to store the image data.
Create an IOleObject: This represents the image.
Insert the IOleObject into the RichEdit control: Use the IRichEditOle interface to insert the object.
Here's a sample code snippet in C++:
Code:
#include <windows.h>
#include <richedit.h>
#include <richole.h>
#include <ole2.h>
#include <comdef.h>
// Function to initialize OLE
void InitializeOLE()
{
OleInitialize(NULL);
}
// Function to create an IStorage object
HRESULT CreateStorage(IStorage** ppStorage)
{
return StgCreateStorageEx(
NULL,
STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
STGFMT_STORAGE,
0,
NULL,
NULL,
IID_IStorage,
reinterpret_cast<void**>(ppStorage));
}
// Function to create an IOleObject from an image file
HRESULT CreateOleObjectFromImage(LPCWSTR imagePath, IOleObject** ppOleObject)
{
// Load the image
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, imagePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (!hBitmap)
return E_FAIL;
// Create a picture object
IPicture* pPicture = NULL;
PICTDESC pd = { sizeof(PICTDESC), PICTYPE_BITMAP, { hBitmap } };
HRESULT hr = OleCreatePictureIndirect(&pd, IID_IPicture, TRUE, (void**)&pPicture);
if (FAILED(hr))
return hr;
// Create an IStorage object
IStorage* pStorage = NULL;
hr = CreateStorage(&pStorage);
if (FAILED(hr))
{
pPicture->Release();
return hr;
}
// Create an IOleObject from the picture
hr = OleCreateStaticFromData(pPicture, IID_IOleObject, OLERENDER_DRAW, NULL, NULL, pStorage, (void**)ppOleObject);
pPicture->Release();
pStorage->Release();
return hr;
}
// Function to insert an IOleObject into a RichEdit control
HRESULT InsertOleObjectIntoRichEdit(HWND hwndRichEdit, IOleObject* pOleObject)
{
IRichEditOle* pRichEditOle = NULL;
SendMessage(hwndRichEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&pRichEditOle);
if (!pRichEditOle)
return E_FAIL;
REOBJECT reobject = { 0 };
reobject.cbStruct = sizeof(REOBJECT);
reobject.cp = REO_CP_SELECTION;
reobject.clsid = CLSID_NULL;
reobject.poleobj = pOleObject;
reobject.polesite = NULL;
reobject.pstg = NULL;
reobject.dvAspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_BELOWBASELINE;
reobject.dwUser = 0;
HRESULT hr = pRichEditOle->InsertObject(&reobject);
pRichEditOle->Release();
return hr;
}
// Example usage
int main()
{
InitializeOLE();
// Create a RichEdit control (for demonstration purposes)
HWND hwndRichEdit = CreateWindowEx(0, RICHEDIT_CLASS, L"", WS_CHILD | WS_VISIBLE | ES_MULTILINE, 0, 0, 500, 500, NULL, NULL, NULL, NULL);
// Create an IOleObject from an image file
IOleObject* pOleObject = NULL;
HRESULT hr = CreateOleObjectFromImage(L"path_to_image.bmp", &pOleObject);
if (SUCCEEDED(hr))
{
// Insert the IOleObject into the RichEdit control
InsertOleObjectIntoRichEdit(hwndRichEdit, pOleObject);
pOleObject->Release();
}
// Clean up
OleUninitialize();
return 0;
}
It should be pretty straightforward to convert this C++ code into VB6 especially since all these interfaces are readily available to use in the "oleexp" TypeLib.
-
Mar 8th, 2025, 12:49 AM
#31
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
I implemented that for my last UI Ribbon project:
Code:
Private lpObject As IOleObject
Private lpStorage As IStorage
Private lpClientSite As IOleClientSite
Private Sub RichEdInsertBmpOld(sFile As String, cx As Long, cy As Long)
Dim lpLockBytes As ILockBytes
Dim render As OLERENDER = OLERENDER_DRAW
Dim cfFormat As Integer
Dim tFmtEtc As FORMATETC
Dim sc As Long
Dim cls As UUID
sc = CreateILockBytesOnHGlobal(0, CTRUE, lpLockBytes)
If sc <> S_OK Then
Debug.Print "Error creating lockbytes"
Exit Sub
End If
sc = StgCreateDocfileOnILockBytes(lpLockBytes, STGM_SHARE_EXCLUSIVE Or STGM_CREATE Or STGM_READWRITE, 0, lpStorage)
If sc <> S_OK Then
Debug.Print "Error creating lpStorage"
Set lpLockBytes = Nothing
Exit Sub
End If
tFmtEtc.dwAspect = DVASPECT_CONTENT
tFmtEtc.lIndex = -1
tFmtEtc.tymed = TYMED_NULL
Set lpClientSite = pRichEditOle.GetClientSite()
sc = OleCreateFromFile(cls, sFile, IID_IOleObject, OLERENDER_DRAW, tFmtEtc, lpClientSite, lpStorage, lpObject)
If lpObject IsNot Nothing Then
OleSetContainedObject lpObject, CTRUE
Dim reobj As REOBJECT
reobj.cbStruct = LenB(Of REOBJECT)
Dim cid As UUID
lpObject.GetUserClassID(cid)
reobj.clsid = cid
reobj.cp = REO_CP_SELECTION
reobj.dvaspect = DVASPECT_CONTENT
reobj.dwFlags = REO_RESIZABLE Or REO_BELOWBASELINE
reobj.dwUser = 0
Set reobj.poleobj = lpObject
Set reobj.polesite = lpClientSite
Set reobj.pstg = lpStorage
pRichEditOle.InsertObject(reobj)
Set lpObject = Nothing
Set lpStorage = Nothing
Set lpClientSite = Nothing
End If
End Sub
where pRichEditOle is module level and set in the RichEdit creation routine:
Code:
Private pRichEditOle As IRichEditOle
Dim hr As Long = CLng(SendMessage(hRE, EM_GETOLEINTERFACE, 0, pRichEditOle))
Some very minor adjustments and missing Api declares needed to switch from tB but the interfaces and most apis are in oleexp.tlb.
PS- I wound up using this method for newer Windows too, replacing EM_INSERTIMAGE, because images inserted that way seem to not be detected as ole objects by IRichEditOle.GetObject, so they wouldn't work with the way I found to programmatically resize/replace selected images.
Last edited by fafalone; Mar 8th, 2025 at 01:08 AM.
-
Mar 8th, 2025, 01:05 PM
#32
-
Mar 8th, 2025, 01:14 PM
#33
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
 Originally Posted by VanGoghGaming
Yep I've seen your work faf, good stuff as always!
It's a pitty you are reluctant to invest the same amount of time in a more modern framework, like say, XAML Islands for instance! 
Aren't you still primarily working in vb6, not even provisioning for x64?
You can talk when using the more modern version of the language 99% of the time and writing x64 compatible code 
Heck I don't even see tB/x64 upgrades of your hilighted code bank submissions
-
Mar 8th, 2025, 01:34 PM
#34
-
Mar 8th, 2025, 02:55 PM
#35
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
Really reaching with the excuses there huh 
I can't see how the small performance difference could possibly save more time than all the modern features... Heck just not having to clear a message box every time you edit a line with a syntax error has saved me more time than a slightly more responsive UI. Nevermind the *massive* time savings coding with WinDevLib, almost never needing to go find some win API def to copy/paste or write yourself.. on the balance it's even saved me more time than it's cost to make it.
Collapsible regions... The advanced info popup...
Nevermind all the new language features you lose access to. Much faster to have AddressOf support class members than use the large, complicated hacks to self sub in VB6. CType(Of ) quicker than a bunch of CopyMemory calls. Inline initialization, assignment of non-variant arrays with array... Err.ReturnHResult instead of vtable swapping...
All that for a snappier UI?
The only way vb6 is still clearly superior is debugging/edit and continue. But still all the other features... Im hooked
If you don't want to use tB that's ok and there are valid reasons... But it's implausible the IDE performance difference is saving you time.
-
Mar 8th, 2025, 04:37 PM
#36
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
 Originally Posted by fafalone
Heck just not having to clear a message box every time you edit a line with a syntax error has saved me more time than a slightly more responsive UI.
You gotta clear this bad boy:
-
Mar 8th, 2025, 06:44 PM
#37
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
Ah good tip, thanks. I've seen that before but figured it would, you know, turn off syntax checking (turning the line red) instead of just turning off the message box.
Still a million other ways tB saves you time though
-
Mar 8th, 2025, 08:18 PM
#38
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
 Originally Posted by fafalone
Ah good tip, thanks. I've seen that before but figured it would, you know, turn off syntax checking (turning the line red) instead of just turning off the message box.
It's a poorly captioned option, but it makes a world of difference when disabled. It's one of my first stops whenever install VB6 on a new machine.
 Originally Posted by fafalone
Still a million other ways tB saves you time though 
Except the **big one**:
 Originally Posted by fafalone
The only way vb6 is still clearly superior is debugging/edit and continue.
It's one of the biggest reasons I'm still mostly programming in VB6 in 2025 (which is insane AFAIC).
That said, I totally get the tB excitement...Wayne is working his butt off and pulling off damn near miracles almost every day.
-
Mar 8th, 2025, 08:51 PM
#39
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
It's a pain point for sure, but again I'd be skeptical about the net balance being in vb6's favor time-wise, even if the new features didn't matter. If you weren't writing new code, just debugging an existing codebase, sure... but then some errors; e.g. compiled only with no existing logger? tB's tracing facilities would be best.
Then of course this is just about time; tB's bugs and missing features are a major but constantly shrinking source of reasons to hold off for now, if those impact your projects.
-
Mar 10th, 2025, 02:28 AM
#40
Re: VB6 - RichEdit and MsftEdit lightweight Unicode Textbox (only code, no OCX requir
 Originally Posted by fafalone
All that for a snappier UI?
Would that it were so simple! 
I am really not one for nitpicking but the editor is miles behind the VB6 one (I really don't care much for themes and pretty colors when functionality is paramount). I know you can't have a snappy UI when the editor is made in JavaScript which is an interpreted language so it will never be a speed demon but the autocomplete is a complete mess.
You should be able to complete the word by pressing punctuation (comma for example), the same as you can in VB6. Also many times, when the cursor is inside a word the autocomplete will insert the suggestion in the middle of that word instead of replacing it like VB6 does.
Also every time you type an opening bracket or quote the editor automatically inserts a closing bracket or quote which often times leads to very undesirable results causing more backtracking to manually correct it! When copy-pasting blocks of code, the indentation is often messed up shifting all pasted blocks to the right, again requiring manual corrections.
Even so, I would be more than happy to overlook all these little idiosyncrasies which are inherent to beta software if the project would run correctly as it does in VB6. Case in point, I have just posted a new thread in the tB forum showcasing such a troublesome project which runs flawlessly in VB6 but is flaky in tB. In all seriousness now I would appreciate if you would take a look at it and let me know your thoughts.
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
|