CIMESoftKbd.cpp
上传用户:hyb6888
上传日期:2016-01-24
资源大小:5186k
文件大小:9k
源码类别:

输入法编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "CIMESoftKbd.h"
  4. CHAR SKLayoutName[NumsSK][11] = {
  5.     "P C 键盘", "希腊字母",
  6. "俄文字母", "注音符号",
  7. "拼    音", "日文平假名",
  8. "日文片假名","标点符号",
  9. "数字序号", "数学符号",
  10. "单位符号", "制 表 符",
  11. "特殊符号",
  12. };
  13. CHAR SKLayout[NumsSK][48*2+1] = {
  14.     "  0123456789abcdefghijklmnopqrstuvwxyz;=,-./`[\]'",
  15. "            κχυμγνξοθπρ ωψι αδλεηφβτζσ           ",
  16. "                      лъшнвопржстуьызиагмдёщбчецф э юя й кх",
  17. "  ㄦㄉ  ㄓ  ㄚㄞㄢㄇㄖㄏㄎㄍㄑㄕㄘㄛㄨㄜㄠㄩㄙㄟㄣㄆㄐㄋㄔㄧㄒㄊ ㄗㄈㄤ ㄝ ㄡㄥㄅ        ",
  18. "            ē ǔěǎè īǒíǐìǘǖò āàé óùáúōū  ǚ ǜü ê   ",
  19. "   ぃぅぇぉかきくけこなもむぬすねのはつひふへゅゃってさせにそちめしみたまほ ょんゎをぁと  ゐゑ",
  20. "  ケィゥヴェォカヵキクナモムヌスネノハツヒフヘュャッテサセニソチメシミタマホコョヶヮヲァトンヰヱ",
  21. "  ˉ,、;:?!…—·〔(【〈“〉《》∶「」『[)"'‘”〕々‖】’〗~〖』¨]ˇ{}。`〃|.",
  22. "  ⅪⅡⅢⅣⅤⅥⅦⅧⅨⅩ㈠⑸⑶㈢⒊㈣㈤㈥⒏㈦㈧㈨⑺⑹⒐⒑⒈⒋㈡⒌⒎⑷⒉⑵⒍⑴㈩  ⑻Ⅻ⑼⑽Ⅰ    ",
  23. "   ≡≠=≤≥<>≮≯∧⊙∠∑-∏∪∩∫∈ ∵∽≌∮∝±×∨÷ ⌒+∥/⊥∴  ∷√ ≈∞   ",
  24. "  ¤°′″$£¥‰%℃百 毫万二亿兆吉七太拍艾  八九○三千四六微一厘五分   ¢   十   ",
  25. "  ┄┍┎┏┐┑┒┓ ─┬╀┾┮┞┯┰┱┣┲┳ ╂╁ │├┟┭┠┢┿┝┽┡┼  ╃┈  ┌┆ ┊ ",
  26. "            ■^@▲☆※→←◇↑↓〓 ̄_◆□§★△○◎\№&●#           ",
  27.   "                                                 ",
  28.    };
  29. CHAR SKLayoutS[NumsSK][48*2+1] = {
  30. "  ) ! @ # $ % ^ & * ( A B C D E F G H I J K L M N O P Q R S T U V W X Y Z : + < _ > ? ~ { | } " ",
  31. "            ΚΧΥΜΓΝΞΟΘΠΡ  ΩΨΙ  ΑΔΛΕΗΦΒΤΖΣ                      ",
  32. "            ЛЪШНВОПРЖСТУЬЫЗИАГМДЁЩБЧЕЦФ  Э  ЮЯ  Й  КХ",
  33. "                                                 ",
  34. "                                                 ",
  35. "   いうえおがぎぐげごぱろるぷずぺぽぼづびぶべゆや でざぜぴぞぢれじりだらぼ よ わ あど   ",
  36. "  ゲイウ エオガ ギグパロルプズペポバヅビブベユヤ デザゼピゾヂレジリダラボゴヨ ワ アド   ",
  37. "                                                 ",
  38. "            ①⒂⒀③⒔④⑤⑥⒙⑦⑧⑨⒄⒃⒚⒛⒒⒕②⒖⒘⒁⒓⑿⒗⑾⑩ ⒅ ⒆⒇     ",
  39. "                                                 ",
  40. "                      佰   贰   柒     捌玖零叁仟肆陆 壹 伍        拾   ",
  41. "  ┅┕┖┗┘┙┚┛ ━┴╈╆┶┦┷┸┹┫┺┻ ╊╉ ┃┤┧┵┨┪╇┥╅┩╄  ╋┉  └┇ ┋ ",
  42. "                                                 ",
  43.   "                                                 ",
  44. };
  45. BYTE VirtKey48Map[48] = {
  46. 0x20, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  47. 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  48. 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  49. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xba, 0xbb, 0xbc,
  50. 0xbd, 0xbe, 0xbf, 0xc0, 0xdb, 0xdc, 0xdd, 0xde
  51. };
  52. CIMESoftKbd::CIMESoftKbd()
  53. {
  54. CurentKbdNum=0;
  55. showflage=0;
  56. mSelID=0;
  57. CIMEhSoftKbdWnd=NULL;
  58. Curhimc=NULL;
  59. savhimc=NULL;
  60. }
  61. HWND CIMESoftKbd::initalize(DWORD himc)
  62. {
  63. LPINPUTCONTEXT lpIMC;
  64. savhimc=himc;
  65. if (!(lpIMC = ImmLockIMC(himc)))//锁存数据
  66.    return 0;
  67. CIMEhSoftKbdWnd=CreateSoftKbd(lpIMC->hWnd);
  68. ImmUnlockIMC(himc);
  69. return CIMEhSoftKbdWnd;
  70. }
  71. HWND CIMESoftKbd:: CreateSoftKbd(HWND hUIWnd)
  72. {
  73. static int flage=0;
  74. POINT pt;
  75. HWND LhSoftKbd;
  76. RECT FullWin;
  77. SystemParametersInfo(SPI_GETWORKAREA,0, &FullWin,0);
  78. pt.y=FullWin.bottom-140;
  79. pt.x=FullWin.right-360;//使窗口在右下角显示
  80. //LhSoftKbd=ImmCreateSoftKeyboard(SOFTKEYBOARD_TYPE_C1,hUIWnd,pt.x,pt.y);
  81. LhSoftKbd=ImmCreateSoftKeyboard(SOFTKEYBOARD_TYPE_C1,0,pt.x,pt.y);//切断与主窗口的联系,防止释放软键盘时出错。
  82. if(LhSoftKbd!=NULL)
  83. ImmShowSoftKeyboard(LhSoftKbd,SW_HIDE);//SW_HIDE
  84.     return LhSoftKbd;
  85. }
  86. CIMESoftKbd::~CIMESoftKbd( )
  87. {
  88. if(CIMEhSoftKbdWnd!=NULL){
  89. LPINPUTCONTEXT lpIMC;
  90. if (!(lpIMC = ImmLockIMC(Curhimc)))//锁存数据
  91. {
  92. return ;
  93. }
  94.   // char ss[100];sprintf(ss,"dd:%d ,%d",CIMEhSoftKbdWnd,lpIMC);
  95. //MessageBox(0,ss,0,0);
  96. //ImmShowSoftKeyboard(CIMEhSoftKbdWnd,SW_SHOW);//SW_HIDE
  97. ImmDestroySoftKeyboard(CIMEhSoftKbdWnd);
  98. ImmUnlockIMC(Curhimc);
  99. }
  100. //MessageBox(0,ss,0,0);
  101. //CIMEhSoftKbdWnd=NULL;
  102. }
  103. /* SetSoftKbdData()                                                   */
  104. /**********************************************************************/
  105. void CIMESoftKbd::SetSoftKbdData(int NUM)
  106. {
  107.     int   i;
  108.     LPSOFTKBDDATA lpSoftKbdData;
  109.     HGLOBAL hsSoftKbdData;
  110. char ss[500];
  111. if(CIMEhSoftKbdWnd==NULL)
  112. return;
  113. CurentKbdNum=NUM;
  114.     hsSoftKbdData = GlobalAlloc(GHND, sizeof(SOFTKBDDATA) * 2);
  115.     if (!hsSoftKbdData) 
  116.         return;
  117.     lpSoftKbdData = (LPSOFTKBDDATA)GlobalLock(hsSoftKbdData);
  118.     if (!lpSoftKbdData) 
  119. return;
  120.     lpSoftKbdData->uCount = 2;
  121.     for (i = 0; i < 48; i++) {
  122.         BYTE bVirtKey;
  123.         bVirtKey = VirtKey48Map[i];
  124.         if (!bVirtKey) 
  125.             continue;
  126. {
  127. WORD CHIByte, CLOByte;
  128. CHIByte = SKLayout[NUM][i*2] & 0x00ff;
  129. CLOByte = SKLayout[NUM][i*2 + 1] & 0x00ff;
  130. lpSoftKbdData->wCode[0][bVirtKey] = (CHIByte << 8) | CLOByte;
  131. CHIByte = SKLayoutS[NUM][i*2] & 0x00ff;
  132. CLOByte = SKLayoutS[NUM][i*2 + 1] & 0x00ff;
  133. lpSoftKbdData->wCode[1][bVirtKey] = (CHIByte << 8) | CLOByte;
  134. }
  135. }
  136.     SendMessage(CIMEhSoftKbdWnd, WM_IME_CONTROL, IMC_SETSOFTKBDDATA, (LPARAM)lpSoftKbdData);
  137.     sprintf(ss,"%s%s",SKLayout[NUM],SKLayoutS[NUM]);
  138. SetWindowText(CIMEhSoftKbdWnd,ss);
  139.     GlobalUnlock(hsSoftKbdData);
  140.     GlobalFree(hsSoftKbdData);
  141.     return;
  142. }
  143. void CIMESoftKbd::ShowSoftKbd()
  144. {
  145. char wintxt[500]="";
  146. try{
  147. if(CIMEhSoftKbdWnd==NULL)
  148. {
  149. LPINPUTCONTEXT lpIMC;
  150. if (!(lpIMC = ImmLockIMC(Curhimc)))//锁存数据
  151. {
  152. sprintf(wintxt,"%d",Curhimc);
  153. MessageBox(0,"CIMEhSoftKbdWnd=NULL",wintxt,0);
  154. return ;
  155. }
  156. CIMEhSoftKbdWnd=CreateSoftKbd(lpIMC->hWnd);
  157. ImmUnlockIMC(Curhimc);
  158. }
  159. if(CIMEhSoftKbdWnd==NULL)
  160. return;
  161. if(!IsWindow(CIMEhSoftKbdWnd))
  162.     {
  163. char ss[100];
  164. sprintf(ss,"%x ",*CIMEhSoftKbdWnd);
  165. MessageBox(0,"CIMEhSoftKbdWnd NoWind",ss,0);
  166. return;
  167. }
  168. GetWindowText(CIMEhSoftKbdWnd,wintxt,10);
  169. if(wintxt[0]==0)
  170. {
  171. showflage=1;
  172. SetSoftKbdData(CurentKbdNum);
  173. ImmShowSoftKeyboard(CIMEhSoftKbdWnd,SW_SHOWNOACTIVATE);
  174. }
  175. else
  176. {
  177. showflage=0;
  178. ImmShowSoftKeyboard(CIMEhSoftKbdWnd,SW_HIDE);
  179. SetWindowText(CIMEhSoftKbdWnd,"");
  180. }
  181.    } catch (...) {
  182. MessageBox(0,"CIMEhSoftKbdWnd","有错误发生",0);
  183. }
  184. }
  185. void CIMESoftKbd::ShowNextSoftKbd()
  186. {
  187. if(CIMEhSoftKbdWnd==NULL)
  188. return;
  189. CurentKbdNum++;
  190. if(showflage==1)
  191. {
  192. SetSoftKbdData(CurentKbdNum);
  193. ImmShowSoftKeyboard(CIMEhSoftKbdWnd,SW_SHOWNOACTIVATE);
  194. }
  195. }
  196.  long CIMESoftKbd::SelID()
  197.  {
  198. return  mSelID;
  199.  }
  200. //返回所选菜单项
  201. long  CIMESoftKbd::Popup()
  202. {
  203.   long iMenu,  nMenus,result;
  204.   POINT p ;
  205.   //char ss[100];
  206.   char buffer [256]="";
  207.   long flag=0;
  208.   HMENU hMenu;
  209.     GetCursorPos(&p);
  210.     hMenu = CreatePopupMenu();
  211.     nMenus = 13;
  212.     for(iMenu = 0 ;iMenu<nMenus;iMenu++)
  213. {
  214.    flag=0;
  215.    if(CurentKbdNum==iMenu)
  216.    flag|=MF_CHECKED;
  217.    if(iMenu==7)
  218.            flag|=MF_ENABLED|MF_MENUBREAK|MF_MENUBARBREAK;
  219.        else
  220.    flag|=MF_ENABLED;
  221.    
  222.    AppendMenu (hMenu, flag, iMenu+1, SKLayoutName[iMenu]);
  223.    if(iMenu!=6)
  224.   AppendMenu (hMenu, MF_SEPARATOR, iMenu, "");
  225.     }
  226.     iMenu = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON + TPM_LEFTALIGN + TPM_NONOTIFY + TPM_RETURNCMD, p.x, p.y, 0, GetForegroundWindow(), 0);
  227.     //得到菜单的选择字符串
  228.     result = GetMenuString(hMenu, (iMenu - 1), buffer, strlen(buffer), MF_BYPOSITION);
  229.     
  230.     DestroyMenu(hMenu);
  231. if(iMenu>0)
  232. {
  233. ImmShowSoftKeyboard(CIMEhSoftKbdWnd,SW_HIDE);
  234. SetSoftKbdData(13);
  235. SetSoftKbdData(iMenu-1);
  236. ImmShowSoftKeyboard(CIMEhSoftKbdWnd,SW_SHOWNOACTIVATE);
  237. }
  238.     mSelID = iMenu;
  239. return  mSelID;
  240. }