原文:c# windows程序调用本地输入法

   好久没写博客了,今天写了一个DEMO,在WINform程序中调用本地输入法,并在窗体中显示出来。其中使用到了很多API,现把代码贴出来,供大家参考

private string _CurrentImeHandleStr = "";
public delegate bool EnumResNameProc(IntPtr hModule, IntPtr nType, StringBuilder sName, IntPtr lParam);
System.ComponentModel.ComponentResourceManager resources = new ComponentResourceManager(typeof(Form17));
public Form17()
{
InitializeComponent();
}
#region API定义

private static readonly int BTN_HEIGHT = 21;
private static readonly int IMAGE_ICON = 1;
private const int DONT_RESOLVE_DLL_REFERENCES = 0x1;
private const int LOAD_LIBRARY_AS_DATAFILE = 0x2;
private const int LOAD_WITH_ALTERED_SEARCH_PATH = 0x8;
private const int RT_ICON = 0x3;
private const int RT_BITMAP = 0x2;
private const int RT_GROUP_ICON = (RT_ICON + 11);

//API定义
[DllImport("Kernel32.dll")]
public extern static bool FreeLibrary(IntPtr hModule);

[DllImport("user32.dll")]
public extern static IntPtr LoadIcon(IntPtr hInstance, string iID);
/// <summary>
/// 得到输入法说明
/// </summary>
/// <param name="Hkl"></param>
/// <param name="sName"></param>
/// <param name="nBuffer"></param>
/// <returns></returns>
[DllImport("Imm32.dll")]
public extern static int ImmGetDescription(IntPtr Hkl, StringBuilder sName, int nBuffer);
/// <summary>
/// 得到输入法的文件名
/// </summary>
/// <param name="Hkl"></param>
/// <param name="sFileName"></param>
/// <param name="nBuffer"></param>
/// <returns></returns>
[DllImport("Imm32.dll")]
public extern static int ImmGetIMEFileName(IntPtr Hkl, StringBuilder sFileName, int nBuffer);

[DllImport("Kernel32.dll")]
public extern static IntPtr LoadLibraryEx(string sFileName, IntPtr hFile, int dwFlags);

[DllImport("Kernel32.dll")]
public extern static bool EnumResourceNames(IntPtr hModule, IntPtr nType, EnumResNameProc lpEnumFunc, int lParam);

[DllImport("shell32.dll")]
public extern static IntPtr ExtractIcon(IntPtr hInstance, string sExeFileName, int nIconIndex);

[DllImport("user32.dll")]
public extern static IntPtr LoadImage(IntPtr hInstance, string sID, int nType, int cx, int cy, int fuLoad);
#endregion

private void Form17_Load(object sender, EventArgs e)
{
//初始化菜单
InitMenus();
}

void Application_Idle(object sender, EventArgs e)
{
if (this._CurrentImeHandleStr == Application.CurrentInputLanguage.Handle.ToString())
return;

//显示新的输入法
ChangeIme(Application.CurrentInputLanguage.Handle);
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
this.contextMenuStrip1.Show(this.toolStrip1, new Point(0, 0), ToolStripDropDownDirection.AboveRight);
}
/// <summary>
/// 初始化菜单
/// </summary>
private void InitMenus()
{
this.contextMenuStrip1.Items.Clear();
string sLayoutName = "";

foreach (InputLanguage item in InputLanguage.InstalledInputLanguages)
{
sLayoutName = GetImmDescription(item);//item.LayoutName; //
if (string.IsNullOrEmpty(sLayoutName))
{
continue;
}
ToolStripMenuItem oMenuItem = new ToolStripMenuItem();
oMenuItem.Checked = (item.Handle.ToString() == InputLanguage.CurrentInputLanguage.Handle.ToString());

oMenuItem.Text = sLayoutName;
oMenuItem.ToolTipText = sLayoutName;
oMenuItem.Click += new EventHandler(oMenuItem_Click);
oMenuItem.Tag = item;
oMenuItem.Image = GetImeBitmap(item);
this.contextMenuStrip1.Items.Add(oMenuItem);
}
}

/// <summary>
/// 得到指定输入法的说明
/// </summary>
/// <param name="hKl"></param>
/// <returns></returns>
private string GetImmDescription(InputLanguage inpt)
{
int nBuffer = 0;

StringBuilder sName = new StringBuilder();
string sDesc = "";

nBuffer = ImmGetDescription(inpt.Handle, null, nBuffer);
sName = new StringBuilder(nBuffer);
ImmGetDescription(inpt.Handle, sName, nBuffer);
sDesc = sName.ToString();
if (string.IsNullOrEmpty(sDesc))
{
sDesc = inpt.LayoutName;
}

return sDesc;
}

