詳解VB6實現(xiàn)MUI程序
來源:易賢網(wǎng) 閱讀:3237 次 日期:2014-12-02 15:26:02
溫馨提示:易賢網(wǎng)小編為您整理了“詳解VB6實現(xiàn)MUI程序”,方便廣大網(wǎng)友查閱!

之前,我負責一個VB6編寫的辦公自動化系統(tǒng),要求能在運行時,支持在不同語言間切換(英文,中文,日文,德文,法文和西班牙文);實質(zhì)就是實現(xiàn)一個VB6的MUI程序.

這個需要的困難在于VB6顯示文本的標準控件(Label,Textbox等)不支持Unicode;但字符串在其內(nèi)部是按Unicode保存的,也就是說VB6本身是支持Unicode的.

VB6中,標準控件顯示字符串的過程如下:

1) 標準控件先將Unicode字符串轉(zhuǎn)換成ANSI字符串;

2) 標準控件嘗試將ANSI字符串轉(zhuǎn)換成其Font.Charset屬性中指定的字符集格式的字符串;如果轉(zhuǎn)換失敗,則顯示為問號(?).

具體可參照:Display Unicode Strings in Visual Basic 6.0.

因此,實現(xiàn)MUI程序的思路有兩種:

1) 將標準控件替換為能支持Unicode的控件;

2) 將資源文件中不同字符集的文本,轉(zhuǎn)換為ANSI格式,提供給標準控件使用;

第1)種方式可用 Forms 2.0 (fm20.dll);

第2)種方式,我在網(wǎng)上找到兩篇文章,但各有不足:

a) Auto-Detect Language and Display Unicode in VB6 TextBox and Label Controls;

ChilkatCharset2控件需購買,具對日文的一些標點符號(如全角句號)不支持.

b) How To Convert from ANSI to Unicode & Unicode to ANSI for OLE

沒有測試成功.

下面,著重介紹我自己認為比較成功的實現(xiàn),它使用了ADODB.Stream(msado25.tlb);其實現(xiàn)思路如下:

1) 將程序中用到的文本保存為Unicode格式的資源文件中;

之所以將資源文本保存為雙字節(jié)的Unicode格式,而非Utf8或Utf7格式,可省去從其它格式向VB6支持的Unicode格式轉(zhuǎn)換過程.

2) 將顯示文本的標準控件的Font.Charset,設置為程序要顯示語言所對應的字符集,并設置一種支持這種字符集的字體到Font.Name屬性

If g_Str.NumJapanese > 0 Then

charset = "Shift_JIS"

Text1.Font.Name = "MS UI Gothic"

Text1.Font.charset = 128

ElseIf g_Str.NumKorean > 0 Then

charset = "ks_c_5601-1987"

Text1.Font.Name = "GulimChe"

Text1.Font.charset = 129

ElseIf g_Str.NumCentralEuro > 0 Then

charset = "windows-1250"

Text1.Font.Name = "Arial"

Text1.Font.charset = 238

ElseIf g_Str.NumArabic > 0 Then

charset = "windows-1256"

Text1.Font.Name = "Traditional Arabic"

Text1.Font.charset = 178

ElseIf g_Str.NumHebrew > 0 Then

charset = "windows-1255"

Text1.Font.Name = "David"

Text1.Font.charset = 177

ElseIf g_Str.NumCyrillic > 0 Then

charset = "windows-1251"

Text1.Font.Name = "Arial"

Text1.Font.charset = 204

ElseIf g_Str.NumGreek > 0 Then

charset = "windows-1253"

Text1.Font.Name = "Arial"

Text1.Font.charset = 161

ElseIf g_Str.NumThai > 0 Then

charset = "windows-874"

Text1.Font.Name = "Angsana New"

Text1.Font.charset = 222

ElseIf g_Str.NumChinese > 0 Then

charset = "gb2312"

Text1.Font.Name = "SimSun"

Text1.Font.charset = 134

' An alternative is to use Big5:

' Text1.Font.Name = "MingLiu"

'charset = "big5"

'fontCh = 136

Else

charset = "windows-1252"

Text1.Font.charset = 0

Text1.Font.Name = "Arial" End If 3)

根據(jù)要顯示的語言,利用ADODB.Stream將資源文件中Unicode格式保存的字符串轉(zhuǎn)換成操作系統(tǒng)的區(qū)域語言(Locale)支持字符集的字符串;

' 調(diào)用API來獲取操作系統(tǒng)默認的區(qū)域語言設置(LocaleID)

Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long

' 將Unicode字符串,轉(zhuǎn)換為指定字符集的字節(jié)數(shù)組;

' 用于轉(zhuǎn)換用資源文件中讀取的文本;

Public Function ConvertStringToBytes(ByRef strText As String, charset As String) As Byte()

Dim objStream As ADODB.Stream

Dim data() As Byte

' init stream

Set objStream = New ADODB.Stream

