有需求或技術問題可以隨時跟我連絡 (MSN上線時)

2009年8月12日 星期三

簡體中文 繁體中文 轉換

UI Layout如下圖所示:


重要的Using:
using System.Runtime.InteropServices;

重要程式碼:
namespace 簡繁中文轉換
{
public partial class Form1 : Form
{
internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

//Import WIN32API:LCMapString
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);


public Form1()
{
InitializeComponent();
}

private void button_Translate_Click(object sender, EventArgs e)
{
if (textBox_TC.Text != string.Empty)
{
textBox_SC.Text = ToSimplified(textBox_TC.Text);
}
else if (textBox_SC.Text != string.Empty)
{
textBox_TC.Text = ToTraditional(textBox_SC.Text);
}
}
///
/// 繁體轉簡體
///

/// 要轉換的繁體字:體
/// 轉換後的簡體字:体
public static string ToSimplified(string pSource)
{
String tTarget = new String(' ', pSource.Length);
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
return tTarget;
}

/**////
/// 簡體轉繁體
///

/// 要轉換的繁體字:体
/// 轉換後的簡體字:體
public static string ToTraditional(string pSource)
{
String tTarget = new String(' ', pSource.Length);
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
return tTarget;
}
}
}

參考資料:Win32 Programmer's Reference LCMapString
尚有其他轉換功能,可以自行研究
LOCALE_SYSTEM_DEFAULT = 0x0800;
LCMAP_FULLWIDTH = 0x00800000; //LCMAP_FULLWIDTH (轉全形字)
LCMAP_HALFWIDTH = 0x00400000; //LCMAP_HALFWIDTH (轉半形字)
LCMAP_HIRAGANA = 0x00100000; //LCMAP_HIRAGANA (轉成平假名,日文)
LCMAP_KATAKANA = 0x00200000; //LCMAP_KATAKANA (轉成片假名,日文)
LCMAP_LINGUISTIC_CASING = 0x01000000; //LCMAP_LINGUISTIC_CASINGLCMAP_UPPERCASE (不明)
LCMAP_LOWERCASE = 0x00000100; //LCMAP_LOWERCASE (轉小寫,應該是英文)
LCMAP_SIMPLIFIED_CHINESE = 0x02000000; //LCMAP_SIMPLIFIED_CHINESE (轉簡體)
LCMAP_SORTKEY = 0x00000400; //LCMAP_SORTKEY (不明)
LCMAP_TRADITIONAL_CHINESE = 0x04000000; //LCMAP_TRADITIONAL_CHINESE (轉繁體)
LCMAP_UPPERCASE = 0x00000200; //LCMAP_UPPERCASE (轉大寫,應該是英文)


認證登錄系統圖片產生 .Net 控制項 (續)

下面圖片中的Circle或是Pie的圖形邊緣都有鋸齒出現不怎麼美觀.


找到IdentifyPictureControl.cs中這個DrawANewPicture() Function
加入下面粗體字那行,修正後就可以美觀一點了...
//Private Function
private void DrawANewPicture()
{
myPictureSize = this.Size;
GraphicsPath myGP = new GraphicsPath();
myBitmap = new Bitmap(this.Width, this.Height);
Graphics myG = Graphics.FromImage(myBitmap);
//GDI+ 修飾用屬性
myG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
myG.Clear(Color.White);
....

修正後,看起來舒服多了...


2 8 10 16進位制轉換

UI Layout如下圖所示:

重要程式碼:
namespace 進制轉換
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox_From.SelectedIndex = 0;
comboBox_To.SelectedIndex = 2;
textBox_Source.Text = "0";
}

private void button1_Click(object sender, EventArgs e)
{
textBox_Result.Text = Convert2UrBase(textBox_Source.Text, int.Parse(comboBox_From.SelectedItem.ToString()), int.Parse(comboBox_To.SelectedItem.ToString())); }
public string Convert2UrBase(String Input_Value, int FromBase, int ToBase)
{
long RtnValue;
try
{
RtnValue = Convert.ToInt64(Input_Value, FromBase);
}
catch (Exception other)
{
RtnValue = 0;
}
return Convert.ToString(RtnValue, ToBase);
}

}
}

中華民國身分證驗證程式碼

驗證規則,如下圖所示



C#程式碼:
public bool CheckIdentificationId(string Input_ID)
{
bool IsTrue = false;
if (Input_ID.Length == 10)
{
Input_ID = Input_ID.ToUpper();
if (Input_ID[0] >= 0x41 && Input_ID[0] <= 0x5A)
{
int[] Location_No = new int[] { 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33 };
int[] Temp = new int[11];
Temp[1] = Location_No[(Input_ID[0]) - 65] % 10;
int Sum = Temp[0] = Location_No[(Input_ID[0]) - 65] / 10;
for (int i = 1; i <= 9; i++)
{
Temp[i + 1] = Input_ID[i] - 48;
Sum += Temp[i] * (10 - i);
}
if (((Sum % 10) + Temp[10]) % 10 == 0)
{
IsTrue = true;
}
}
}
return IsTrue;
}

應用範例:
bool Result = CheckIdentificationId(textBox_ID.Text);

修改字串編碼方式(Encoding)

有時候會出現顯示會出現亂碼,通常是編碼方式出了問題.
我們可以理用Encoding來解決這個問題...
範例程式:
string Source = "轉換編碼方式範例字串";
byte[] Target_BIG5 = Encoding.Default.GetBytes(Source ); //將字串轉為byte[], 中文版Default就是指BIG5編碼
byte[] Target_UTF8 = Encoding.Convert(Encoding.Default, Encoding.UTF8, Target_BIG5 );//轉碼Encoding.Convert(Source Encoder,Target Encoder , Source Variable)
MessageBox.Show(Encoding.UTF8.GetString(Target_UTF8 ));//顯示轉為UTF8後的字串

C# 3.0提供Dictionary新的宣告法

這是3.0以前的宣告方式
Dictionary myDictionary = new Dictionary();
myDictionary.Add("Candy", "住址wwwwww");
myDictionary.Add("Jony", "住址xxxxx");
myDictionary.Add("Mary", "住址yyyyy");
myDictionary.Add("Nelson", "住址zzzzz");


3.0起,強化了Collection Initializer,所以你可以改成這樣的宣告
Dictionary dctNewWay = new Dictionary() {
{"Candy", "住址wwwwww"},
{"Jony", "住址xxxxx"},
{"Mary", "住址yyyyy"},
{"Nelson", "住址zzzzz"}
};

這樣的方式
就像是宣告固定元素的陣列
string[] NameList = { "Candy", "Jony", "Mary", "Nelson" };