博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#汉语转拼音,日文转假名
阅读量:6891 次
发布时间:2019-06-27

本文共 4454 字,大约阅读时间需要 14 分钟。

hot3.png

源码下载地址:

转换类

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Runtime.CompilerServices;/// /// Created By Jickie阿文/// public class MSIMEProvider{    private bool FInitialized = false;    private const int S_OK = 0;    private const int CLSCTX_LOCAL_SERVER = 4;    private const int CLSCTX_INPROC_SERVER = 1;    private const int CLSCTX_INPROC_HANDLER = 2;    private const int CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER;    private const int FELANG_REQ_REV = 0x00030000;    private const int FELANG_CMODE_PINYIN = 0x00000100;    private const int FELANG_CMODE_NOINVISIBLECHAR = 0x40000000;    [DllImport("ole32.dll")]    private static extern int CLSIDFromString([MarshalAs(UnmanagedType.LPWStr)] string lpsz, out Guid pclsid);    [DllImport("ole32.dll")]    private static extern int CoCreateInstance([MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,        IntPtr pUnkOuter, uint dwClsContext, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IntPtr rpv);    [DllImport("ole32.dll")]    private static extern int CoInitialize(IntPtr pvReserved);    [DllImport("ole32.dll")]    private static extern int CoUninitialize();    //-------------------------------------------------------------------------------------    // Constructor    public MSIMEProvider()    {        int res = CoInitialize(IntPtr.Zero);        if (res == S_OK)            FInitialized = true;    }    public void Dispose()    {        if (FInitialized)        {            CoUninitialize();            FInitialized = false;        }    }    // Destructor    ~MSIMEProvider()    {        if (FInitialized)            CoUninitialize();    }    public string GetReadString(string value, MSIME imetype = MSIME.China)    {        if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))        {            return value;        }        string readString = String.Empty;        Guid pclsid;        int res;        //  Get CLSID's pointer from text's CLSID        res = CLSIDFromString(imetype.GetName(), out pclsid);        if (res != S_OK)        {            this.Dispose();            return readString;        }        Guid riid = new Guid("019F7152-E6DB-11D0-83C3-00C04FDDB82E ");        IntPtr ppv;        res = CoCreateInstance(pclsid, IntPtr.Zero, CLSCTX_SERVER, riid, out ppv);        if (res != S_OK)        {            this.Dispose();            return readString;        }        IFELanguage language = Marshal.GetTypedObjectForIUnknown(ppv, typeof(IFELanguage)) as IFELanguage;        res = language.Open();        if (res != S_OK)        {            this.Dispose();            return readString;        }        IntPtr result;        res = language.GetJMorphResult(FELANG_REQ_REV, FELANG_CMODE_PINYIN | FELANG_CMODE_NOINVISIBLECHAR,                value.Length, value, IntPtr.Zero, out result);        if (res != S_OK)        {            this.Dispose();            return readString;        }        readString = Marshal.PtrToStringUni(Marshal.ReadIntPtr(result, 4), Marshal.ReadInt16(result, 8));        language.Close();        return readString;    }} // end of ImeLanguage classpublic enum MSIME{    China,    Taiwan,    Japan,    Korea}public static class Extensions{    public static string GetName(this MSIME value)    {        return string.Format("{0}.{1}", typeof(MSIME).Name, Enum.GetName(typeof(MSIME), value));    }}//**************************************************************************************// IFELanguage Interface//**************************************************************************************[ComImport][Guid("019F7152-E6DB-11D0-83C3-00C04FDDB82E")][InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]internal interface IFELanguage{    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]    int Open();    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]    int Close();    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]    int GetJMorphResult(uint dwRequest, uint dwCMode, int cwchInput,        [MarshalAs(UnmanagedType.LPWStr)] string pwchInput, IntPtr pfCInfo, out IntPtr ppResult);} // end of IFELanguage Interface

使用方法

 MSIMEProvider IMEProvider = new MSIMEProvider(); this.tbReadString.Text = IMEProvider.GetReadString(this.txtInput.Text, MSIME.China);

转载于:https://my.oschina.net/jickie/blog/227441

你可能感兴趣的文章
webpack 持久化缓存
查看>>
WebRTC 入门教程(二)|WebRTC信令控制与STUN/TURN服务器搭建
查看>>
前端成长DAY.1 Html+CSS
查看>>
mysql rownum in hibernate
查看>>
Redux源码完全解读
查看>>
wordpress主题制作教程(12):单页面模板page.php
查看>>
Android 使用RecyclerView实现轮播图
查看>>
表单的checkbox switch开关设计
查看>>
centos7用yum安装nodejs
查看>>
华为2013内部推荐岗位,欢迎符合条件的童鞋投递
查看>>
JDBC Item5: 数据库连接池
查看>>
我的友情链接
查看>>
offsetleft
查看>>
创新B2B行业网站广告模式,增加客户效果
查看>>
11g Release 1 (11.1.0.7) Patch Set 1 for Microsoft
查看>>
我的友情链接
查看>>
纵观金笛的全球邮件收发保证
查看>>
关于dubbo服务的xml配置文件报错的问题
查看>>
实时计算无线数据分析
查看>>
Java Web应用中的任务调度
查看>>