With common controls v6 (available since XP), we can use VB's own textbox (some limitations explained later) for a simple input-only textbox, and have it accept unicode text. Of course, you can get the unicode text also. No subclassing, no custom controls.
CAVEAT: Not perfect, but for many scenarios, this is as simple as it gets without subclassing or adding other controls to your project for the sole purpose of an input unicode-aware textbox.
Limitations.... If the textbox is only meant for input, these limitations are not that bad.
1. This 'trick' requires the textbox to be multi-line. You can kind of imitate a single-line textbox by setting the scrollbar to horizontal only, then hiding the scrollbar during form_load. This will prevent the text from wrapping when the caret goes past the visible horizontal boundary. What this doesn't do is prevent a user from getting to a new line via Enter or Ctrl+Enter, nor does it prevent the user from pasting multiple lines of text from the clipboard. With a little effort you can overcome this limitation with keyboard events and/or keyboard-related APIs; along with monitoring the textbox change event. A simple Enter key can be prevented with something like:
2. You can't really edit the text other than sending text to the clipboard in unicode format and pasting it to the textbox. This is why I suggest limiting this trick to just input-text boxes, i.e., custom inputbox, textbox on a usercontrol's property page, etc.
3. Must include a manifest with the project, textbox has got to be themed with common controls v6. An external manifest can be applied to VB6.exe to have the IDE themed, allowing you a WYSIWYG experience while in IDE.
4. Use of APIs to hide the horizontal scrollbar and get text
How to set it up.
1. Place a textbox on your form. Size it the way you need and ensure at least these properties are set:
2. If you actually want a multiline textbox, then set the scrollbars how you want & ignore this step. In form_load, hide the scrollbar
3. Retrieving the text in unicode, requires several APIs. The reason why a multiline textbox is required is that EM_GETHANDLE message is invalid for single line textboxes. And simple APIs like GetWindowTextW don't return unicode from the textbox; neither does VB's .Text property.
4. Pick a decent, scalable true-type font for your textbox.
For a bit more control without the 1st limitation mentioned above, you can always create an API unicode textbox, setting the window style as you need. Still shouldn't need to subclass or add external controls.
The best solution is a full-blown unicode textbox, but this thread is simply meant to offer a suitable substitute if the scenario fits.
Screenshot below simply shows that this will not work if the project is not themed via a manifest. When the text was retrieved, here are the hex values of each character (AscW), after pasting those Chinese characters: 5C0F 7EA2 72D0 72F8
![Name: Untitled.jpg
Views: 127
Size: 1.8 KB]()
FYI: Google Translate -- great place to get various language translations & unicode characters for copying and pasting for testing.
Edited: I hosed up the lstrlenW API, sorry about that. Always a risk of leaving in test code. Oops & fixed the sample code above.
CAVEAT: Not perfect, but for many scenarios, this is as simple as it gets without subclassing or adding other controls to your project for the sole purpose of an input unicode-aware textbox.
Limitations.... If the textbox is only meant for input, these limitations are not that bad.
1. This 'trick' requires the textbox to be multi-line. You can kind of imitate a single-line textbox by setting the scrollbar to horizontal only, then hiding the scrollbar during form_load. This will prevent the text from wrapping when the caret goes past the visible horizontal boundary. What this doesn't do is prevent a user from getting to a new line via Enter or Ctrl+Enter, nor does it prevent the user from pasting multiple lines of text from the clipboard. With a little effort you can overcome this limitation with keyboard events and/or keyboard-related APIs; along with monitoring the textbox change event. A simple Enter key can be prevented with something like:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then KeyAscii = 0
End Sub
3. Must include a manifest with the project, textbox has got to be themed with common controls v6. An external manifest can be applied to VB6.exe to have the IDE themed, allowing you a WYSIWYG experience while in IDE.
4. Use of APIs to hide the horizontal scrollbar and get text
How to set it up.
1. Place a textbox on your form. Size it the way you need and ensure at least these properties are set:
Mutliline = True
Scrollbars = Horizontal
Scrollbars = Horizontal
2. If you actually want a multiline textbox, then set the scrollbars how you want & ignore this step. In form_load, hide the scrollbar
Code:
Private Declare Function ShowScrollBar Lib "user32.dll" (ByVal hWnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
Private Const SB_HORZ As Long = 0
Example: ShowScrollBar Text1.hWnd, SB_HORZ, 0&
Code:
Private Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function lstrlenW Lib "kernel32.dll" (ByVal lpString As Long) As Long
Private Declare Function LocalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function LocalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
Private Const EM_GETHANDLE As Long = &HBD
Example:
Dim s As String
Dim lCount As Long, lHandle As Long, hGlobal As Long
lHandle = SendMessageW(Text1.hWnd, EM_GETHANDLE, 0&, ByVal 0&)
If lHandle = 0 Then Exit Sub ' fails on single-line textbox controls
hGlobal = LocalLock(lHandle)
lCount = lstrlenW(hGlobal)
s = String$(lCount, 0)
CopyMemory ByVal StrPtr(s), ByVal hGlobal, lCount * 2&
LocalUnlock hGlobal
' variable s now contains the unicode text
For a bit more control without the 1st limitation mentioned above, you can always create an API unicode textbox, setting the window style as you need. Still shouldn't need to subclass or add external controls.
The best solution is a full-blown unicode textbox, but this thread is simply meant to offer a suitable substitute if the scenario fits.
Screenshot below simply shows that this will not work if the project is not themed via a manifest. When the text was retrieved, here are the hex values of each character (AscW), after pasting those Chinese characters: 5C0F 7EA2 72D0 72F8
FYI: Google Translate -- great place to get various language translations & unicode characters for copying and pasting for testing.
Edited: I hosed up the lstrlenW API, sorry about that. Always a risk of leaving in test code. Oops & fixed the sample code above.