上一篇介绍了用INI文件设置程序显示语言的方法,但那都是正常控件,如果使用了控件数组,那种方法就不能使用了。
下面的方法可以对控件数组进行设置。
1,首先要根据窗体创建INI文件。
Public Sub prCreateIniForArray() '根据控件数组创建INI文件 ' Tao Add to Set Chinese by Ini, --20110816Dim pFileName, pstr As StringDim pFileNum, pIDim plblName As StringpFileName = App.Path & "ini" & App.EXEName & "_lang_" & Me.Name & ".ini"pFileNum = FreeFileOpen pFileName For Output As #pFileNum'写入空行pstr = " "Print #pFileNum, pstr'处理控件数组开始---((( 如有多个控件数组,请在下面重复执行此过程plblName = "fraDraw" '----------设定控件数组名称'写入节名pstr = "[" & Me.Name & "." & plblName & "]"Print #pFileNum, pstr'循环各控件,写入Ini文件For pI = fraDraw.LBound To fraDraw.UBound '----------设定控件数组名称If fChkControls(frmInfo, "fraDraw", pI) = True Then '----------设定控件数组名称pstr = " " & pI & " = " & fraDraw(pI).Caption '----------设定控件数组名称Print #pFileNum, pstrEnd IfNext pI'处理控件数组结束---)))'处理控件数组开始---((( 如有多个控件数组,请在下面重复执行此过程plblName = "lblData" '----------设定控件数组名称'写入节名pstr = "[" & Me.Name & "." & plblName & "]"Print #pFileNum, pstr'循环各控件,写入Ini文件For pI = lblData.LBound To lblData.UBound '----------设定控件数组名称If fChkControls(frmInfo, "lblData", pI) = True Then '----------设定控件数组名称pstr = " " & pI & " = " & lblData(pI).Caption '----------设定控件数组名称Print #pFileNum, pstrEnd IfNext pI'处理控件数组结束---)))'处理控件数组开始---((( 如有多个控件数组,请在下面重复执行此过程plblName = "lblHWY" '----------设定控件数组名称'写入节名pstr = "[" & Me.Name & "." & plblName & "]"Print #pFileNum, pstr'循环各控件,写入Ini文件For pI = lblHWY.LBound To lblHWY.UBound '----------设定控件数组名称If fChkControls(frmInfo, "lblHWY", pI) = True Then '----------设定控件数组名称pstr = " " & pI & " = " & lblHWY(pI).Caption '----------设定控件数组名称Print #pFileNum, pstrEnd IfNext pI'处理控件数组结束---)))'如有多个控件数组,请在下面重复执行上面过程'----------(('----------))'处理完成后关闭文件Close #pFileNumMsgBox "Ini file for [" & Me.Name & "] have created success!"End Sub
然后,在窗体上添加一按钮,执行以下代码,对每个窗体生成ini文件:
Private Sub Command1_Click() ' Tao Add to Set Chinese by Ini, --20110816frmData.prCreateIniForArrayfrmData2.prCreateIniForArrayfrmInfo.prCreateIniForArrayEnd Sub
将各窗体生成的ini文件合并,命名为App.EXEName_Lang.ini。保存目录:App.Pathini
生成的ini文件内容示例如下:
[COMMON]'此部分为公用过程,请直接添加LANGUAGE = CHINESE'LANGUAGE = JAPANESE'系统语言WINDOWS=CHINESE'WINDOWS=JAPANESE'控件caption字符集Proportional Font = Simsun'Proportional Font = MS PGothic'控件文体内容字符集Regular Font = Simsun'Regular Font = MS Gothic[frmData.lblData]0 = 工号1 = 工名2 = 图纸的种类'......[frmData2.lblData]0 = 工号1 = 工名2 = 图纸的种类3 = 用纸4 = 横向'......[frmInfo.fraDraw]1 = 1-RC柱2 = 2-升降机器'......[frmInfo.lblData]1 = (外侧)2 = (内侧)3 = (外侧)9 = A'......
2,使用INI文件设置窗体显示:在每个窗体中添加以下过程,并在form_load中调用此过程。
Public Sub prSetDisplay_Array() '根据INI文件,设定控件数组显示 ' Tao Add to Set Chinese by Ini, --20110816Dim pIniFile As StringDim plblName As StringDim plblIdx As IntegerplblName = "lblData" '---------设定标签数组名称-----------pIniFile = App.Path & "ini" & App.EXEName & "_lang.ini"If Dir(pIniFile) = "" ThenMsgBox "Ini File [" + pIniFile + "] not found !!", vbCritical + vbOKOnlyExit SubEnd IfDim pLang$, pPrp_Font, pReg_Font, pWin$Dim pCharset_datapLang$ = cfReadIniFile(pIniFile, "COMMON", "LANGUAGE") ' "JAPANESE" or "CHINESE"pPrp_Font = cfReadIniFile(pIniFile, "COMMON", "Proportional Font")pReg_Font = cfReadIniFile(pIniFile, "COMMON", "Regular Font")pWin$ = cfReadIniFile(pIniFile, "COMMON", "WINDOWS") ' "JAPANESE" or "CHINISE"If (pLang$ = "JAPANESE") And (pWin$ = "JAPANESE") ThenpCharset_data = 1ElseIf (pLang$ = "JAPANESE") And (pWin$ = "CHINESE") ThenpCharset_data = 1 '128ElseIf (pLang$ = "CHINESE") And (pWin$ = "CHINESE") ThenpCharset_data = 1ElseIf (pLang$ = "CHINESE") And (pWin$ = "JAPANESE") ThenpCharset_data = 128End IfDim n As IntegerDim dat$, a$Dim pFlag As Booleann = FreeFilepFlag = Falsedat$ = ""'处理控件数组开始---((( 如有多个控件数组,请在下面重复执行此过程Open pIniFile For Input As nplblName = "fraDraw" '---------设定控件数组名称Do While Not EOF(n)Line Input #n, a$a$ = Trim(CutQuote(a$))If a$ <> "" ThenIf Not pFlag ThenIf UCase(a$) = UCase("[" & Me.Name & "." & plblName & "]") ThenpFlag = TrueEnd IfElseIf Left$(a$, 1) = "[" ThenExit DoElseplblIdx = CInt(GetItem(a$, 1))If IsNumeric(plblIdx) ThenfraDraw(plblIdx).FontName = pPrp_FontfraDraw(plblIdx).Font.Charset = pCharset_datafraDraw(plblIdx).Caption = GetItem(a$, 2) '---------设定控件数组名称End IfEnd IfEnd IfEnd IfLoopClose #n'处理控件数组结束---)))'处理控件数组开始---((( 如有多个控件数组,请在下面重复执行此过程pFlag = FalseOpen pIniFile For Input As nplblName = "lblData" '---------设定控件数组名称Do While Not EOF(n)Line Input #n, a$a$ = Trim(CutQuote(a$))If a$ <> "" ThenIf Not pFlag ThenIf UCase(a$) = UCase("[" & Me.Name & "." & plblName & "]") ThenpFlag = TrueEnd IfElseIf Left$(a$, 1) = "[" ThenExit DoElseplblIdx = CInt(GetItem(a$, 1))If IsNumeric(plblIdx) ThenlblData(plblIdx).FontName = pPrp_FontlblData(plblIdx).Font.Charset = pCharset_datalblData(plblIdx).Caption = GetItem(a$, 2) '---------设定控件数组名称End IfEnd IfEnd IfEnd IfLoopClose #n'处理控件数组结束---)))'处理控件数组开始---((( 如有多个控件数组,请在下面重复执行此过程pFlag = FalseOpen pIniFile For Input As nplblName = "lblHWY" '---------设定控件数组名称Do While Not EOF(n)Line Input #n, a$a$ = Trim(CutQuote(a$))If a$ <> "" ThenIf Not pFlag ThenIf UCase(a$) = UCase("[" & Me.Name & "." & plblName & "]") ThenpFlag = TrueEnd IfElseIf Left$(a$, 1) = "[" ThenExit DoElseplblIdx = CInt(GetItem(a$, 1))If IsNumeric(plblIdx) ThenlblHWY(plblIdx).FontName = pPrp_FontlblHWY(plblIdx).Font.Charset = pCharset_datalblHWY(plblIdx).Caption = GetItem(a$, 2) '---------设定控件数组名称End IfEnd IfEnd IfEnd IfLoopClose #n'处理控件数组结束---)))'如有多个控件数组,请在下面重复执行上面过程'----------(('----------))End Sub
3,也需要一些公用过程支持,除了方法(一)中的公用过程,还需要以下过程支持,请将以下代码添加到公用模块中。
Public Function fChkControls(frmObject As Form, strControlsName As String, lngIndex) As BooleanOn Error GoTo ErrDim strContrName As StringIf lngIndex >= 0 ThenstrContrName = frmObject.Controls(strControlsName)(lngIndex).NameElsestrContrName = frmObject.Controls(strControlsName).NameEnd IffChkControls = TrueExit FunctionErr:fChkControls = FalseEnd Function
这样,用INI设置VB显示语言,基本上就可以完全对应了,还是很方便的!
【版權聲明】
本文爲原創,遵循CC 4.0 BY-SA版權協議!轉載時請附上原文鏈接及本聲明。
原文鏈接:https://tdlib.com/am.php?t=Q004vV47Te5w Tag: VB技巧 编程 TTTBLOG