c# API實現簡繁體轉換的方法(kernel32.dll)

iDotNetSpace發表於2009-12-10

1.using System.Runtime.InteropServices;

2.import kernel32.dll

1 [DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
2     public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);
3     const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
4     const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

3.轉簡體

01 //轉簡體
02     protected void btnCHS_Click(object sender, EventArgs e)
03     {
04         string src =TextBox1.Text.Trim();
05         byte[] srcByte = Encoding.Default.GetBytes(src);
06         byte[] desByte = new byte[srcByte.Length];
07         LCMapString(2052, LCMAP_SIMPLIFIED_CHINESE, srcByte, -1, desByte, srcByte.Length);
08         string des = Encoding.Default.GetString(desByte);
09          
10         TextBox2.Text = des;
11   
12     }

4.轉繁體

01 //繁轉簡
02    protected void btnCHZ_Click(object sender, EventArgs e)
03    {
04        string src = TextBox1.Text.Trim();
05        byte[] srcByte = Encoding.Default.GetBytes(src);
06        byte[] desByte = new byte[srcByte.Length];
07        LCMapString(2052, LCMAP_TRADITIONAL_CHINESE, srcByte, -1, desByte, srcByte.Length);
08        string des = Encoding.Default.GetString(desByte);
09        TextBox2.Text = des; 
10    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-622137/,如需轉載,請註明出處,否則將追究法律責任。

相關文章