萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

VB6 實現 MUI 多語系程式完整解決方案:ADODB.Stream 編碼轉換核心技術

VB6 MUI 核心難點

VB6 標準控制項 (Label、TextBox 等) 內部以 ANSI 顯示文字,不支援 Unicode 直接渲染。雖然 VB6 字串內部為 UTF-16 (Unicode),但顯示管線為:

  1. Unicode 字串 → 依系統預設代碼頁 (ACP) 轉為 ANSI 位元組。
  2. ANSI 位元組 → 依控制項 Font.Charset 指定字符集再轉換。
  3. 轉換失敗 → 顯示「?」或亂碼。

因此,若要在同一程式中動態切換中/英/日/韓/德/法/西等語言,必須解決「Unicode 資源 → 目標字符集 ANSI」的轉換問題。

兩大技術路線比較

方案核心技術優點缺點
替換控制項Forms 2.0 (fm20.dll) / Unicode 控制項原生支援 Unicode,無需轉換部署依賴 fm20.dll、授權限制、舊專案改動大
資源轉換ADODB.Stream / MultiByteToWideChar API零元件依賴、相容標準控制項、部署簡單需正確對應字符集、字型

本文採用方案二:ADODB.Stream (msado25.tlb),純 VB6 程式碼,無第三方付費元件。

完整實作架構

1. Unicode 資源檔準備

  • 所有顯示文字存為 UTF-16 LE (Unicode) 編碼文字檔 (記事本「另存新檔」→ 編碼選 Unicode)。
  • 檔案命名慣例:Resources_zh-CN.txt、Resources_ja-JP.txt、Resources_en-US.txt。
  • 優點:免去 UTF-8/UTF-7 → UTF-16 中間轉換,直接讀入 VB6 字串變數。

2. 執行期字型與字符集動態切換

' 根據偵測到的語言設定字型與 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) 實例,用於自動偵測字串語言/字符集 (下載)。

3. ADODB.Stream 核心轉換函式

' 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

4. 完整顯示流程

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,取得純目標編碼位元組。
  • 第二次轉換時,以目標字符集開啟 Stream 寫入位元組,再切回 adTypeText 讀取,即得到系統 ACP (Locale) 字串,標準控制項可正確顯示。
  • GetSystemDefaultLCID API 取得作業系統預設地區識別碼,對應字符集映射表需自行維護。

已知限制與替代方案

  • 日文全形標點 (如「。」) 在某些字型下可能缺字 → 改用 MS Gothic / Meiryo。
  • 複雜腳本 (阿拉伯文、泰文、印地文) 需 Uniscribe 支援,VB6 標準控制項渲染有限 → 建議遷移 VB.NET/WinForms/WPF。
  • 大型專案建議改用 資源 DLL 架構 (Satellite DLL),每種語言一個 DLL,載入對應 DLL 取代檔案讀取。

此方案在 2000 年代實際產品 (辦公自動化系統) 中穩定運行,支援 6 國語言動態切換,為 VB6 多語系開發提供了零成本、零外部依賴的經典解法。

copyright © 萬盛學電腦網 all rights reserved