码迷,mamicode.com
首页 > 其他好文 > 详细

获取系统当前的输入法列表并通过编程方式切换输入法

时间:2016-05-14 00:48:53      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

1.获取输入法列表
    //保存输入法信息的结构
    struct _tagInputMethod
    {
     char szLanguageId[30];
     char szLanguageName[100];
     char szLanguageImeFileName[100];
     HICON hIcon;
    };

    _tagInputMethod m_InputMethodList[30];
    int m_InputMethodCount;

    //获取输入法列表
    HKEY hKey,hKey1;
    DWORD cp=16;
    char LanguageId[15];
    CString szID;
    CString szKeyName,szKeyName1;
    szKeyName = "Keyboard Layout\\Preload";
    szKeyName1 = "System\\CurrentControlSet\\Control\\Keyboard Layouts\\"; 
    int i=0;
    szID.Format("%d",i+1); 
    DWORD lpT=REG_SZ;
    if(::RegOpenKey(HKEY_CURRENT_USER,szKeyName,&hKey)==ERROR_SUCCESS )
    {
        while(::RegQueryValueEx(hKey,szID,NULL,&lpT,(LPBYTE)LanguageId,&cp) == ERROR_SUCCESS )
        {
            CString szTempName;
            szTempName = szKeyName1 + (LPCTSTR)(LPTSTR)LanguageId;
            if(RegOpenKey(HKEY_LOCAL_MACHINE,szTempName,&hKey1)==ERROR_SUCCESS )
            {
                char szLanguageName[100];
                memset(szLanguageName,0,100);
 
                char szImeFileName[100];
                memset(szImeFileName,0,100);
 
                char szImeFileName_Layout[100];
                memset(szImeFileName_Layout,0,100);
 
                char Temp[100];
                memset(Temp,0,100);
 
                DWORD lpSLen=100;          
                if(RegQueryValueEx(hKey1,"Layout text",NULL,&lpT,(LPBYTE)szLanguageName,&lpSLen)==ERROR_SUCCESS)
                {
                    strcpy(m_InputMethodList[i].szLanguageId,LanguageId);
                    strcpy(m_InputMethodList[i].szLanguageName,szLanguageName);                
                }
 
                //ImeFileName
                lpSLen=100;
                RegQueryValueEx(hKey1,"IME file",NULL,&lpT,(LPBYTE)szImeFileName,&lpSLen);
                if (strlen(szImeFileName)>0)
                {
                    strcpy(Temp,szImeFileName);
                }
                else
                {
                    lpSLen=100;
                    RegQueryValueEx(hKey1,"Layout File",NULL,&lpT,(LPBYTE)szImeFileName_Layout,&lpSLen);
                    if (strlen(szImeFileName_Layout)>0)
                    {
                        strcpy(Temp,szImeFileName_Layout);
                    }
                }
                strcpy(m_InputMethodList[i].szLanguageImeFileName,Temp);
 
                //IME的文件里取出16*16图标
                HICON hSmallImeIcon[1];
                int _ImeIconCount=ExtractIconEx(Temp,0,NULL,hSmallImeIcon,1);
                if (_ImeIconCount==0)
                {
                    hSmallImeIcon[0]=LoadIcon(AfxGetApp()->m_hInstance,(LPCTSTR)IDI_UnKnowInputMethodIcon);
                }
                m_InputMethodList[i].hIcon=hSmallImeIcon[0];
            }  
            ::RegCloseKey(hKey1);
            i++;
            szID.Format("%d",i+1);
            m_InputMethodCount=i;
        }
    }
    ::RegCloseKey(hKey);
2.通过编程方式切换输入法

     HKL hkl=LoadKeyboardLayout(szLanguageId,KLF_ACTIVATE);
     ::PostMessage(::GetForegroundWindow(),WM_INPUTLANGCHANGEREQUEST,TRUE,(LPARAM)hkl);

获取系统当前的输入法列表并通过编程方式切换输入法

标签:

原文地址:http://www.cnblogs.com/dgx/p/5491507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!