VB6 標準控制項 (Label、TextBox 等) 內部以 ANSI 顯示文字,不支援 Unicode 直接渲染。雖然 VB6 字串內部為 UTF-16 (Unicode),但顯示管線為:
Font.Charset 指定字符集再轉換。因此,若要在同一程式中動態切換中/英/日/韓/德/法/西等語言,必須解決「Unicode 資源 → 目標字符集 ANSI」的轉換問題。
| 方案 | 核心技術 | 優點 | 缺點 |
|---|---|---|---|
| 替換控制項 | Forms 2.0 (fm20.dll) / Unicode 控制項 | 原生支援 Unicode,無需轉換 | 部署依賴 fm20.dll、授權限制、舊專案改動大 |
| 資源轉換 | ADODB.Stream / MultiByteToWideChar API | 零元件依賴、相容標準控制項、部署簡單 | 需正確對應字符集、字型 |
本文採用方案二:ADODB.Stream (msado25.tlb),純 VB6 程式碼,無第三方付費元件。
Resources_zh-CN.txt、Resources_ja-JP.txt、Resources_en-US.txt。' 根據偵測到的語言設定字型與 Charset
Select Case True
Case g_Str.NumJapanese > 0
charset = "Shift_JIS" : txt.Font.Name = "MS UI Gothic" : txt.Font.Charset = 128
Case g_Str.NumKorean > 0
charset = "ks_c_5601-1987" : txt.Font.Name = "GulimChe" : txt.Font.Charset = 129
Case g_Str.NumChinese > 0
charset = "gb2312" : txt.Font.Name = "SimSun" : txt.Font.Charset = 134
Case g_Str.NumCentralEuro > 0
charset = "windows-1250" : txt.Font.Name = "Arial" : txt.Font.Charset = 238
Case g_Str.NumArabic > 0
charset = "windows-1256" : txt.Font.Name = "Traditional Arabic" : txt.Font.Charset = 178
Case g_Str.NumHebrew > 0
charset = "windows-1255" : txt.Font.Name = "David" : txt.Font.Charset = 177
Case g_Str.NumCyrillic > 0
charset = "windows-1251" : txt.Font.Name = "Arial" : txt.Font.Charset = 204
Case g_Str.NumGreek > 0
charset = "windows-1253" : txt.Font.Name = "Arial" : txt.Font.Charset = 161
Case g_Str.NumThai > 0
charset = "windows-874" : txt.Font.Name = "Angsana New" : txt.Font.Charset = 222
Case Else
charset = "windows-1252" : txt.Font.Name = "Arial" : txt.Font.Charset = 0
End Select
g_Str 為免費元件 ChilkatCharset2 (CkString) 實例,用於自動偵測字串語言/字符集 (下載)。
' Unicode 字串 → 指定字符集位元組陣列 (跳過 UTF-8 BOM 3 位元組)
Public Function ConvertStringToBytes(ByVal strText As String, ByVal charset As String) As Byte()
Dim objStream As New ADODB.Stream
objStream.Charset = charset
objStream.Mode = adModeReadWrite
objStream.Type = adTypeText
objStream.Open
objStream.WriteText strText
objStream.Flush
objStream.Position = 0
objStream.Type = adTypeBinary
objStream.Read 3 ' 略過 UTF-8 BOM (EF BB BF)
ConvertStringToBytes = objStream.Read
objStream.Close
End Function
' 指定字符集位元組陣列 → 系統 Locale (ACP) 字串 (供標準控制項顯示)
Public Function ConvertBytesToString(ByRef data() As Byte, ByVal charset As String) As String
Dim objStream As New ADODB.Stream
objStream.Charset = charset
objStream.Mode = adModeReadWrite
objStream.Type = adTypeBinary
objStream.Open
objStream.Write data
objStream.Flush
objStream.Position = 0
objStream.Type = adTypeText
ConvertBytesToString = objStream.ReadText
objStream.Close
End Function
' 取得系統預設 Locale 對應字符集
Public Function GetCharset() As String
Dim lcid As Long: lcid = GetSystemDefaultLCID()
Select Case lcid
Case 1033: GetCharset = "windows-1252" ' 英文 (美國)
Case 2052: GetCharset = "gb2312" ' 簡體中文
Case 1041: GetCharset = "Shift_JIS" ' 日文
Case Else: GetCharset = "windows-1252"
End Select
End Function
Private Sub DisplayResource(resFile As String)
' 1. 以 Unicode 讀取資源檔
Dim fso As New FileSystemObject, ts As TextStream
Set ts = fso.OpenTextFile(resFile, ForReading, False, TristateTrue) ' TristateTrue = Unicode
Dim unicodeText As String: unicodeText = ts.ReadAll: ts.Close
' 2. 偵測語言、設定字型
Dim detector As New CkString: detector.Str = unicodeText
Dim targetCharset As String
' ... (同字型切換邏輯) ...
' 3. Unicode → 目標字符集位元組 → 系統 ACP 字串
Dim bytes() As Byte: bytes = ConvertStringToBytes(unicodeText, targetCharset)
Dim displayText As String: displayText = ConvertBytesToString(bytes, GetCharset())
' 4. 指派給控制項
txtDisplay.Text = displayText
End Sub
ADODB.Stream 同時支援文字/二進位模式切換 (Type 屬性),是編碼轉換核心。Flush、Position=0、切換 adTypeBinary、Read 3 略過 UTF-8 BOM,取得純目標編碼位元組。adTypeText 讀取,即得到系統 ACP (Locale) 字串,標準控制項可正確顯示。GetSystemDefaultLCID API 取得作業系統預設地區識別碼,對應字符集映射表需自行維護。MS Gothic / Meiryo。此方案在 2000 年代實際產品 (辦公自動化系統) 中穩定運行,支援 6 國語言動態切換,為 VB6 多語系開發提供了零成本、零外部依賴的經典解法。