当前位置: 源码素材网 » VB教程 » 详情页

用VB为窗口添加语言切换选择功能-代码完整

  •   时间:2019-01-17
  • 概述:语言切换

看到很多程序的窗口中都有一个多语言切换的功能,自己看着眼痒,索性也写一个,虽然不完善,但凑合可以用,有此方面需要的可仔细看下代码,下面会把代码和bas都发上来供参考,后还附有源码包,Form1.frm主窗口代码:

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "VB Set Language Sample"
   ClientHeight    =   2145
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   2685
   LinkTopic       =   "Form1"
   ScaleHeight     =   2145
   ScaleWidth      =   2685
   StartUpPosition =   2  '屏幕中心
   Begin VB.Frame Frame1
      Caption         =   "Language"
      Height          =   615
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   2415
      Begin VB.ComboBox CmbLan
         Height          =   300
         Left            =   120
         TabIndex        =   1
         Tag             =   "0"
         Text            =   "Combo1"
         Top             =   240
         Width           =   2175
      End
   End
   Begin VB.Label Label2
      Caption         =   "A VB Program     By:Iollcy"
      Height          =   255
      Left            =   120
      TabIndex        =   3
      Top             =   1680
      Width           =   2415
   End
   Begin VB.Label Label1
      Caption         =   "This program can set language by user or auto set language"
      Height          =   615
      Left            =   120
      TabIndex        =   2
      Top             =   840
      Width           =   2295
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'-------------------------------------------------------
'||写了很久了,但不是一个单独的程序                    ||
'||今天没事干,就从那个程序中把选择语言这部分提取出来  ||
'||代码不太精练,欢迎大家一起改进                      ||
'||2010-07-02                                         ||
'||By:Iollcy                                          ||
'-------------------------------------------------------
Option Explicit
Dim TmpLanItem() As String, SysLanCom As String
Private Sub Form_Load()
INI.IniFileName = App.Path & "\language.ini"
Call GetSysLan
Call LoadLanComBox
Call RefItem
End Sub
Private Sub CmbLan_Click()
    If CmbLan.Tag <> 1 Then Exit Sub    '防止被无故调用
    SetLan TmpLanItem(CmbLan.ListIndex) '读取TmpLanItem的语言

    '刷新
    Call RefItem
End Sub
Private Sub CmbLan_GotFocus()
CmbLan.Tag = 1
End Sub
Private Sub CmbLan_LostFocus()
CmbLan.Tag = 0
End Sub
Sub LoadLanComBox()     '载入ini和语言选项给CmbLan
Dim i As Integer
TmpLanItem = Split(INI.GetIniKey("CONFIG", "ITEM"), ",")
For i = 0 To UBound(TmpLanItem)
If TmpLanItem(i) <> "" Then
CmbLan.AddItem INI.GetIniKey(TmpLanItem(i), "NAME")
End If
Next
CmbLan.Text = INI.GetIniKey(SysLanCom, "NAME")
End Sub
'获取语言
Sub GetSysLan()
    Dim SysLan As String
    SysLan = INI.GetIniKey("CONFIG", "DEFAULT") 'CONFIG这一节DEFAULT项(获得LCID的值)

    If SysLan = "" Then     '如果mghack的CONFIG节DEFAULTLAN项为空字符
        SysLan = Trim(Str(GetSystemDefaultLCID))    '读取系统LCID
    End If

    If INI.GetIniKey(SysLan, "NAME") = "" Then  '如果读取ini中SysLan节的NAME项为空
        SysLan = 1033 '找不到系统语言就使用英文
    End If

    SysLanCom = SysLan
    SetLan SysLan   '设置语言
End Sub

ModLanguage.bas

Attribute VB_Name = "ModLanguage"
Option Explicit
Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long '获取系统语言
'语言项目总数.本程序中:窗体标题X1 + Label标题X2 + Frame标题X1=4
Public LanItem(3) As String '数组从0开始 0,1,2,3 正好4个
Public INI As New CIniFile
'Download b yhttp://www.codefans.net
Sub SetLan(Lan)
Dim i As Integer
    If Lan = "1028" Then Lan = "3076"   '如果LCID为台湾的话就以香港的LCID来处理
    For i = 0 To UBound(LanItem)    '循环0到 (LanItem的行的个数) 次
        LanItem(i) = INI.GetIniKey(Lan, Trim(Str(i)))   'LanItem(i)为配置文件里Lan项第i节的内容
    Next
    INI.WriteIniKey "CONFIG", "DEFAULT", Trim(Str(Lan))    '将Lan写入mghack.cfg的CONFIG项DEFAULTLAN节中
