How to display Unicode characters in VB6?

CJ7 picture CJ7 · Dec 29, 2012 · Viewed 40.9k times · Source

Possible Duplicate:
What’s the best option to display Unicode text (hebrew, etc.) in VB6

What is the correct way to display the unicode character 9646 (BLACK VERTICAL RECTANGLE) in VB6?

When I try ChrW(9646) it displays ?.

Answer

bonCodigo picture bonCodigo · Dec 29, 2012

Here is a tutorial to explore. Take a look at this article for the black vertical rectangle.

Assuming Unicode is turned on, send the following string to a window to display:

Wchar_t mStr[] = {9646,0,0};

Reference. This code snippet and reference is more inlined with C++. Where you would turn off/on UNICODE in Visual C++ using following steps:

  1. Open your project in VS2008/2010;

  2. Right click the project in Solution Explorer and click Properties;

  3. Select Configuration Properties-> General, select Character Set and change the current value to Use Multi-Byte Character Set. (turning off)


Good article aboubt displaying UNICODE in VB.

When you are working with a textbox control in a Form, add the Microsoft Forms 2.0 Object Library as a reference library. This component provides Unicode supportted controls, such as: textbox, label, command button,list box, combo box, checkbox, radio button, toggle button, image, tabstrip, and multiple page control.

Working with VB6 and displaying non-us-ANSI characters you need 3 main things to understand:

  • Internally, VB6 stores strings as Unicode.
  • When displaying a string, the standard VB6 textbox and label controls do an implicit (and internal) conversion from Unicode to ANSI.
  • The standard VB6 textbox and label controls display the ANSI bytes according to a character encoding that you can specify.

After the Unicode-to-ANSI conversion, VB6 then attempts to display the character data according to the control's Font.Charset property, which if left unchanged is equal to the ANSI charset. Changing the control's Font.Charset changes the way VB6 interprets the "ANSI" bytes. In other words, you're telling VB6 to treat the bytes as some other character encoding instead of "ANSI".

For e.g. consider trying to display a Unicode Japanese string on an English computer: You set the Font.Charset = 128 (for Japanese), but your Unicode string displays as all question mark characters. It's because VB6 is first trying to convert your Japanese Unicode string to ANSI, which is Windows-1252 for English computers. Japanese characters are not representable in Windows-1252. Each character fails to convert and is replaced with a question mark. for e.g. Selecting the Japanese script in the TextBox control's property settings is the same as setting the Font.Charset at runtime.

As Jukka said Font plays a vital role showing UNICODEs given the availability of the characters within a Font. As Hans said, glyph unsupported Font produces a rectangle. So you need to make sure the Font you select is capable of rendering glyphs. For e.g. MS Sans Serif font does not render ƒ (LATIN SMALL LETTER F WITH HOOK, 2-byte Unicode value is 0x0192), so you'll see a thin solid rectangular box in its place. However on Windows, there are very few fonts with a wide enough character repertoire to represent Chinese..

In the following code the Font Name () is set during Run Time along the Font CharSet

Charset properties:

134     Simplified Chinese, gb2312 - Mainland China(PRC) and Singapore
136     Traditional Chinese, big5 - Taiwan and Hong Kong

Code:

Sub changeToUniCodes()
Dim strTxt2 As String    

UserForm1.TextBox2.Font.Charset = 134    '--CHINESESIMPLIFIED_CHARSET
UserForm1.TextBox2.Font.Name = ChrW(&H5B8B) + ChrW(&H4F53) '-- 宋体 SimSun font

UserForm1.TextBox2.Text = ChrW(37446)
strTxt2 = UserForm1.TextBox2.Text
'notice that ChrW(9246) produces a different character in Chinese
UserForm1.TextBox2.Text = strTxt2 & " " & ChrW(9246)
End Sub

Output in VBE IDE: You may give it a try in VB6 form as well.

enter image description here

After all the above writing, I noticed this MSDN article. Well at least it's VB confirmation :D