本博客(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 settings int 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 table ReadConversionTableList(hListViewWnd); //show the talbe ShowTableInListView(); } } private void btnWrite_Click(object sender, EventArgs e) { //format the text string str = FormatText(); //write the table to file WriteTableToFile(str); MessageBox.Show(" write to file successfully! ", "information prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } //false: no signal at the beginning static AutoResetEvent autoResetEvent = new AutoResetEvent(false); public int GetListViewWnd() { //get the listview handle int 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 item int count = Win32API.SendMessage(hListViewWnd, Win32Data.LVM_GETITEMCOUNT, 0, 0); uint processid = 0; uint threadid = Win32API.GetWindowThreadProcessId(hListViewWnd, ref processid); //open process IntPtr vProcess = Win32API.OpenProcess(Win32Data.PROCESS_VM_OPERATION | Win32Data.PROCESS_VM_READ | Win32Data.PROCESS_VM_WRITE, false, processid); //allocate memory in this process address space IntPtr 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 process Win32API.WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(LVItem)), ref vNumberOfBytesRead); //let the target process read item text to this struct Win32API.SendMessage(hListViewWnd, Win32Data.LVM_GETITEMTEXT, i, vPointer.ToInt32()); //read this struct Win32API.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 array liData.Add(strTemp); } } finally { Win32API.VirtualFreeEx(vProcess, vPointer, 0, Win32Data.MEM_RELEASE); Win32API.CloseHandle(vProcess); } } public void ShowTableInListView() { //insert this item text to a listview int 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 texts for (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); else sb.AppendFormat("{0,-14}", strText); } } return sb.ToString(); } public void WriteTableToFile(string str) { //write to file FileStream 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 work public 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)相关推荐

  1. 利用Python读取并操作注册表

    利用Python读取并操作注册表 一.Python与注册表相关的库 winreg(主要) win32api 二.注册表结构:键.值 三.winreg--访问Windows注册表 1.模块功能: win ...

  2. 添加微软自带日文输入方法

    先检查C:\WINDOWS下的ime文件是否为86.4M,如果小于的话表示安装系统时为精简版的,省去了很多文件.解决方法: 1:先把ime文件复制覆盖掉到C:\WINDOWS  覆盖掉ime文件.不要 ...

  3. 利用pandas读取SQL Sever数据表

    python的pandas库读取SQL sever有两种方法.一种使用pymssql,另一种使用sqlalchemy.这里只是将数据库中的表读取为DataFrame,不进行修改等表操作. 目录 pyt ...

  4. html读取用户输入,HTML表单处理用户输入(示例代码)

    1.HTML表单用于收集用户输入.通过 标签定义 2. 元素是最重要的文本元素. 2.1.text定义常规文本输入,password定义密码字段 User name: User password: 2 ...

  5. Snagit_日文输入法:促音,长音,小写的输入方法

    Snagit使用经典 SnagIt使用方法 日文输入法:促音,长音,小写的输入方法 收藏 61 语种: 日语 分类: 日语输入法 将键盘输入调成日语输入法,根据罗马音输入,平假名和片假名还好,就是促音 ...

  6. 方法的调用:虚方法表

    在面向对象的编程中,会很频繁的使用到动态分派,如果在每次动态分派的过程中都要重新在类的方法元数据中搜索合适的目标的话就可能影响到执行效率.因此,为了提高性能,JVM采用在类的方法区建立一个虚方法表 ( ...

  7. python键盘输入数组_python 二维数组切割Python读取键盘输入的2种方法

    Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.如下: 1.raw_input 2.input raw_input函数raw_input() 函数从标准输入读取一个行,并返 ...

  8. python二维数组换行输出_python 二维数组切割Python读取键盘输入的2种方法

    Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.如下: 1.raw_input 2.input raw_input函数raw_input() 函数从标准输入读取一个行,并返 ...

  9. java利用poi读取excel_java利用POI读取excel文件的方法

    摘要:利用java读取excel文件,读取文件并获取文件中每一个sheet中的值. 一.需要提前导入的包: import java.io.File; import java.io.FileInputS ...

最新文章

  1. collections deque队列及其他队列
  2. 3.C#基础之基本概念(完成)
  3. 程序员面试金典 - 面试题 16.21. 交换和(哈希set)
  4. python董付国教程_董付国老师python教学——学习笔记(一)
  5. 安装redis3.2.4集群时出现的一些坑
  6. LeetCode 242 Valid Anagram
  7. Nginx: 解决connect() to xxxx failed (13: Permission denied) while connecting to upstream的问题
  8. 走近夜间灯光——教你平均灯光指数(ANLI)如何得到(超详细)
  9. 手机定位折腾记(1):安卓手机的网络定位与NetworkLocation.apk
  10. 当生命科学遇上AI,会产生怎样1+1>2的效果?
  11. 35岁之前不应该错过的30本书
  12. 课程笔记《李彦宏·智能交通7讲》
  13. 【每日蓝桥】8、一三年省赛Java组真题“打印十字图”
  14. DirectX11实现FFT海面模拟
  15. spring简单教程
  16. 我只是一名创业者,不是成功者
  17. 用计算机遥感技术图片,遥感简答题
  18. 详解DDD“洋葱架构”
  19. 前端开发:Vue中v-if和v-show的使用以及应用场景
  20. 【不得不看】aiwi游戏电脑安装步骤

热门文章

  1. ae稳定不能获取动态服务器,ae 获取动态链接服务器超时
  2. 机器学习学习笔记 --- 标称型数据和数值型数据
  3. 两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对
  4. bzoj 3620: 似乎在梦中见过的样子
  5. Java中抽象类与方法的重写
  6. 读过的laravel文章
  7. Yolov1、Yolov2、Yolov3学习笔记
  8. 申请了SSL数字证书如何进行域名验证?
  9. 我所学到的EC-2(个人学习总结,不能保证正确,欢迎大佬指正)
  10. JESD204标准概述