Quantcast
Viewing all articles
Browse latest Browse all 1449

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
  • Left
  • Top
  • Width
  • ScaleWidth (read-only)
  • Height
  • ScaleHeight (read-only)
  • BackColor
  • Enabled
  • Font
  • ForeColor
  • Locked
  • MaxLength
  • PasswordChar
  • SelLength
  • SelStart
  • SelText
  • TabIndex
  • Text (default property)
  • Visible

Methods:

  • CreateRichEdit - creates and places the RichEdit control on the parent form at the specified position
  • Move - for later repositioning
  • SetSelectionColor - changes the background and/or text color of a single word or selected text
  • SetFocus

Events:

  • Change
  • 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:

Image may be NSFW.
Clik here to view.
Name:  RichEditTest.jpg
Views: 133
Size:  26.9 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 FF As Long, sTest As String, byteArr() As Byte, lDataLen As Long
    FF = FreeFile: Randomize
    Open "RichEditTest.txt" For Binary Access Read As #FF
    ReDim byteArr(0 To LOF(FF) - 1): Get #FF, , byteArr: Close #FF
    lDataLen = MultiByteToWideCharPtrs(CP_UTF8, 0, VarPtr(byteArr(0)), UBound(byteArr) + 1, StrPtr(sTest), 0)
    sTest = String$(lDataLen, vbNullChar)
    lDataLen = MultiByteToWideCharPtrs(CP_UTF8, 0, VarPtr(byteArr(0)), UBound(byteArr) + 1, StrPtr(sTest), lDataLen)
    Set richTest1 = New clsRichEdit20
    Set richTest2 = New clsRichEdit20
    richTest1.CreateRichEdit Me, lblRichEdit1.Left, txtTest.Top, txtTest.Width, txtTest.Height, True
    richTest2.CreateRichEdit Me, lblRichEdit2.Left, txtTest.Top, txtTest.Width, lblRichEdit2.Height
    txtTest = sTest
    richTest1 = sTest
End Sub

Clicking on the form randomly changes the background and text color of the multi-line RichEdit:

Code:

Private Sub Form_Click()
    richTest1.BackColor = Int(Rnd * &HFFFFFF)
    richTest1.ForeColor = Int(Rnd * &HFFFFFF)
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:

Code:

Private Sub richTest1_SelectionChange(SelText As String)
    richTest2 = SelText
End Sub

Private Sub richTest2_Click()
    richTest2.SetSelectionColor Int(Rnd * &HFFFFFF), Int(Rnd * &HFFFFFF)
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 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.

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
Attached Images
Image may be NSFW.
Clik here to view.
 
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles