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

输入法编程

开发平台:

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 CompWndProc(
  23. HWND   hWnd,
  24. UINT   message,
  25. WPARAM wParam,
  26. LPARAM lParam)
  27. {
  28. HWND  hUIWnd;
  29.     HGLOBAL hUIExtra;
  30.     LPUIEXTRA lpUIExtra;
  31. hUIWnd = (HWND)GetWindowLong(hWnd,FIGWL_SVRWND);
  32.     hUIExtra = (HGLOBAL)GetWindowLong(hUIWnd,IMMGWL_PRIVATE);
  33. lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
  34.     switch (message)
  35.     {
  36.         case WM_PAINT:
  37.             PaintCompWindow( hWnd);
  38.             break;
  39.         case WM_SETCURSOR:
  40.         case WM_MOUSEMOVE:
  41.         case WM_LBUTTONUP:
  42.         case WM_RBUTTONUP:
  43.             DragUI(hWnd,lpUIExtra->uiCand.hWnd,message,wParam,lParam,TRUE);
  44.             if ((message == WM_SETCURSOR) &&
  45.                 (HIWORD(lParam) != WM_LBUTTONDOWN) &&
  46.                 (HIWORD(lParam) != WM_RBUTTONDOWN)) 
  47.                 return DefWindowProc(hWnd,message,wParam,lParam);
  48.             if ((message == WM_LBUTTONUP) || (message == WM_RBUTTONUP))
  49.                 SetWindowLong(hWnd,FIGWL_MOUSE,0L);
  50.             break;
  51.         default:
  52.             if (!MyIsIMEMessage(message)) {
  53. GlobalUnlock(hUIExtra);
  54.                 return DefWindowProc(hWnd,message,wParam,lParam);
  55. }
  56.             break;
  57.     }
  58.     GlobalUnlock(hUIExtra);
  59.     return 0;
  60. }
  61. void CreateCompWindow( HWND hUIWnd, LPUIEXTRA lpUIExtra)
  62. {
  63.     if (!IsWindow(lpUIExtra->uiComp.hWnd))
  64.     {
  65. HDC hDC;
  66. SIZE sz;
  67. TCHAR szStr[100];
  68. lpUIExtra->uiComp.hWnd = 
  69.             CreateWindowEx( WS_EX_WINDOWEDGE,
  70.                          COMPCLASSNAME,NULL,
  71.                          WS_DISABLED | WS_POPUP | WS_DLGFRAME,
  72.                          0,
  73.                          0,
  74.                          1,
  75.  1,
  76.                          hUIWnd,NULL,hInst,NULL);
  77. SetWindowLong(lpUIExtra->uiComp.hWnd,FIGWL_SVRWND,(DWORD)hUIWnd);
  78. _stprintf(szStr,_T("AAAAAAAAAAAAA"));
  79. hDC = GetDC(lpUIExtra->uiComp.hWnd);
  80. GetTextExtentPoint(hDC,szStr,_tcslen(szStr),&sz);
  81. ReleaseDC(lpUIExtra->uiComp.hWnd,hDC);
  82. lpUIExtra->uiComp.sz.cx = sz.cx;
  83. lpUIExtra->uiComp.sz.cy = sz.cy+4;
  84.     }
  85.     ShowWindow(lpUIExtra->uiComp.hWnd,SW_HIDE);
  86.     return;
  87. }
  88. void MoveCompWindow( HWND hUIWnd, LPUIEXTRA lpUIExtra, LPINPUTCONTEXT lpIMC)
  89. {
  90. if (!IsWindow(lpUIExtra->uiComp.hWnd))
  91. CreateCompWindow( hUIWnd, lpUIExtra);
  92. if (IsWindow(lpUIExtra->uiComp.hWnd))
  93. {
  94. HDC hDC;
  95. LPCOMPOSITIONSTRING lpCompStr;
  96. POINT pt;
  97. RECT screenrc;
  98. LPTSTR lpStr;
  99. SIZE sz;
  100. sz.cx = 0;
  101. sz.cy = 0;
  102. if (lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr))
  103. {
  104. if ((lpCompStr->dwSize > sizeof(COMPOSITIONSTRING))
  105. && (lpCompStr->dwCompStrLen > 0))
  106. {
  107. lpStr = 
  108. ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPaintCompStr;
  109. hDC = GetDC(lpUIExtra->uiComp.hWnd);
  110. GetTextExtentPoint(hDC,lpStr,_tcslen(lpStr),&sz);
  111. ReleaseDC(lpUIExtra->uiComp.hWnd,hDC);
  112. if(_tcslen(lpStr)) sz.cx += 2*sz.cx/_tcslen(lpStr);
  113. ImmUnlockIMCC(lpIMC->hCompStr);
  114. }
  115. else{
  116. ShowWindow(lpUIExtra->uiComp.hWnd, SW_HIDE);
  117. ImmUnlockIMCC(lpIMC->hCompStr);
  118. return;
  119. }
  120. }
  121. if(sz.cx < lpUIExtra->uiComp.sz.cx)
  122. sz.cx = lpUIExtra->uiComp.sz.cx;
  123. sz.cy = lpUIExtra->uiComp.sz.cy;
  124. sz.cx += 4 * GetSystemMetrics(SM_CXEDGE);
  125. sz.cy += 4 * GetSystemMetrics(SM_CYEDGE);
  126. if(lpUIExtra->uiComp.pt.x < 0) {
  127. pt.x = 15;
  128. pt.y = 15;
  129. ClientToScreen(lpIMC->hWnd, &pt);
  130. }
  131. else{
  132. pt.x = lpUIExtra->uiComp.pt.x;
  133. pt.y = lpUIExtra->uiComp.pt.y;
  134. }
  135. SystemParametersInfo(SPI_GETWORKAREA,
  136. 0,
  137. &screenrc,
  138. 0);
  139. if( (pt.x + sz.cx) > screenrc.right )
  140. pt.x = screenrc.right - sz.cx;
  141. if( (pt.y + sz.cy) > screenrc.bottom )
  142. pt.y = screenrc.bottom - sz.cy;
  143. MoveWindow(lpUIExtra->uiComp.hWnd,
  144. pt.x,
  145. pt.y,
  146. sz.cx,
  147. sz.cy,
  148. TRUE);
  149. ShowWindow(lpUIExtra->uiComp.hWnd, SW_SHOWNOACTIVATE);
  150. InvalidateRect(lpUIExtra->uiComp.hWnd,NULL,FALSE);
  151. }
  152. }
  153. void PaintCompWindow( HWND hCompWnd)
  154. {
  155.     PAINTSTRUCT ps;
  156.     HIMC hIMC;
  157.     LPINPUTCONTEXT lpIMC;
  158.     LPCOMPOSITIONSTRING lpCompStr;
  159.     HDC hDC;
  160.     RECT rc;
  161.     HWND hSvrWnd;
  162. HBRUSH hBrush = (HBRUSH)NULL;
  163. HBRUSH hOldBrush = (HBRUSH)NULL;
  164. HPEN hPen = (HPEN)NULL;
  165. HPEN hOldPen = (HPEN)NULL;
  166.     hDC = BeginPaint(hCompWnd,&ps);
  167. GetClientRect(hCompWnd,&rc);
  168. hBrush = GetStockObject(LTGRAY_BRUSH);
  169. hOldBrush=SelectObject(hDC,hBrush);
  170. PatBlt(hDC,
  171. rc.left,
  172. rc.top ,
  173. rc.right - GetSystemMetrics(SM_CXEDGE)/2,
  174. rc.bottom - GetSystemMetrics(SM_CYEDGE)/2,
  175. PATCOPY);
  176. if(hBrush && hOldBrush)
  177. SelectObject(hDC,hOldBrush);
  178. hPen = GetStockObject(WHITE_PEN);
  179. hOldPen = SelectObject(hDC,hPen);
  180. MoveToEx(hDC,0,rc.bottom - GetSystemMetrics(SM_CXEDGE)/2,NULL);
  181. LineTo(hDC,rc.right-GetSystemMetrics(SM_CXEDGE)/2,rc.bottom - GetSystemMetrics(SM_CXEDGE)/2);
  182. LineTo(hDC,rc.right-GetSystemMetrics(SM_CXEDGE)/2,0);
  183. hPen = CreatePen(PS_SOLID ,0,RGB(128,128,128));
  184. SelectObject(hDC,hPen);
  185. MoveToEx(hDC,rc.right-GetSystemMetrics(SM_CXEDGE)/2,0,NULL);
  186. LineTo(hDC,0,0);
  187. LineTo(hDC,0,rc.bottom-GetSystemMetrics(SM_CYEDGE)/2);
  188. SelectObject(hDC,hOldPen);
  189. DeleteObject(hPen);
  190.     hSvrWnd = (HWND)GetWindowLong(hCompWnd,FIGWL_SVRWND);
  191.     if (hIMC = (HIMC)GetWindowLong(hSvrWnd,IMMGWL_IMC))
  192.     {
  193. if( (lpIMC = ImmLockIMC(hIMC)) == NULL ){
  194. EndPaint(hCompWnd,&ps);
  195. return;
  196. }
  197.         if (lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr))
  198.         {
  199.             if ((lpCompStr->dwSize > sizeof(COMPOSITIONSTRING))
  200.                && (lpCompStr->dwCompStrLen > 0))
  201.             {
  202.                 LPTSTR lpStr,lpPaintStr;
  203. SIZE sz,sz1;
  204. SHORT wEditCaret;
  205. SHORT wCharNum,wCount;
  206. int i;
  207. wEditCaret = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.wEditCaret;
  208. lpStr = GETLPCOMPSTR(lpCompStr);
  209. lpPaintStr = 
  210. ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPaintCompStr;
  211. SetBkMode(hDC,TRANSPARENT);
  212. SetPaintColor(hDC, wConversionMode);
  213. TextOut(hDC,2,2,lpPaintStr,_tcslen(lpPaintStr));
  214. wCharNum = _tcslen(lpStr) - wEditCaret;
  215. wCount = 0;
  216. for(i = _tcslen(lpPaintStr);i;i--) {
  217. if(wCount == wCharNum) break;
  218. if( *(lpPaintStr + i -1 ) != _T(' ')) wCount++;
  219. }
  220. GetTextExtentPoint(hDC,lpPaintStr,i,&sz);
  221. GetTextExtentPoint(hDC,"A",1,&sz1);
  222. hPen = CreatePen(PS_SOLID,3,RGB(0,0,0));
  223. hOldPen = SelectObject(hDC,hPen);
  224. MoveToEx(hDC,sz.cx + sz1.cx/4,sz1.cy*11/10,NULL);
  225. LineTo(hDC,sz.cx + sz1.cx*2/3,sz1.cy*11/10);
  226. SelectObject(hDC,hOldPen);
  227. DeleteObject(hPen);
  228.             }
  229.             ImmUnlockIMCC(lpIMC->hCompStr);
  230.         }
  231.         ImmUnlockIMC(hIMC);
  232.     }
  233.     EndPaint(hCompWnd,&ps);
  234. }
  235. void HideCompWindow(LPUIEXTRA lpUIExtra)
  236. {
  237.     if (IsWindow(lpUIExtra->uiComp.hWnd))
  238.     {
  239.         ShowWindow(lpUIExtra->uiComp.hWnd, SW_HIDE);
  240.     }
  241. }