End Sub
Sub RefItem() '刷新
'请根据情况更改
'有很多相同的控件时建议用数组,然后用循环来赋植
'多窗体时建议每个窗体写一个刷新过程,因为写在一起的话
'访问窗体或窗体中控件会自动Load窗体,并且Visible为False,不但占资源,影响速度,而且可能出现其他问题
Form1.Caption = LanItem(0)
Form1.Frame1.Caption = LanItem(1)
Form1.Label1.Caption = LanItem(2)
Form1.Label2.Caption = LanItem(3)
End Sub

CIniFile.cls代码:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CIniFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private strINI As String
'读写ini文件API函数声明
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Property Get IniFileName() As String     'IniFileName过程获取一个String属性值(本过程为了获取文件名)
'' Returns the current ini path
IniFileName = strINI    '文件名赋值
End Property
Public Property Let IniFileName(ByVal New_IniPath As String)    '定义IniFileName过程给一个属性赋值
'' Sets the new ini path
strINI = New_IniPath
End Property
Private Sub CreateIni(strDrv As String, strDir As String)   '定义CreateIni过程
'' Make a new ini file
strINI = MakePath(strDrv, strDir)   'strINI赋值
End Sub
'清除KeyWord"键"(Sub)
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)     '定义DelIniKey函数返回变体
Dim RetVal As Integer   'RetVal为整型
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)    '将SectionName节KeyWord项的0&赋值给RetVal
End Function
''如果是清除Section就少写一个Key多一个""。
''清除 Section"段"(Sub)
Public Function DelIniSec(ByVal SectionName As String) ''清除Section的函数
Dim RetVal As Integer   'RetVal为整型
RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)     '将SectionName节0&项的空字符赋值给RetVal,如果默认值为strINI
End Function
Public Function GetIniKey(strSection, strKey As String) As String   'GetIniKey函数返回字符型
    Dim strTmp As String
    Dim lngRet As String
    Dim i As Integer
    Dim strTmp2 As String
    strTmp = String$(1200, Chr(32))      '获得1200长度的的空格符给strTmp(原来为1048576)
    lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)   '读取strINI文件中的strSection节 strKey项 ""字符给lngRet,缓冲字符数量为Len(strTmp)个
    strTmp = Trim(strTmp)   '去掉strTmp的前后空格
    strTmp2 = ""    '赋空值
    For i = 1 To Len(strTmp)    '循环Len(strTmp)次
        If Asc(Mid(strTmp, i, 1)) <> 0 Then     '如果strTmp不是空字符,那么
            strTmp2 = strTmp2 + Mid(strTmp, i, 1)   'strTmp2为strTmp2+strTmp的第i个字符
        End If
    Next i
    strTmp = strTmp2
    GetIniKey = strTmp  '''为什么上面不直接GetIniKey=strTmp????
End Function
Private Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String     'MakePath函数返回一个字符型
'' Makes an INI file: Guarantees a sub dir
Do While Right$(strDrv, 1) = ""     '当strDrv右边第1个字符为空字符的时候??????????
    strDrv = Left$(strDrv, Len(strDrv) - 1)     'strDrv为strDrv长度-1个字符
Loop            '''这个过程就是为了避开strDrv右边的空字符是吧???????
Do While Left$(strDir, 1) = ""      '当strDir左边第1个字符为空字符的时候??????
    strDir = Mid$(strDir, 2)        '去掉strDir左边那个空字符?????
Loop
'' Return the path
MakePath = strDrv & "" & strDir     '去掉各自左,右两边的空字体,相加时只插入一个空字符,为什么不用Replce??????
End Function
Public Sub WriteIniKey(strSection, strKey As String, strValue As String)
'' Write to strINI
WritePrivateProfileString strSection, strKey, strValue, strINI  '将strValue写入strSection节的strKe项,默认值为strINI
End Sub

    相关内容:

    相关声明:

      若“用VB为窗口添加语言切换选择功能-代码完整”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。