Using Rich Edit 2 DLL with Unicode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tristanlbailey
    New Member
    • Apr 2007
    • 30

    Using Rich Edit 2 DLL with Unicode

    I been scouring the Internet for an answer to my problem, and a couple of times thought I had almost found the answer, but still to no avail.

    I'm tying to use the Rich Edit class (riched20.dll), to display unicode text. The riched20.dll file is loaded by using the LoadLibrary function, and a Rich Edit control created with the CreateWindowEx function. The text is input into a string variable from a unicode text file. The text is then inserted into the control, by using the SendMessage function and the WM_SETTEXT message. The font is set after the text has been inserted by using the WM_SETFONT message, which uses the form's font.

    [CODE=vb]
    Private Declare Function CreateWindowEx Lib "user32.dll " Alias "CreateWindowEx A" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByRef lpParam As Any) As Long
    Private Declare Function GetCurrentObjec t Lib "gdi32.dll" (ByVal hDC As Long, ByVal uObjectType As Long) As Long
    Private Declare Function LoadLibrary Lib "kernel32.d ll" Alias "LoadLibrar yA" (ByVal lpLibFileName As String) As Long
    Private Declare Function SendMessage Lib "user32.dll " Alias "SendMessag eA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    Private Declare Function SendMessageStri ng Lib "user32.dll " Alias "SendMessag eA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

    Private Const ES_MULTILINE As Long = &H4
    'Private Const ES_READONLY As Long = &H800

    Private Const OBJ_FONT As Long = 6

    Private Const ST_DEFAULT As Long = 0

    Private Const WM_SETFONT As Long = &H30
    Private Const WM_SETTEXT As Long = &HC

    Private Const WM_USER As Long = &H400
    Private Const EM_SETTEXTMODE As Long = (WM_USER + 89)
    Private Const EM_SETTEXTEX As Long = (WM_USER + 97)

    Private Const WS_CHILD As Long = &H40000000
    Private Const WS_VISIBLE As Long = &H10000000
    Private Const WS_VSCROLL As Long = &H200000

    Private Const WS_EX_CLIENTEDG E As Long = &H200&

    Private hFont As Long
    Private hRich As Long
    Private hWndRich As Long

    Private Sub Form_Load()

    hRich = LoadLibrary("ri ched20.dll")

    Line Input #1, TempStr
    For I = 1 To 6
    Line Input #1, TempStr
    lblDictAboutInf o(I) = TempStr
    Next I
    I = 0

    Do Until TempStr = "[Notes]"
    Line Input #1, TempStr
    Loop

    TempStr = vbNullString
    TempStr2 = vbNullString

    Line Input #1, TempStr

    Do
    TempStr2 = TempStr2 & (TempStr & vbCrLf)
    Line Input #1, TempStr
    Loop Until TempStr = "[Abbreviations]"

    hWndRich = CreateWindowEx( WS_EX_CLIENTEDG E, "RichEdit20 W", vbNullString, ES_MULTILINE Or ES_READONLY Or WS_CHILD Or WS_VISIBLE Or WS_VSCROLL, 9, 152, 476, 214, hWnd, 0, App.hInstance, 0)

    SendMessageStri ng hWndRich, WM_SETTEXT, 0, TempStr2
    hFont = GetCurrentObjec t(hDC, OBJ_FONT)
    SendMessage hWndRich, WM_SETFONT, hFont, 1

    TempStr = vbNullString
    TempStr2 = vbNullString

    Seek #1, 1

    End Sub
    [/CODE]
    The majority of the text works fine, but there are special unicode characters that aren't displayed properly within the text. The font is set to Verdana, which should work with Unicode characters. I understand that version 1 of the Rich Edit class doesn't support Unicode, but version 2 or higher should.

    I'm using Visual Basic 6, and Windows XP.
    Last edited by debasisdas; Mar 28 '08, 09:45 AM. Reason: added code=vb tags
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Just an FYI - The whole Unicode thing is why I switched to VB.NET. While I could get VB6 to display some Unicode I was never able to propagate it properly throughout my application. Reading Unicode from the controls proved most annoying.

    Comment

    • tristanlbailey
      New Member
      • Apr 2007
      • 30

      #3
      I think I've worked it out...

      For those that are interested in using unicode in a VB6 app, here's some code for you to try out:

      [CODE=vb]
      Option Explicit

      'General Function Declarations
      Private Declare Function CreateWindowEx Lib "user32.dll " Alias "CreateWindowEx A" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByRef lpParam As Any) As Long
      Private Declare Function GetCurrentObjec t Lib "gdi32.dll" (ByVal hDC As Long, ByVal uObjectType As Long) As Long
      Private Declare Function GetVersionEx Lib "kernel32.d ll" Alias "GetVersion ExA" (lpVersionInfo As OSVERSIONINFO) As Long
      Private Declare Function LoadLibrary Lib "kernel32.d ll" Alias "LoadLibrar yA" (ByVal lpLibFileName As String) As Long
      Private Declare Function SendMessage Lib "user32.dll " Alias "SendMessag eA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
      Private Declare Function SendMessageStri ngA Lib "user32.dll " Alias "SendMessag eA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
      Private Declare Function SendMessageStri ngW Lib "user32.dll " Alias "SendMessag eW" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

      'General Type Declarations
      Private Type OSVERSIONINFO
      dwOSVersionInfo Size As Long
      dwMajorVersion As Long
      dwMinorVersion As Long
      dwBuildNumber As Long
      dwPlatformId As Long
      szCSDVersion As String * 128
      End Type

      'General Variable Declarations
      Private Const OBJ_FONT As Long = 6

      Private Const TM_PLAINTEXT As Long = 1

      Private Const WM_USER As Long = &H400
      Private Const EM_SETTEXTMODE As Long = (WM_USER + 89)

      Private Const ES_MULTILINE As Long = &H4

      Private Const WM_SETFONT As Long = &H30
      Private Const WM_SETTEXT As Long = &HC

      Private Const WS_CHILD As Long = &H40000000
      Private Const WS_EX_CLIENTEDG E As Long = &H200&
      Private Const WS_VISIBLE As Long = &H10000000
      Private Const WS_VSCROLL As Long = &H200000

      Private hFont As Long
      Private hRich As Long
      Private hWndRich As Long

      Private TempStr As String

      Private WinVer As OSVERSIONINFO

      Private Sub Form_Load()

      'Store operating system version information
      WinVer.dwOSVers ionInfoSize = Len(WinVer)
      GetVersionEx WinVer

      'Load the Rich Edit 2.0/3.0 library/class
      hRich = LoadLibrary("ri ched20.dll")

      'Create the control to be used with unicode
      hWndRich = CreateWindowEx( WS_EX_CLIENTEDG E, "RichEdit20 W", vbNullString, ES_MULTILINE Or WS_CHILD Or WS_VISIBLE Or WS_VSCROLL, 9, 10, 290, 180, hWnd, 0, App.hInstance, 0)

      'Open the unicode file in binary mode
      Open App.Path & "\unicode.t xt" For Binary As #1

      'Strips unicode file header
      Seek 1, 3

      'Store the unicode file contents to a string variable
      Do
      TempStr = TempStr & Input$(1, 1)
      Loop Until EOF(1)

      'Set the control to use "plain text" mode
      SendMessage hWndRich, EM_SETTEXTMODE, TM_PLAINTEXT, 0
      'Set the control to use the form's font
      hFont = GetCurrentObjec t(hDC, OBJ_FONT)
      SendMessage hWndRich, WM_SETFONT, hFont, 1

      'Send the stored text to the control
      If WinVer.dwPlatfo rmId = 2 Then
      SendMessageStri ngW hWndRich, WM_SETTEXT, 0, TempStr
      Else
      SendMessageStri ngA hWndRich, WM_SETTEXT, 0, TempStr
      End If

      'Close the file
      Close #1

      End Sub
      [/CODE]
      The code should work on Windows 95 through to Windows Vista.

      If you want to create a single line textbox instead, the multiline constant can be left out.

      The control needs to be set to "plain text" mode, so that unicode characters are displayed properly (or are at least visible). This prevents the use of RTF formatting, and object embedding, plus the control is limited to using a single font.

      If anyone knows of how this code could be improved, please post here. I will post any more decent information that I find out about unicode formatting, in the near future.

      Comment

      Working...