c# API实现简繁体转换的方法(kernel32.dll)
1.using System.Runtime.InteropServices;
2.import kernel32.dll
[DllImport("kernel32.dll", EntryPoint = "LCMapStringA")] public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest); const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000; const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
3.转简体
//转简体 protected void btnCHS_Click(object sender, EventArgs e) { string src =TextBox1.Text.Trim(); byte[] srcByte = Encoding.Default.GetBytes(src); byte[] desByte = new byte[srcByte.Length]; LCMapString(2052, LCMAP_SIMPLIFIED_CHINESE, srcByte, -1, desByte, srcByte.Length); string des = Encoding.Default.GetString(desByte); TextBox2.Text = des; }
4.转繁体
//繁转简 protected void btnCHZ_Click(object sender, EventArgs e) { string src = TextBox1.Text.Trim(); byte[] srcByte = Encoding.Default.GetBytes(src); byte[] desByte = new byte[srcByte.Length]; LCMapString(2052, LCMAP_TRADITIONAL_CHINESE, srcByte, -1, desByte, srcByte.Length); string des = Encoding.Default.GetString(desByte); TextBox2.Text = des; }