2012年1月31日 星期二

VB.NET多國語言程式 - 已開啟表單的語系切換範例

今天實做Form在開啟狀態下切換語系,如下面兩張圖。
我所使用的讀檔方式是採用ini的讀取方式,並建立兩個ini檔,分別為中文test.CHT與英文test.ENG
當我按中文時,從中文檔讀入
當我按英文時,從英文檔讀入
 

程式如下:
程式中透過按鈕設定路徑,並透過副程式的呼叫執行檔案的讀取,範例如下

Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1

    Dim pathA As String = ""


    Public Declare Function GetPrivateProfileString Lib "kernel32" _
     Alias "GetPrivateProfileStringA" ( _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpApplicationName As String, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpKeyName As String, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpDefault As String, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpReturnedString As StringBuilder, _
     ByVal nSize As UInt32, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String) As UInt32
    Public Declare Function WritePrivateProfileString Lib "kernel32" _
     Alias "WritePrivateProfileStringA" ( _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpApplicationName As String, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpKeyName As String, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpReturnedString As StringBuilder, _
     <MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String) As UInt32
    Dim sKeyValue As New StringBuilder(1024)
    Dim nSize As UInt32 = Convert.ToUInt32(1024)
    Dim sinifilename As String = Application.StartupPath & pathA    '讀檔檔名位置

    Public Sub renew()

        GetPrivateProfileString("City", "City_1", "", sKeyValue, nSize, Application.StartupPath & pathA)
        Label1.Text = sKeyValue.ToString
        GetPrivateProfileString("City", "City_2", "", sKeyValue, nSize, Application.StartupPath & pathA)
        Label2.Text = sKeyValue.ToString
        GetPrivateProfileString("City", "City_3", "", sKeyValue, nSize, Application.StartupPath & pathA)
        Label3.Text = sKeyValue.ToString
        GetPrivateProfileString("City", "City_4", "", sKeyValue, nSize, Application.StartupPath & pathA)
        Label4.Text = sKeyValue.ToString
        GetPrivateProfileString("City", "City_5", "", sKeyValue, nSize, Application.StartupPath & pathA)
        Label5.Text = sKeyValue.ToString

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        pathA = "\\test.CHT"
        Call renew()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        pathA = "\\test.ENG"
        Call renew()
    End Sub
End Class


 



沒有留言:

張貼留言