objStream.charset = charset

objStream.Mode = adModeReadWrite

objStream.Type = adTypeText

objStream.Open

' write bytes into stream

objStream.WriteText strText

objStream.Flush

' rewind stream and read text

objStream.Position = 0

objStream.Type = adTypeBinary '

objStream.Read 3 ' skip first 3 bytes as this is the utf-8 marker

data = objStream.Read()

' close up and return

objStream.Close

ConvertStringToUtf8Bytes = data

End Function '

轉(zhuǎn)換為指定字符集的字節(jié)數(shù)組,轉(zhuǎn)換為ANSI(即LocaleID)對應的字符集的字符串; ' 用于將ConvertStringToBytes()返回的字節(jié)數(shù)組轉(zhuǎn)換為GetCharset()字符集的字符串.

Public Function ConvertBytesToString(ByRef data() As Byte, charset As String) As String

Dim objStream As ADODB.Stream

Dim strTmp As String

' init stream

Set objStream = New ADODB.Stream

objStream.charset = charset

objStream.Mode = adModeReadWrite

objStream.Type = adTypeBinary

objStream.Open

' write bytes into stream

objStream.Write data

objStream.Flush

' rewind stream and read text

objStream.Position = 0

objStream.Type = adTypeText

strTmp = objStream.ReadText

' close up and return

objStream.Close

ConvertUtf8BytesToString = strTmp

End Function

'獲取操作系統(tǒng)默認區(qū)域語言對應的字符集

Public Function GetCharset() As String

Dim localeId As Long

Dim charset As String

' 獲取操作系統(tǒng)的LocaleId

localeId = GetSystemDefaultLCID()

Select Case localeId

Case 1033

charset = "windows-1252"

Case 2052

charset = "gb2312"

Case 1041

charset = "Shift_JIS"

Case Else

charset = "windows-1252"

End Select

GetCharset = charset End Function

'顯示資源文本中的文本

Private Sub DisplayText(filename As String)

'保存資源文本中的文本的變量

Dim textBytes As Variant

Dim f As New FileSystemObject

Dim fs As TextStream

'將Unicode文本讀取到變量中

Set fs = f.OpenTextFile(filename, ForReading, False, TristateTrue)

Do While Not fs.AtEndOfStream

textBytes = fs.ReadAll

Loop fs.Close

' Convert to a Unicode string:

Dim s As String

s = textBytes

Dim t As Long

' CkString是免費的第三方組件,可自動判定一個字符串所對應的字符集

' 下載地址:http://www.chilkatsoft.com/download/CkString.zip

Dim g_Str As New CkString

g_Str.Str = s

'獲取資源文本所代表的字符集名稱,設置對應的字體和字符集到textbox上

Dim charset As String

If g_Str.NumJapanese > 0 Then '日文

charset = "Shift_JIS"

Text1.Font.Name = "MS UI Gothic"

Text1.Font.charset = 128

ElseIf g_Str.NumKorean > 0 Then '韓語

charset = "ks_c_5601-1987"

Text1.Font.Name = "GulimChe"

Text1.Font.charset = 129

ElseIf g_Str.NumCentralEuro > 0 Then '中歐語言

charset = "windows-1250"

Text1.Font.Name = "Arial"

Text1.Font.charset = 238

ElseIf g_Str.NumArabic > 0 Then '阿拉伯語

charset = "windows-1256"

Text1.Font.Name = "Traditional Arabic"

Text1.Font.charset = 178

ElseIf g_Str.NumGreek > 0 Then '希臘語

charset = "windows-1253"

Text1.Font.Name = "Arial"

Text1.Font.charset = 161

ElseIf g_Str.NumThai > 0 Then '泰語

charset = "windows-874"

Text1.Font.Name = "Angsana New"

Text1.Font.charset = 222

ElseIf g_Str.NumChinese > 0 Then '中文

charset = "gb2312"

Text1.Font.Name = "SimSun"

Text1.Font.charset = 134

' 繁體則使用Big5:

' Text1.Font.Name = "MingLiu"

'charset = "big5"

'Text1.Font.charset = 136

Else '默認值

charset = "windows-1252"

Text1.Font.charset = 0

Text1.Font.Name = "Arial"

End If

Dim bytes() As Byte

Dim g_OSCharset As String

'獲取操作系統(tǒng)默認語言對應的字符集

g_OSCharset = GetCharset()

'先將Unicode資源文本轉(zhuǎn)換成對應的字節(jié)數(shù)組;

bytes = ConvertStringToBytes(s, charset)

'將字節(jié)數(shù)組轉(zhuǎn)換成ANSI(即默認字符集)對應的字符串

vbstr2 = ConvertBytesToString(bytes, g_OSCharset)

' 設置字符串到VB6的標準控件中

Me.Text1.Text = vbstr2 End Sub

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:詳解VB6實現(xiàn)MUI程序

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)