设为首页 收藏本站
查看: 1989|回复: 0

使用TSF在Vista、Win7下精确切换输入法

[复制链接]

尚未签到

发表于 2015-5-18 11:53:58 | 显示全部楼层 |阅读模式
  --本文非原创,信息来自网上,作者不明


DSC0000.gif DSC0001.gif 代码

///////////////////////////////////////////////////////////////////////////////////////////////
//               Microsoft Text Service Framework Declaration
//                        from C++ header file
//
//////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Collections.Generic;
namespace TSF
{
        [StructLayout( LayoutKind.Sequential )]
        internal struct TF_LANGUAGEPROFILE
        {
                internal Guid clsid;
                internal short langid;
                internal Guid catid;
                [MarshalAs( UnmanagedType.Bool )]
                internal bool fActive;
                internal Guid guidProfile;
        }

        [ComImport, SecurityCritical, SuppressUnmanagedCodeSecurity,
    Guid( "1F02B6C5-7842-4EE6-8A0B-9A24183A95CA" ),
         InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
        internal interface ITfInputProcessorProfiles
        {
                [SecurityCritical]
                void Register( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                void Unregister( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                void AddLanguageProfile( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                void RemoveLanguageProfile( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                void EnumInputProcessorInfo( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                int GetDefaultLanguageProfile( short langid, ref Guid catid, out Guid clsid, out Guid profile );
                [SecurityCritical]
                void SetDefaultLanguageProfile( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                int ActivateLanguageProfile( ref Guid clsid, short langid, ref Guid guidProfile );
                [PreserveSig, SecurityCritical]
                int GetActiveLanguageProfile( ref Guid clsid, out short langid, out Guid profile );
                [SecurityCritical]
                int GetLanguageProfileDescription( ref Guid clsid, short langid, ref Guid profile, out IntPtr desc );
                [SecurityCritical]
                void GetCurrentLanguage( out short langid ); //non-implement!!  may be is wrong declaration.
                [PreserveSig, SecurityCritical]
                int ChangeCurrentLanguage( short langid ); //non-implement!!  may be is wrong declaration.
                [PreserveSig, SecurityCritical]
                int GetLanguageList( out IntPtr langids, out int count );
                [SecurityCritical]
                int EnumLanguageProfiles( short langid, out IEnumTfLanguageProfiles enumIPP );
                [SecurityCritical]
                int EnableLanguageProfile( );
                [SecurityCritical]
                int IsEnabledLanguageProfile( ref Guid clsid, short langid, ref Guid profile, out bool enabled );
                [SecurityCritical]
                void EnableLanguageProfileByDefault( ); //non-implement!!  may be is wrong declaration.
                [SecurityCritical]
                void SubstituteKeyboardLayout( ); //non-implement!!  may be is wrong declaration.
        }

        [ComImport, InterfaceType( ComInterfaceType.InterfaceIsIUnknown ),
             Guid( "3d61bf11-ac5f-42c8-a4cb-931bcc28c744" )]
        internal interface IEnumTfLanguageProfiles
        {
                void Clone( out IEnumTfLanguageProfiles enumIPP );
                [PreserveSig]
                int Next( int count, [Out, MarshalAs( UnmanagedType.LPArray, SizeParamIndex = 2 )]
                             TF_LANGUAGEPROFILE[ ] profiles, out int fetched );
                void Reset( );
                void Skip( int count );
        }

        internal static class TSF_NativeAPI
        {
                public static readonly Guid GUID_TFCAT_TIP_KEYBOARD;

                static TSF_NativeAPI( )
                {
                        GUID_TFCAT_TIP_KEYBOARD = new Guid( 0x34745c63, 0xb2f0,
                                   0x4784, 0x8b, 0x67, 0x5e, 0x12, 200, 0x70, 0x1a, 0x31 );
                }

                [SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport( "msctf.dll" )]
                public static extern int TF_CreateInputProcessorProfiles( out ITfInputProcessorProfiles profiles );
        }

        public class TSFWrapper
        {
                public static short[ ] GetLangIDs( )
                {
                        List langIDs = new List( );
                        ITfInputProcessorProfiles profiles;
                        if ( TSF_NativeAPI.TF_CreateInputProcessorProfiles( out profiles ) == 0 )
                        {
                                IntPtr langPtrs;
                                int fetchCount = 0;
                                if ( profiles.GetLanguageList( out langPtrs, out fetchCount ) == 0 )
                                {
                                        for ( int i = 0; i < fetchCount; i++ )
                                        {
                                                short id = Marshal.ReadInt16( langPtrs, sizeof( short ) * i );
                                                langIDs.Add( id );
                                        }
                                }
                                Marshal.ReleaseComObject( profiles );
                        }
                        return langIDs.ToArray( );
                }

                public static string[ ] GetInputMethodList( short langID )
                {
                        List imeList = new List( );
                        ITfInputProcessorProfiles profiles;
                        if ( TSF_NativeAPI.TF_CreateInputProcessorProfiles( out profiles ) == 0 )
                        {
                                try
                                {
                                        IEnumTfLanguageProfiles enumerator = null;
                                        if ( profiles.EnumLanguageProfiles( langID, out enumerator ) == 0 )
                                        {
                                                if ( enumerator != null )
                                                {
                                                        TF_LANGUAGEPROFILE[ ] langProfile = new TF_LANGUAGEPROFILE[ 1 ];
                                                        int fetchCount = 0;
                                                        while ( enumerator.Next( 1, langProfile, out fetchCount ) == 0 )
                                                        {
                                                                IntPtr ptr;
                                                                if ( profiles.GetLanguageProfileDescription( ref langProfile[ 0 ].clsid, langProfile[ 0 ].langid, ref langProfile[ 0 ].guidProfile, out ptr ) == 0 )
                                                                {
                                                                        bool enabled;
                                                                        if ( profiles.IsEnabledLanguageProfile( ref langProfile[ 0 ].clsid,
                                                                        langProfile[ 0 ].langid, ref langProfile[ 0 ].guidProfile, out enabled ) == 0 )
                                                                        {
                                                                                if ( enabled )
                                                                                        imeList.Add( Marshal.PtrToStringBSTR( ptr ) );
                                                                        }
                                                                }
                                                                Marshal.FreeCoTaskMem( ptr );
                                                        }
                                                }
                                        }
                                }
                                finally
                                {
                                        Marshal.ReleaseComObject( profiles );
                                }
                        }
                        return imeList.ToArray( );
                }

                public static bool ActiveInputMethodWithDesc( short langID, string desc )
                {
                        ITfInputProcessorProfiles profiles;
                        if ( TSF_NativeAPI.TF_CreateInputProcessorProfiles( out profiles ) == 0 )
                        {
                                try
                                {
                                        IEnumTfLanguageProfiles enumerator = null;
                                        if ( profiles.EnumLanguageProfiles( langID, out enumerator ) == 0 )
                                        {
                                                if ( enumerator != null )
                                                {
                                                        TF_LANGUAGEPROFILE[ ] langProfile = new TF_LANGUAGEPROFILE[ 1 ];
                                                        int fetchCount = 0;
                                                        while ( enumerator.Next( 1, langProfile, out fetchCount ) == 0 )
                                                        {
                                                                IntPtr ptr;
                                                                if ( profiles.GetLanguageProfileDescription( ref langProfile[ 0 ].clsid,
                                                                   langProfile[ 0 ].langid, ref langProfile[ 0 ].guidProfile, out ptr ) == 0 )
                                                                {
                                                                        bool enabled;
                                                                        if ( profiles.IsEnabledLanguageProfile( ref langProfile[ 0 ].clsid,
                                                                            langProfile[ 0 ].langid, ref langProfile[ 0 ].guidProfile, out enabled ) == 0 )
                                                                        {
                                                                                if ( enabled )
                                                                                {
                                                                                        string s = Marshal.PtrToStringBSTR( ptr );
                                                                                        if ( s.Equals( desc ) )
                                                                                                return profiles.ActivateLanguageProfile( ref langProfile[ 0 ].clsid,
                                                                                            langProfile[ 0 ].langid, ref langProfile[ 0 ].guidProfile ) == 0;
                                                                                }
                                                                        }
                                                                        Marshal.FreeCoTaskMem( ptr );
                                                                }
                                                        }
                                                }
                                        }
                                }
                                finally
                                {
                                        Marshal.ReleaseComObject( profiles );
                                }
                        }
                        return false;
                }

                public static bool DeActiveInputMethod( short langID )
                {
                        ITfInputProcessorProfiles profiles;
                        if ( TSF_NativeAPI.TF_CreateInputProcessorProfiles( out profiles ) == 0 )
                        {
                                try
                                {
                                        Guid clsid = Guid.Empty;
                                        return profiles.ActivateLanguageProfile( ref clsid, langID, ref clsid ) == 0;
                                }
                                finally
                                {
                                        Marshal.ReleaseComObject( profiles );
                                }
                        }
                        return false;
                }

        }
}


  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-68042-1-1.html 上篇帖子: [转]Win7独立语言包下载 下篇帖子: win7旗舰版系统设置自动锁屏问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表