uicand.c
上传用户:shdz666
上传日期:2007-01-03
资源大小:566k
文件大小:9k
源码类别:

输入法编程

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 1999.4  Li ZhenChun
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License; or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that is will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of 
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 675 Mass Ave, Cambridge, M A 02139, USA.
  17.  *
  18.  * Author: Li ZhenChun  email: zhchli@163.net or zhchli@126.com
  19.  * 
  20.  */
  21. #include "freepy.h"
  22. LRESULT WINAPI CandWndProc( 
  23. HWND hWnd,
  24. UINT message,
  25. WPARAM wParam,
  26. LPARAM lParam)
  27. {
  28.     switch (message)
  29.     {
  30.         case WM_PAINT:
  31.             PaintCandWindow(hWnd);
  32.             break;
  33.         case WM_SETCURSOR:
  34.         case WM_MOUSEMOVE:
  35.         case WM_LBUTTONUP:
  36.         case WM_RBUTTONUP:
  37.             DragUI(hWnd,NULL,message,wParam,lParam,FALSE);
  38.             if ((message == WM_SETCURSOR) &&
  39.                 (HIWORD(lParam) != WM_LBUTTONDOWN) &&
  40.                 (HIWORD(lParam) != WM_RBUTTONDOWN)) 
  41.                     return DefWindowProc(hWnd,message,wParam,lParam);
  42.             if ((message == WM_LBUTTONUP) || (message == WM_RBUTTONUP))
  43.                 SetWindowLong(hWnd,FIGWL_MOUSE,0L);
  44.             break;
  45.         default:
  46.             if (!MyIsIMEMessage(message)){
  47.                 return DefWindowProc(hWnd,message,wParam,lParam);
  48. }
  49.             break;
  50.     }
  51.     return 0L;
  52. }
  53. void CreateCandWindow( HWND hUIWnd,LPUIEXTRA lpUIExtra)
  54. {
  55.     if (!IsWindow(lpUIExtra->uiCand.hWnd))
  56.     {
  57. HDC hDC;
  58. TCHAR szStr[100];
  59.         lpUIExtra->uiCand.hWnd = 
  60.                 CreateWindowEx(WS_EX_WINDOWEDGE,
  61.                              CANDCLASSNAME,NULL,
  62.                              WS_DISABLED | WS_POPUP | WS_DLGFRAME,
  63.                              0,
  64.                              0,
  65.                              1,
  66.  1,
  67.                              hUIWnd,NULL,hInst,NULL);
  68. SetWindowLong(lpUIExtra->uiCand.hWnd,FIGWL_SVRWND,(DWORD)hUIWnd);
  69. hDC = GetDC(lpUIExtra->uiCand.hWnd);
  70. _stprintf(szStr,_T("AAAAAAAAAAAAA"));
  71. GetTextExtentPoint(hDC,szStr,_tcslen(szStr),sizeCand);
  72. _stprintf(szStr,_T("<< 1啊 2啊 3啊 4啊 5啊 6啊 7啊 8啊 9啊 0啊 >>"));
  73. GetTextExtentPoint(hDC,szStr,_tcslen(szStr),sizeCand+1);
  74. ReleaseDC(lpUIExtra->uiCand.hWnd,hDC);
  75. sizeCand[0].cy *= 11;
  76. sizeCand[1].cy *= 2;
  77. sizeCand[1].cy -= 4;
  78. // if(wConversionSet & CONVERSION_SET_SHAPE) {
  79. // lpUIExtra->uiCand.sz.cx = sizeCand[1].cx;
  80. // lpUIExtra->uiCand.sz.cy = sizeCand[1].cy;
  81. // }
  82. // else {
  83. lpUIExtra->uiCand.sz.cx = sizeCand[0].cx;
  84. lpUIExtra->uiCand.sz.cy = sizeCand[0].cy;
  85. // }
  86.     }
  87.     ShowWindow(lpUIExtra->uiCand.hWnd, SW_HIDE);
  88.     return;
  89. }
  90. BOOL GetCandPosFromCompWnd(LPUIEXTRA lpUIExtra,LPSIZE lpsz)
  91. {
  92. if (IsWindow(lpUIExtra->uiComp.hWnd))
  93.     {
  94. RECT rc,screenrc;
  95. POINT pt;
  96. GetWindowRect(lpUIExtra->uiComp.hWnd,&rc);
  97. if(wConversionSet & CONVERSION_SET_SHAPE) {
  98. pt.x = rc.left;
  99. pt.y = rc.bottom;
  100. }
  101. else {
  102. pt.x = rc.right + 5;
  103. pt.y = rc.top;
  104. }
  105. SystemParametersInfo(SPI_GETWORKAREA,
  106. 0,
  107. &screenrc,
  108. 0);
  109. if(wConversionSet & CONVERSION_SET_SHAPE) {
  110. if( (pt.x + sizeCand[1].cx) > screenrc.right)
  111. pt.x = screenrc.right - sizeCand[1].cx;
  112. if( (pt.y + sizeCand[1].cy) > screenrc.bottom)
  113. pt.y = rc.top - sizeCand[1].cy;
  114. }
  115. else {
  116. if( (pt.x + lpsz->cx) > screenrc.right)
  117. pt.x = rc.left - lpsz->cx - 5;
  118. if( (pt.y + lpsz->cy) > screenrc.bottom)
  119. pt.y = screenrc.bottom - lpsz->cy;
  120. }
  121. lpUIExtra->uiCand.pt.x = pt.x;
  122. lpUIExtra->uiCand.pt.y = pt.y;
  123. return TRUE;
  124.     }
  125.     return FALSE;
  126. }
  127. void MoveCandWindow(HWND hUIWnd,LPUIEXTRA lpUIExtra, LPINPUTCONTEXT lpIMC)
  128. {
  129. if (!IsWindow(lpUIExtra->uiCand.hWnd))
  130. CreateCandWindow( hUIWnd, lpUIExtra);
  131. if (IsWindow(lpUIExtra->uiCand.hWnd))
  132. {
  133.     LPCANDIDATEINFO lpCandInfo;
  134.     LPCANDIDATELIST lpCandList;
  135. HDC hDC;
  136. SIZE sz;
  137. LPTSTR lpStr;
  138. sz.cx = 0;
  139. sz.cy = 0;
  140. if (lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo))
  141. {
  142. lpCandList = (LPCANDIDATELIST)((LPSTR)lpCandInfo  + lpCandInfo->dwOffset[0]);
  143. if( lpCandList->dwCount <= 0) {
  144. ShowWindow(lpUIExtra->uiCand.hWnd, SW_HIDE);
  145. ImmUnlockIMCC(lpIMC->hCandInfo);
  146. return;
  147. }
  148. lpStr = GETLPCANDSTR(lpCandList,1);
  149. hDC = GetDC(lpUIExtra->uiCand.hWnd);
  150. GetTextExtentPoint(hDC,lpStr,_tcslen(lpStr),&sz);
  151. ReleaseDC(lpUIExtra->uiCand.hWnd,hDC);
  152. if(_tcslen(lpStr)) sz.cx += 3*sz.cx/_tcslen(lpStr);
  153. ImmUnlockIMCC(lpIMC->hCandInfo);
  154. }
  155. if(wConversionSet & CONVERSION_SET_SHAPE) {
  156. sz.cx = sizeCand[1].cx;
  157. sz.cy = sizeCand[1].cy;
  158. }
  159. else{
  160. if(sz.cx < lpUIExtra->uiCand.sz.cx)
  161. sz.cx = lpUIExtra->uiCand.sz.cx;
  162. sz.cy = lpUIExtra->uiCand.sz.cy;
  163. }
  164. GetCandPosFromCompWnd(lpUIExtra,&sz);
  165. MoveWindow(lpUIExtra->uiCand.hWnd,
  166. lpUIExtra->uiCand.pt.x,
  167. lpUIExtra->uiCand.pt.y,
  168. sz.cx,
  169. sz.cy,
  170. TRUE);
  171. ShowWindow(lpUIExtra->uiCand.hWnd,SW_SHOWNOACTIVATE);
  172. InvalidateRect(lpUIExtra->uiCand.hWnd,NULL,FALSE);
  173. }
  174. }
  175. void PaintCandWindow( HWND hCandWnd)
  176. {
  177.     PAINTSTRUCT ps;
  178.     HIMC hIMC;
  179.     LPINPUTCONTEXT lpIMC;
  180.     LPCANDIDATEINFO lpCandInfo;
  181.     LPCANDIDATELIST lpCandList;
  182.     HDC hDC;
  183.     RECT rc;
  184.     LPTSTR lpstr;
  185. TCHAR szStr[100];
  186.     int height,width;
  187.     DWORD i;
  188.     SIZE sz;
  189.     HWND hSvrWnd;
  190. HBRUSH hBrush = (HBRUSH)NULL;
  191. HBRUSH hOldBrush = (HBRUSH)NULL;
  192. HPEN hPen = (HPEN)NULL;
  193. HPEN hOldPen = (HPEN)NULL;
  194.     hDC = BeginPaint(hCandWnd,&ps);
  195. GetClientRect(hCandWnd,&rc);
  196. hBrush = GetStockObject(LTGRAY_BRUSH);
  197. hOldBrush=SelectObject(hDC,hBrush);
  198. PatBlt(hDC,
  199. rc.left,
  200. rc.top ,
  201. rc.right,
  202. rc.bottom,
  203. PATCOPY);
  204. if(hBrush && hOldBrush)
  205. SelectObject(hDC,hOldBrush);
  206. hPen = GetStockObject(WHITE_PEN);
  207. hOldPen = SelectObject(hDC,hPen);
  208. MoveToEx(hDC,0,rc.bottom - GetSystemMetrics(SM_CXEDGE)/2,NULL);
  209. LineTo(hDC,rc.right-GetSystemMetrics(SM_CXEDGE)/2,rc.bottom - GetSystemMetrics(SM_CXEDGE)/2);
  210. LineTo(hDC,rc.right-GetSystemMetrics(SM_CXEDGE)/2,0);
  211. hPen = CreatePen(PS_SOLID ,0,RGB(128,128,128));
  212. SelectObject(hDC,hPen);
  213. MoveToEx(hDC,rc.right-GetSystemMetrics(SM_CXEDGE)/2,0,NULL);
  214. LineTo(hDC,0,0);
  215. LineTo(hDC,0,rc.bottom-GetSystemMetrics(SM_CYEDGE)/2);
  216. SelectObject(hDC,hOldPen);
  217. DeleteObject(hPen);
  218.     hSvrWnd = (HWND)GetWindowLong(hCandWnd,FIGWL_SVRWND);
  219.     if (hIMC = (HIMC)GetWindowLong(hSvrWnd,IMMGWL_IMC))
  220.     {
  221.         lpIMC = ImmLockIMC(hIMC);
  222.         if (lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo))
  223.         {
  224. DWORD dwMaxPaintPos;
  225. WORD  wCount;
  226.             height = 3;
  227. width = 1;
  228.             lpCandList = (LPCANDIDATELIST)((LPSTR)lpCandInfo  + lpCandInfo->dwOffset[0]);
  229. SetBkMode(hDC,TRANSPARENT);
  230. SetPaintColor(hDC, wConversionMode);
  231. if(wConversionSet & CONVERSION_SET_SHAPE) {
  232. lpstr = GETLPCANDSTR(lpCandList,0);
  233. TextOut(hDC,width,height,lpstr,_tcslen(lpstr));
  234. GetTextExtentPoint(hDC,lpstr,_tcslen(lpstr),&sz);
  235. width += sz.cx*3/2;
  236. }
  237. dwMaxPaintPos = lpCandList->dwPageStart +lpCandList->dwPageSize ;
  238. if(dwMaxPaintPos > lpCandList->dwCount + 2)  dwMaxPaintPos = lpCandList->dwCount + 2;
  239. wCount = 0;
  240.             for (i = lpCandList->dwPageStart; i < dwMaxPaintPos; i++)
  241.             {
  242. wCount++;
  243.                 lpstr = GETLPCANDSTR(lpCandList,i);
  244. GetTextExtentPoint(hDC,lpstr,_tcslen(lpstr),&sz);
  245. if(wConversionSet & CONVERSION_SET_SHAPE) {
  246. _stprintf(szStr,"%d%s ",wCount%10,lpstr);
  247. TextOut(hDC,width,height,szStr,_tcslen(szStr));
  248. GetTextExtentPoint(hDC,szStr,_tcslen(szStr),&sz);
  249. width += sz.cx;
  250. }
  251. else {
  252. _stprintf(szStr,"%d %s ",wCount%10,lpstr);
  253. TextOut(hDC,width,height,szStr,_tcslen(szStr));
  254. GetTextExtentPoint(hDC,szStr,_tcslen(szStr),&sz);
  255. height += sz.cy;
  256. }
  257.             }
  258. if(wConversionSet & CONVERSION_SET_SHAPE) {
  259. lpstr = GETLPCANDSTR(lpCandList,1);
  260. if(_tcslen(lpstr)) {
  261. GetTextExtentPoint(hDC,"A",1,&sz);
  262. width = sizeCand[1].cx - sz.cx*3/2;
  263. TextOut(hDC,width,height,lpstr,_tcslen(lpstr));
  264. }
  265. }
  266.             ImmUnlockIMCC(lpIMC->hCandInfo);
  267.         }
  268.         ImmUnlockIMC(hIMC);
  269.     }
  270.     EndPaint(hCandWnd,&ps);
  271. }
  272. void HideCandWindow( LPUIEXTRA lpUIExtra)
  273. {
  274.     if (IsWindow(lpUIExtra->uiCand.hWnd))
  275.     {
  276.         ShowWindow(lpUIExtra->uiCand.hWnd, SW_HIDE);
  277.     }
  278. }