利用API读取日文输入方法表(Romaji-Kana conversion table)
本博客(http://blog.csdn.net/livelylittlefish)貼出作者(三二一、小魚)相關研究、學習內容所做的筆記,歡迎廣大朋友指正
?
利用API讀取日文輸入轉換表(Romaji-Kana conversion table)
?
1. 引子
?
輸入日語的時候有很多輸入知道讀法卻不知道如何輸入,例如,“すぇ”應該輸入“swe”,即Romaji和Kana的轉換方法。日語的輸入中共有277種輸入,如果知道這些輸入方法,輸入日語時便不會有障礙。
?
2. Romaji-Kana conversion table的位置
?
后來發現該輸入方法在輸入法設置中能夠找到。在“文字服務和輸入語言”對話框中,選擇日語輸入“Microsoft IME standard”鍵盤(版本不限),如下圖所示。
?
?
查看該鍵盤的屬性,如下圖所示。
?
?
?
點擊"Advanced..."按鈕,出現“Microsoft IME advanced settings”對話框,其“Romaji-Kana conversion”頁中的“Romaji-Kana conversion table”即是我們要的,如下圖所示。
?
?
?
3. 利用API讀取Romaji-Kana conversion table
?
本文的目的就是讀取“Romaji-Kana conversion table”,但是如何在應用程序中讀取該窗口的內容,我們自然會想到利用API來操縱該窗口及其中的元素(控件)。
其中較為麻煩的是該table存放在ListView中,微軟的ListView控件操作較為麻煩,我們在C#中需要重新定義LVItem結構。
?
具體編程方法參見以下代碼。
?
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Collections; using System.IO; using System.Diagnostics; using System.Threading;namespace ReadRomajiKanaConversionTable {public partial class frmMain : Form{//define struct of List View Item[StructLayout(LayoutKind.Sequential)]public struct LVItem{public int mask;public int iItem;public int iSubItem;public int state;public int stateMask;public IntPtr pszText;public int cchTextMax;public int iImage;public IntPtr lParam;public int iIndent;public int iGroupId;public int cColumns;public IntPtr puColumns;}ArrayList liData = new ArrayList();public frmMain(){InitializeComponent();}private void btnClose_Click(object sender, EventArgs e){this.Close();}private void btnRead_Click(object sender, EventArgs e){//get process of IME advanced settingsint hListViewWnd = GetListViewWnd();if (hListViewWnd == 0){MessageBox.Show(" please open the advanced settings! ", "information prompt",MessageBoxButtons.OKCancel, MessageBoxIcon.Information);}else{lvTable.Items.Clear();liData.Clear();//read the tableReadConversionTableList(hListViewWnd);//show the talbeShowTableInListView();}}private void btnWrite_Click(object sender, EventArgs e){//format the textstring str = FormatText();//write the table to fileWriteTableToFile(str);MessageBox.Show(" write to file successfully! ", "information prompt",MessageBoxButtons.OKCancel, MessageBoxIcon.Information);}//false: no signal at the beginningstatic AutoResetEvent autoResetEvent = new AutoResetEvent(false);public int GetListViewWnd(){//get the listview handleint hSettingsWnd = Win32API.FindWindow("#32770", "Microsoft IME advanced settings");int hConversionWnd = Win32API.FindWindowEx(hSettingsWnd, 0, "#32770", "Romaji-Kana conversion");int hListViewWnd = Win32API.FindWindowEx(hConversionWnd, 0, "SysListView32", "List1");return hListViewWnd;}static void GetSettingsHandle(){while (true){autoResetEvent.WaitOne();int hSettingsWnd = Win32API.FindWindow("#32770", "Microsoft IME advanced settings");if (hSettingsWnd>0)break;}}public void ReadConversionTableList(int hListViewWnd){//get the number of list itemint count = Win32API.SendMessage(hListViewWnd, Win32Data.LVM_GETITEMCOUNT, 0, 0);uint processid = 0;uint threadid = Win32API.GetWindowThreadProcessId(hListViewWnd, ref processid);//open processIntPtr vProcess = Win32API.OpenProcess(Win32Data.PROCESS_VM_OPERATION | Win32Data.PROCESS_VM_READ |Win32Data.PROCESS_VM_WRITE, false, processid);//allocate memory in this process address spaceIntPtr vPointer = Win32API.VirtualAllocEx(vProcess, IntPtr.Zero, 4096, Win32Data.MEM_RESERVE |Win32Data.MEM_COMMIT, Win32Data.PAGE_READWRITE);lvTable.View = View.Details;string strText = string.Empty;try{for (int i = 0; i < count; i++){byte[] buffer = new byte[100];LVItem[] vItem = new LVItem[1];vItem[0].mask = Win32Data.LVIF_TEXT;vItem[0].iItem = i;vItem[0].iSubItem = 0;vItem[0].cchTextMax = buffer.Length;vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVItem)));uint vNumberOfBytesRead = 0;//write the struct to the memory of target processWin32API.WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),Marshal.SizeOf(typeof(LVItem)), ref vNumberOfBytesRead);//let the target process read item text to this structWin32API.SendMessage(hListViewWnd, Win32Data.LVM_GETITEMTEXT, i, vPointer.ToInt32());//read this structWin32API.ReadProcessMemory(vProcess, (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVItem))),Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0), buffer.Length, ref vNumberOfBytesRead);string strTemp = Marshal.PtrToStringUni(Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0));//add this item text to arrayliData.Add(strTemp);}}finally{Win32API.VirtualFreeEx(vProcess, vPointer, 0, Win32Data.MEM_RELEASE);Win32API.CloseHandle(vProcess);}}public void ShowTableInListView(){//insert this item text to a listviewint i;for (i = 0; i < liData.Count; i += 5){if (liData.Count - i < 5)break;ListViewItem li = new ListViewItem();li.SubItems.Clear();li.SubItems[0].Text = liData[i].ToString();li.SubItems.Add(liData[i + 1].ToString());li.SubItems.Add(liData[i + 2].ToString());li.SubItems.Add(liData[i + 3].ToString());li.SubItems.Add(liData[i + 4].ToString());lvTable.Items.Add(li);}ListViewItem li2 = new ListViewItem();li2.SubItems.Clear();li2.SubItems[0].Text = liData[i].ToString();for (int j = i + 1; j < liData.Count; j++){li2.SubItems.Add(liData[j].ToString());}lvTable.Items.Add(li2);}public string FormatText(){StringBuilder sb = new StringBuilder(5000);//format this textsfor (int i = 0; i < liData.Count; i++){string strText = liData[i].ToString();if ((i + 1) % 5 == 0)sb.AppendFormat(strText.Trim() + "/r/n");else{string[] temp = strText.Split('=');if (temp[0].Trim().Length == 1)sb.AppendFormat("{0,-15}", strText);elsesb.AppendFormat("{0,-14}", strText);}}return sb.ToString();}public void WriteTableToFile(string str){//write to fileFileStream fs = new FileStream("Romaji-Kana conversion table.txt", FileMode.Create, FileAccess.Write);byte[] byteArray = Encoding.Default.GetBytes(str);fs.Write(byteArray, 0, byteArray.Length);fs.Flush();fs.Close(); }}public class Win32Data{public const int LB_GETTEXT = 0x0189;public const int LB_GETCOUNT = 0x018B;public const int LVM_FIRST = 0x1000;public const int LVM_GETITEMCOUNT = LVM_FIRST + 4;public const int LVM_GETITEMTEXT = LVM_FIRST + 75; //LVM_FIRST + 45 doesn't workpublic const uint PROCESS_VM_OPERATION = 0x0008;public const uint PROCESS_VM_READ = 0x0010;public const uint PROCESS_VM_WRITE = 0x0020;public const uint MEM_COMMIT = 0x1000;public const uint MEM_RELEASE = 0x8000;public const uint MEM_RESERVE = 0x2000;public const uint PAGE_READWRITE = 4;public const int LVIF_TEXT = 0x0001;}public class Win32API{#region api function[DllImport("user32.dll")]public static extern int FindWindow(string lpClassName, string lpWindowName);[DllImport("User32.dll")]public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);[DllImport("User32.dll")]public static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);[DllImport("User32.dll")]public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);[DllImport("User32.dll")]public static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);[DllImport("kernel32.dll")]public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint lpNumberOfBytesRead);[DllImport("kernel32.dll")]public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);[DllImport("User32.dll", CharSet = CharSet.Auto)]public static extern uint GetWindowThreadProcessId(int hwnd, ref uint lpdwProcessId);[DllImport("kernel32.dll")]public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);[DllImport("kernel32.dll")]public static extern bool CloseHandle(IntPtr hObject);[DllImport("kernel32.dll")]public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);[DllImport("kernel32.dll")]public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);#endregion}
?
4. 輸出Romaji-Kana conversion table
?
?
?
輸出結果如下:
?
ぁ = la ぁ = xa あ = a ぃ = li ぃ = lyi ぃ = xi ぃ = xyi い = i い = yi いぇ = ye ぅ = lu ぅ = xu う = u う = whu う = wu うぁ = wha うぃ = whi うぃ = wi うぇ = we うぇ = whe うぉ = who ぇ = le ぇ = lye ぇ = xe ぇ = xye え = e ぉ = lo ぉ = xo お = o か = ca か = ka が = ga き = ki きぃ = kyi きぇ = kye きゃ = kya きゅ = kyu きょ = kyo ぎ = gi ぎぃ = gyi ぎぇ = gye ぎゃ = gya ぎゅ = gyu ぎょ = gyo く = cu く = ku く = qu くぁ = kwa くぁ = qa くぁ = qwa くぃ = qi くぃ = qwi くぃ = qyi くぅ = qwu くぇ = qe くぇ = qwe くぇ = qye くぉ = qo くぉ = qwo くゃ = qya くゅ = qyu くょ = qyo ぐ = gu ぐぁ = gwa ぐぃ = gwi ぐぅ = gwu ぐぇ = gwe ぐぉ = gwo け = ke げ = ge こ = co こ = ko ご = go さ = sa ざ = za し = ci し = shi し = si しぃ = syi しぇ = she しぇ = sye しゃ = sha しゃ = sya しゅ = shu しゅ = syu しょ = sho しょ = syo じ = ji じ = zi じぃ = jyi じぃ = zyi じぇ = je じぇ = jye じぇ = zye じゃ = ja じゃ = jya じゃ = zya じゅ = ju じゅ = jyu じゅ = zyu じょ = jo じょ = jyo じょ = zyo す = su すぁ = swa すぃ = swi すぅ = swu すぇ = swe すぉ = swo ず = zu せ = ce せ = se ぜ = ze そ = so ぞ = zo た = ta だ = da ち = chi ち = ti ちぃ = cyi ちぃ = tyi ちぇ = che ちぇ = cye ちぇ = tye ちゃ = cha ちゃ = cya ちゃ = tya ちゅ = chu ちゅ = cyu ちゅ = tyu ちょ = cho ちょ = cyo ちょ = tyo ぢ = di ぢぃ = dyi ぢぇ = dye ぢゃ = dya ぢゅ = dyu ぢょ = dyo っ = ltsu っ = ltu っ = xtu つ = tsu つ = tu つぁ = tsa つぃ = tsi つぇ = tse つぉ = tso づ = du て = te てぃ = thi てぇ = the てゃ = tha てゅ = thu てょ = tho で = de でぃ = dhi でぇ = dhe でゃ = dha でゅ = dhu でょ = dho と = to とぁ = twa とぃ = twi とぅ = twu とぇ = twe とぉ = two ど = do どぁ = dwa どぃ = dwi どぅ = dwu どぇ = dwe どぉ = dwo な = na に = ni にぃ = nyi にぇ = nye にゃ = nya にゅ = nyu にょ = nyo ぬ = nu ね = ne の = no は = ha ば = ba ぱ = pa ひ = hi ひぃ = hyi ひぇ = hye ひゃ = hya ひゅ = hyu ひょ = hyo び = bi びぃ = byi びぇ = bye びゃ = bya びゅ = byu びょ = byo ぴ = pi ぴぃ = pyi ぴぇ = pye ぴゃ = pya ぴゅ = pyu ぴょ = pyo ふ = fu ふ = hu ふぁ = fa ふぁ = fwa ふぃ = fi ふぃ = fwi ふぃ = fyi ふぅ = fwu ふぇ = fe ふぇ = fwe ふぇ = fye ふぉ = fo ふぉ = fwo ふゃ = fya ふゅ = fyu ふょ = fyo ぶ = bu ぷ = pu へ = he べ = be ぺ = pe ほ = ho ぼ = bo ぽ = po ま = ma み = mi みぃ = myi みぇ = mye みゃ = mya みゅ = myu みょ = myo む = mu め = me も = mo ゃ = lya ゃ = xya や = ya ゅ = lyu ゅ = xyu ゆ = yu ょ = lyo ょ = xyo よ = yo ら = ra り = ri りぃ = ryi りぇ = rye りゃ = rya りゅ = ryu りょ = ryo る = ru れ = re ろ = ro ゎ = lwa ゎ = xwa わ = wa を = wo ん = nn ん = xn ヴ = vu ヴぁ = va ヴぃ = vi ヴぃ = vyi ヴぇ = ve ヴぇ = vye ヴぉ = vo ヴゃ = vya ヴゅ = vyu ヴょ = vyo ヵ = lka ヵ = xka ヶ = lke ヶ = xke
總結
以上是生活随笔為你收集整理的利用API读取日文输入方法表(Romaji-Kana conversion table)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows XP系统输入法不见了怎么
- 下一篇: clickhouse 新增列_# 记录一