熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB6實現MUI程序方法詳解

2013-11-13 10:23:49  來源: .NET編程 

  本文將為大家詳細講述的是VB實現MUI程序方法希望本文能給大家的日常開發工作帶來一些啟示

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

  這個需要的困難在於VB顯示文本的標准控件(LabelTextbox等)不支持Unicode;但字符串在其內部是按Unicode保存的也就是說VB本身是支持Unicode的

  VB標准控件顯示字符串的過程如下:

  ) 標准控件先將Unicode字符串轉換成ANSI字符串;

  ) 標准控件嘗試將ANSI字符串轉換成其FontCharset屬性中指定的字符集格式的字符串;如果轉換失敗則顯示為問號(?)

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

  因此實現MUI程序的思路有兩種:

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

  ) 將資源文件中不同字符集的文本轉換為ANSI格式提供給標准控件使用;

  第)種方式可用 Forms (fmdll);

  第)種方式我在網上找到兩篇文章但各有不足:

  a) AutoDetect Language and Display Unicode in VB TextBox and Label Controls;

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

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

  沒有測試成功

  下面著重介紹我自己認為比較成功的實現它使用了ADODBStream(msadotlb);其實現思路如下:

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

  之所以將資源文本保存為雙字節的Unicode格式而非Utf或Utf格式可省去從其它格式向VB支持的Unicode格式轉換過程

  ) 將顯示文本的標准控件的FontCharset設置為程序要顯示語言所對應的字符集並設置一種支持這種字符集的字體到FontName屬性

  If g_StrNumJapanese > Then

  charset = Shift_JIS

  TextFontName = MS UI Gothic

  TextFontcharset =

  ElseIf g_StrNumKorean > Then

  charset = ks_c_

  TextFontName = GulimChe

  TextFontcharset =

  ElseIf g_StrNumCentralEuro > Then

  charset = windows

  TextFontName = Arial

  TextFontcharset =

  ElseIf g_StrNumArabic > Then

  charset = windows

  TextFontName = Traditional Arabic

  TextFontcharset =

  ElseIf g_StrNumHebrew > Then

  charset = windows

  TextFontName = David

  TextFontcharset =

  ElseIf g_StrNumCyrillic > Then

  charset = windows

  TextFontName = Arial

  TextFontcharset =

  ElseIf g_StrNumGreek > Then

  charset = windows

  TextFontName = Arial

  TextFontcharset =

  ElseIf g_StrNumThai > Then

  charset = windows

  TextFontName = Angsana New

  TextFontcharset =

  ElseIf g_StrNumChinese > Then

  charset = gb

  TextFontName = SimSun

  TextFontcharset =

   An alternative is to use Big:

   TextFontName = MingLiu

  charset = big

  fontCh =

  Else

  charset = windows

  TextFontcharset =

  TextFontName = Arial End If )

  根據要顯示的語言利用ADODBStream將資源文件中Unicode格式保存的字符串轉換成操作系統的區域語言(Locale)支持字符集的字符串;

   調用API來獲取操作系統默認的區域語言設置(LocaleID)

  Private Declare Function GetSystemDefaultLCID Lib kernel () As Long

   將Unicode字符串轉換為指定字符集的字節數組;

   用於轉換用資源文件中讀取的文本;

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

  Dim objStream As ADODBStream

  Dim data() As Byte

   init stream

  Set objStream = New ADODBStream

  objStreamcharset = charset

  objStreamMode = adModeReadWrite

  objStreamType = adTypeText

  objStreamOpen

   write bytes into stream

  objStreamWriteText strText

  objStreamFlush

   rewind stream and read text

  objStreamPosition =

  objStreamType = adTypeBinary  

  objStreamRead skip first bytes as this is the utf marker

  data = objStreamRead()

   close up and return

  objStreamClose

  ConvertStringToUtfBytes = data

  End Function 

  轉換為指定字符集的字節數組轉換為ANSI(即LocaleID)對應的字符集的字符串;   用於將ConvertStringToBytes()返回的字節數組轉換為GetCharset()字符集的字符串

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

  Dim objStream As ADODBStream

  Dim strTmp As String

   init stream

  Set objStream = New ADODBStream

  objStreamcharset = charset

  objStreamMode = adModeReadWrite

  objStreamType = adTypeBinary

  objStreamOpen

   write bytes into stream

  objStreamWrite data

  objStreamFlush

   rewind stream and read text

  objStreamPosition =

  objStreamType = adTypeText

  strTmp = objStreamReadText

   close up and return

  objStreamClose

  ConvertUtfBytesToString = strTmp

  End Function

  獲取操作系統默認區域語言對應的字符集

  Public Function GetCharset() As String

  Dim localeId As Long

  Dim charset As String

   獲取操作系統的LocaleId

  localeId = GetSystemDefaultLCID()

  Select Case localeId

  Case

  charset = windows

  Case

  charset = gb

  Case

  charset = Shift_JIS

  Case Else

  charset = windows

  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 = fOpenTextFile(filename ForReading False TristateTrue)

  Do While Not fsAtEndOfStream

  textBytes = fsReadAll

  Loop        fsClose

   Convert to a Unicode string:

  Dim s As String

  s = textBytes

  Dim t As Long

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

   下載地址:

  Dim g_Str As New CkString

  g_StrStr = s

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

  Dim charset As String

  If g_StrNumJapanese > Then  日文

  charset = Shift_JIS

  TextFontName = MS UI Gothic

  TextFontcharset =

  ElseIf g_StrNumKorean > Then 韓語

  charset = ks_c_

  TextFontName = GulimChe

  TextFontcharset =

  ElseIf g_StrNumCentralEuro > Then 中歐語言

  charset = windows

  TextFontName = Arial

  TextFontcharset =

  ElseIf g_StrNumArabic > Then 阿拉伯語

  charset = windows

  TextFontName = Traditional Arabic

  TextFontcharset =

  ElseIf g_StrNumGreek > Then  希臘語

  charset = windows

  TextFontName = Arial

  TextFontcharset =

  ElseIf g_StrNumThai > Then  泰語

  charset = windows

  TextFontName = Angsana New

  TextFontcharset =

  ElseIf g_StrNumChinese > Then 中文

  charset = gb

  TextFontName = SimSun

  TextFontcharset =

   繁體則使用Big:

   TextFontName = MingLiu

  charset = big

  TextFontcharset =

  Else 默認值

  charset = windows

  TextFontcharset =

  TextFontName = Arial

  End If

  Dim bytes() As Byte

  Dim g_OSCharset As String

  獲取操作系統默認語言對應的字符集

  g_OSCharset = GetCharset()

  先將Unicode資源文本轉換成對應的字節數組;

  bytes = ConvertStringToBytes(s charset)

  將字節數組轉換成ANSI(即默認字符集)對應的字符串

  vbstr = ConvertBytesToString(bytes g_OSCharset)

   設置字符串到VB的標准控件中

  MeTextText = vbstr  End Sub


From:http://tw.wingwit.com/Article/program/net/201311/13483.html
  • 上一篇文章:

  • 下一篇文章:
  • Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.