/// <summary>
/// 单击输入法事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void oMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem oItem = (ToolStripMenuItem)sender;

foreach (ToolStripMenuItem item in this.contextMenuStrip1.Items)
{
item.CheckState = CheckState.Unchecked;
}
oItem.CheckState = CheckState.Checked;

Application.CurrentInputLanguage = ((InputLanguage)oItem.Tag);
InputLanauageChangedUI();
}

/// <summary>
/// 得到指定输入法的图标
/// </summary>
/// <param name="ime"></param>
/// <returns></returns>
private Image GetImeBitmap(InputLanguage ime)
{
int nBuffer = 0;
StringBuilder sName;
Image oBitmap = null;

//得到IME文件
nBuffer = ImmGetIMEFileName(ime.Handle, null, nBuffer);
sName = new StringBuilder(nBuffer);
ImmGetIMEFileName(ime.Handle, sName, nBuffer);

if (string.IsNullOrEmpty(sName.ToString()))
{
return Properties.Resources.input;

}
else
{
//从资源文件中得到图标
string sFileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), sName.ToString());
if (File.Exists(sFileName))
{
oBitmap = GetBitmapFromResource(sFileName, "");
}
if (oBitmap == null)
{
oBitmap = Properties.Resources.input;
}
return oBitmap;
}
}
private Image GetBitmapFromResource(string sFileName, string sBitmapFlag)
{
Bitmap oBitmap = null;
IntPtr hModule = LoadLibraryEx(sFileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
if (hModule == IntPtr.Zero)
{
System.Diagnostics.Debug.WriteLine("未能成功加载" + sFileName);
return null;
}
string sName = "IMEICO";
IntPtr hIcon = IntPtr.Zero;

System.Diagnostics.Debug.WriteLine("正在获取" + sFileName + "中所有图标。");

hIcon = ExtractIcon(this.Handle, sFileName, 0);

if (hIcon == IntPtr.Zero)
{
sName = "#101";
hIcon = LoadImage(hModule, sName, IMAGE_ICON, 16, 16, 0);
}

if (hIcon != IntPtr.Zero)
{
System.Diagnostics.Debug.WriteLine(string.Format("Hicon:{0}", hIcon.ToString()));
oBitmap = Icon.FromHandle(hIcon).ToBitmap();

}

EnumResourceNames(hModule, this.MAKEINTRESOURCE(RT_GROUP_ICON), this.EnumIconResourceProc, 0);
//释放
FreeLibrary(hModule);
return oBitmap;
}
private IntPtr MAKEINTRESOURCE(int nID)
{
return new IntPtr((long)((short)nID));
}
private bool EnumIconResourceProc(IntPtr hModule, IntPtr nType, StringBuilder sName, IntPtr lParam)
{
System.Diagnostics.Debug.WriteLine(string.Format("得到的资源名称:{0}", sName));
//得到图标
IntPtr hIcon = LoadIcon(hModule, sName.ToString());
Icon icon = Icon.FromHandle(hIcon);

return true;
}
private void Form17_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
{
Application.CurrentInputLanguage = e.InputLanguage;
this.ChangeIme(e.InputLanguage.Handle);

}
/// <summary>
/// 改变输入法函数
/// </summary>
/// <param name="handle"></param>
private void ChangeIme(IntPtr handle)
{
this._CurrentImeHandleStr = handle.ToString();

//改变输入法的状态
foreach (ToolStripMenuItem item in this.contextMenuStrip1.Items)
{
if (((InputLanguage)item.Tag).Handle.ToString() == handle.ToString())
{
item.CheckState = CheckState.Checked;
}
else
{
item.CheckState = CheckState.Unchecked;
}
}
InputLanauageChangedUI();
}

/// <summary>
/// 输入法改变时界面的变化
/// </summary>
private void InputLanauageChangedUI()
{
//改变相应的图标
foreach (ToolStripMenuItem item in this.contextMenuStrip1.Items)
{
if (item.CheckState == CheckState.Checked)
{
this.ToolBtn.Image = item.Image;
this.ToolBtn.ToolTipText = item.Text;

}
}

//重新设置组件的大小
this.toolStrip1.Height = BTN_HEIGHT;

}

下载地址http://download.csdn.net/source/2819817

 

c# windows程序调用本地输入法的更多相关文章

  1. [AIR] AIR程序调用本地默认应用程序打开本地文件

    摘要:      File类提供了一个方法openWithDefaultApplication可以用本地默认应用程序打开指定路径下的文件. 当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: v ...

  2. AIR程序调用本地默认应用程序打开本地文件

    当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: var file:File = File.desktopDirectory.resolvePath("test.jpg") ...

  3. 自定义浏览器协议,实现web程序调用本地程序

    转自  http://blog.csdn.net/talking12391239/article/details/40712759 亲测可用 tencent://Message/?Uin=000000 ...

  4. Windows程序调用dll

    可以写在WndProc的WM_CREATE里面,不能写在WinMain里面

  5. 网页调用本地程序(Windows下浏览器全兼容)

    用网页调用本地应用程序的思路是,先进行注册表注册自定义一个URL Protocol协议,再利用URL Protocol实现网页调用本地应用程序. 1.先写一个注册表文件,将其保存为.reg后缀的注册表 ...

  6. ubuntu下浏览器调用本地应用程序

    ubunut下浏览器调用本地应用程序需要desktop文件和scheme协议的支持,和windows 的url protocol类似,只是注册协议的方式不一样. 首先是desktop文件,里面需要加入 ...

  7. html网页调用本地exe程序的实现方法:

    html网页调用本地exe程序的实现方法:1.新建注册表具体文件: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\hhtpexe] [ ...

  8. web调用本地exe应用程序并传入参数

    从网页中通过自定义URL Protocol调用本地程序,需要将协议写到注册表中.浏览器在解析到自定义URL Protocol之后,寻找注册表,通过注册表启动相应的程序并传入参数.协议里面需要记录本地程 ...

  9. PC网页js调用本地应用程序

    最近要现实一个在PC网页中实现点击按钮调用本地应用程序的功能 其实实现原理也非常简单, 首先注册一个本地注册表文件,指向本地应用程序路径 其次在网页中用js指向这个注册表文件,就可以实现网页调用本地应 ...

随机推荐

  1. href="javascript:;" href="javascript:void(0);" href="#"区别

    一.href="javascript:;" 这种用法不正确,这么用的话会出现浏览器访问"javascript:;"这个地址的现象: 二.href="j ...

  2. [Grid Layout] Use the minmax function to specify dynamically-sized tracks

    Using minmax() is an amazingly useful way to specify boundaries for the smallest and largest size a ...

  3. 学习JS的这些日子——十二月总结

    事实上非常想早就发表这篇十二月份的总结了,可是一直拖拖拉拉没有完毕.一直在想2015年都过去了,该不该再去 写这一篇2015年最后一个月的总结.还有就是2015年的年终总结能否够取代十二月的总结,后来 ...

  4. Redis学习笔记4-Redis配置具体解释

    在Redis中直接启动redis-server服务时, 採用的是默认的配置文件.採用redis-server   xxx.conf 这种方式能够依照指定的配置文件来执行Redis服务. 依照本Redi ...

  5. freemarker自己定义标签(一)

    freemarker自己定义标签 1.自己定义标签说明 宏变量存储模板片段能够被用作自己定义指令macro 2.演示样例说明 <html> <head> <meta ht ...

  6. hbase 配置高可用hmaster

    1.先停掉hbase bin/stop-hbase.sh 2.在hbase的conf目录下创建 backup-masters 添加hadoop003 3.分发 4.重新启动hbase并查看 bin/s ...

  7. Eclipse Che安装依赖

    java Java 用于运行Che的服务器和用于创建Plug-in包的SDK工具,所以需要安装Java Jdk 1.8 如果只是运行Che的话下载JRE就足够了,但是加入你需要从源代码编译的话你还需要 ...

  8. poj1639 Picnic Planning,K度限制生成树

    题意: 矮人虽小却喜欢乘坐巨大的轿车,车大到能够装下不管多少矮人.某天,N(N≤20)个矮人打算到野外聚餐.为了集中到聚餐地点,矮人A 要么开车到矮人B 家中,留下自己的轿车在矮人B 家,然后乘坐B ...

  9. 记录一下go web 文档

    https://github.com/astaxie/build-web-application-with-golang

  10. Spring实战5-基于Spring构建Web应用

    主要内容 将web请求映射到Spring控制器 绑定form参数 验证表单提交的参数 写在前面:关于Java Web,首先推荐一篇文章——写给java web一年左右工作经验的人,这篇文章的作者用精练 ...