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

输入法编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "ConHwnd.h"
  4. #include "windows.h"
  5. #include "CMyWnd.h"
  6. #include "tool\mytool.h"
  7. ConHwnd::ConHwnd()
  8. {
  9. POINT pp[100];
  10. fCanMove=0;
  11. WinStartPos=myResM.ReadRegPos("ConHwnd");
  12. WinHeight=myResM.WinHeight;
  13. WinWidth=myResM.WinWidth;
  14. ControlHeight=myResM.ControlHeight;
  15. pp[0].x=20+3; pp[0].y=WinHeight+ControlHeight;
  16. pp[1].x=20+3; pp[1].y=ControlHeight+40-4;
  17. pp[2].x=0; pp[2].y=ControlHeight+20-4;
  18. pp[3].x=0; pp[3].y=WinHeight+ControlHeight;
  19. WinRGN1=CreatePolygonRgn(pp,4,1);
  20. WinRGN2=CreatePolygonRgn(pp,4,1);
  21. hUIFont = CreateFont(20, 0, 0, 0, FW_NORMAL,
  22. FALSE, FALSE, FALSE, ANSI_CHARSET,
  23. OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
  24. DEFAULT_PITCH, "宋体");
  25. myHwndParent=NULL;
  26. }
  27. ConHwnd::~ConHwnd()
  28. {
  29. SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  30. DestroyWindow(m_hWnd);
  31. // MessageBox(0,"~ConHwnd",0,0);
  32. m_hWnd=NULL;
  33. //如果不消毁窗口当主程序,退出时窗口依然在运行,会引起系统崩溃
  34. DeleteObject(hUIFont);
  35. DeleteObject (WinRGN1);
  36. }
  37. BOOL ConHwnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
  38. {
  39. // 初始化全局字串
  40. WNDCLASSEX wcex;
  41. wcex.cbSize = sizeof(WNDCLASSEX); 
  42. wcex.style = CS_IME;
  43. wcex.lpfnWndProc = InitProc;
  44. wcex.cbClsExtra = 0;
  45. wcex.cbWndExtra = 12;
  46. wcex.hInstance = hInstance;
  47. wcex.hIcon = 0;
  48. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  49. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  50. wcex.lpszMenuName = 0;
  51. wcex.lpszClassName = szClassName;
  52. wcex.hIconSm = 0;
  53. hInst=hInstance;
  54. RegisterClassEx(&wcex);
  55. {
  56.    m_hWnd=CreateWindowEx( 0,
  57. szClassName,NULL,
  58. WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
  59. WinStartPos.x,WinStartPos.y,WinWidth,WinHeight,
  60. hWndParent,NULL,hInstance,this);
  61. ShowWindow(m_hWnd,SW_SHOWNOACTIVATE);
  62. UpdateWindow(m_hWnd);
  63. }
  64. myHwndParent=hWndParent;
  65. return m_hWnd != NULL;
  66. }
  67.  LRESULT WINAPI ConHwnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
  68.  {
  69.   POINT pt;
  70.   int st;
  71.   long x,y;
  72.   static int moveflage;
  73.   static int tt;
  74.   char ss[100];
  75.   try {
  76. sprintf(ss,"%d",uMsg);
  77. TRACE("Paste received = '%s'rn", ss);
  78.     switch (uMsg)
  79.     {
  80. case WM_CREATE:
  81. myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  82. initalizeWin();
  83. myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  84.   break;
  85. case WM_PAINT://绘制窗口
  86.   myPaintTxt(0);
  87. break;
  88.         case WM_SETCURSOR:
  89. if ((HIWORD(lParam) == WM_MOUSEMOVE))
  90. {
  91. GetCursorPos( &pt );
  92. GetWindowRect(m_hWnd,&WinRect);
  93. WinSIZE.cx = WinRect.right - WinRect.left;
  94. WinSIZE.cy = WinRect.bottom - WinRect.top;
  95. Cursordif.x = pt.x - WinRect.left;//得到相对位置
  96. Cursordif.y = pt.y - WinRect.top;
  97. SetCapture(m_hWnd);
  98. //SetWindowLong(m_hWnd,4,1);//GWL_HINSTANCE DWL_MSGRESULT
  99. //SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
  100. }
  101. else 
  102. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  103. break;
  104.         case WM_LBUTTONDOWN:
  105. fCanMove = TRUE;
  106. moveflage=0;
  107. break;
  108.         case WM_LBUTTONUP:
  109. GetCursorPos( &pt );//取得光标位置
  110. y=lParam/0x10000;
  111. x=lParam%0x10000;
  112. if( fCanMove) 
  113. {
  114. GetCursorPos( &pt );
  115. MoveWindow(m_hWnd,pt.x - Cursordif.x,pt.y - Cursordif.y,WinSIZE.cx,WinSIZE.cy,TRUE);
  116. myResM.WriteRegPos("ConHwnd",pt);
  117. fCanMove = FALSE;
  118. }
  119. if(moveflage==0)
  120. {
  121. //确定选择区域
  122. st=getRgnFlage(x,y);
  123. TranStr(st);
  124. }
  125. myPaintTxt(0);
  126. break;
  127.         
  128.         case WM_MOUSEMOVE:
  129. moveflage=1;
  130. GetCursorPos( &pt );//取得光标位置
  131. y=lParam/0x10000;
  132. x=lParam%0x10000;
  133. if(fCanMove==TRUE)
  134. {
  135. MoveWindow(m_hWnd,pt.x - Cursordif.x, pt.y - Cursordif.y, WinSIZE.cx,WinSIZE.cy,TRUE);
  136. myPaintTxt(0);
  137. //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
  138. //在不移动时可以使用它。
  139. }
  140. else
  141. {
  142. st=getRgnFlage(x,y);
  143. if(st==0)
  144. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_SIZEALL )));
  145. else
  146. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
  147. myPaintTxt(st);
  148. }
  149.    //测试鼠标是否移出窗口
  150. if(PtInRegion(WinRGN2,x,y)==0)
  151. {
  152. myPaintTxt(0);
  153. }
  154. if(fCanMove==FALSE )
  155. {   
  156. //SetWindowLong(m_hWnd,4,0);
  157. ReleaseCapture();
  158. }
  159. break;
  160. case WM_DESTROY:
  161. //SendMessage(m_hWnd,WM_CLOSE,0,0);
  162. if(myHwndParent==NULL)
  163. {
  164. //PostQuitMessage(0);
  165. }
  166. else
  167. {
  168. SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  169. DestroyWindow(m_hWnd);
  170. }
  171. break;
  172.         default:
  173.                 return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  174.             break;
  175.     }
  176.   } catch (...) {
  177. //   delete (CMyWnd*)Main_CWnd;
  178. //   SetWindowLong(*Main_hWnd,GWL_WNDPROC,(long)endProc);
  179.  // DestroyWindow(*Main_hWnd);
  180. //   SetWindowLong(*inpu_hWnd,GWL_WNDPROC,(long)endProc);
  181.  // DestroyWindow(*inpu_hWnd);
  182. //   SetWindowLong(*Cont_hWnd,GWL_WNDPROC,(long)endProc);
  183.  // DestroyWindow(*Cont_hWnd);
  184.      //  MessageBox(0,"Error","有错误发生",0);
  185.   }
  186.     return 0;
  187. }
  188. //确定指定位置所在的区域
  189. int ConHwnd::getRgnFlage(long x,long y)
  190. {
  191. int ret=0;
  192. long rgb,r,g,b;
  193. rgb=GetPixel(BackDC,x,y-ControlHeight+120);
  194. r=GetRValue(rgb);
  195. g=GetGValue(rgb);
  196. b=GetBValue(rgb);
  197. if(r==255 && g==0 && b==0)
  198. ret=1;
  199. if(r==0 && g==255 && b==0)
  200. ret=2;
  201. if(r==0 && g==0 && b==255)
  202. ret=3;
  203.    return ret;
  204. }
  205. //窗口的初始化
  206. ConHwnd::initalizeWin()
  207. {
  208. WindDC=GetDC(m_hWnd);
  209. Texthdc = CreateCompatibleDC(WindDC);
  210. Texthdcbmp=CreateCompatibleBitmap(WindDC,WinWidth,WinHeight);
  211. SelectObject(Texthdc, hUIFont);
  212. DeleteObject(SelectObject(Texthdc,Texthdcbmp));
  213. SetBkMode(Texthdc,TRANSPARENT);
  214. SelectObject(WindDC, hUIFont);
  215. TextH=GetTabbedTextExtent(WindDC,"1",1,0,0)/0x10000; 
  216. SetWindowRgn(m_hWnd,WinRGN1,TRUE);
  217. BackhBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCE(IDB_Back));
  218. BackDC = CreateCompatibleDC(WindDC);
  219. DeleteObject(SelectObject(BackDC,BackhBmp));
  220. }
  221. int ConHwnd::myPaintTxt(int select )
  222. {
  223.   PAINTSTRUCT ps;
  224.   HDC hdc;
  225.     //清除掉文本区
  226. hdc=Texthdc;
  227. if(select==0)
  228. BitBlt(hdc,0,ControlHeight,WinWidth,WinHeight-ControlHeight,BackDC,0,0,SRCCOPY);
  229. else
  230. BitBlt(hdc,0,ControlHeight,WinWidth,WinHeight-ControlHeight,BackDC,select*25,125,SRCCOPY);
  231. BeginPaint(m_hWnd, &ps);
  232. BitBlt(WindDC,0,0,WinWidth,WinHeight,hdc,0,0,SRCCOPY);
  233. EndPaint(m_hWnd, &ps);
  234. }
  235. int ConHwnd::RedrawWin()
  236. {
  237. RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
  238. return 0;
  239. }
  240. int ConHwnd::TranStr(int selet)
  241. {
  242.     HIMC  hUICurIMC;
  243. char buf[1001]="";
  244. char ss[1001]="";
  245. mytool jjj;
  246. int tt=0,rr=0;
  247.      hUICurIMC= (HIMC)GetWindowLong(myHwndParent,IMMGWL_IMC);
  248. if(hUICurIMC)
  249. {
  250. jjj.TranComStr(selet,buf);
  251. sprintf(ss,"%d",strlen(buf));
  252. //MessageBox(0,ss,"d",0);
  253. //buf[80]=0;
  254. IMESenChiApp(hUICurIMC,buf);
  255. }
  256. return 0;
  257. }
  258. /////////////////////////////////////////////////////////////
  259. //下面为函数部分
  260. //用于向系统发送消息
  261. BOOL ConHwnd::GenerateMessage(HIMC hIMC, LPDWORD lpdwTransKey,LPGENEMSG lpGeneMsg)
  262. {
  263.     LPINPUTCONTEXT lpIMC;
  264. if( (lpIMC = ImmLockIMC(hIMC)) == NULL ) 
  265. return FALSE;
  266.     
  267.     if (IsWindow(lpIMC->hWnd))
  268.     {
  269.         LPDWORD lpdw;
  270.         if (!(lpIMC->hMsgBuf = ImmReSizeIMCC(lpIMC->hMsgBuf,
  271. sizeof(DWORD) * (lpIMC->dwNumMsgBuf +1) * 3)))
  272.             return FALSE;
  273.         if (!(lpdw = (LPDWORD)ImmLockIMCC(lpIMC->hMsgBuf)))
  274.             return FALSE;
  275.         lpdw += (lpIMC->dwNumMsgBuf) * 3;
  276.         *((LPGENEMSG)lpdw) = *lpGeneMsg;
  277.         ImmUnlockIMCC(lpIMC->hMsgBuf);
  278.         lpIMC->dwNumMsgBuf++;
  279.         ImmGenerateMessage(hIMC);
  280.     }
  281. ImmUnlockIMC(hIMC);
  282.     return TRUE;
  283. }
  284. //把转换后的汉字发到接收程序中使用内部机制.
  285. BOOL ConHwnd::IMESenChiApp(HIMC hIMC, char *ss)
  286. {
  287. //内部发送在首次发送时可能失效,因此暂时不用
  288. if(strlen(ss)<2)
  289. {
  290. GENEMSG GnMsg;
  291. LPCOMPOSITIONSTRING lpCompStr;
  292. LPINPUTCONTEXT lpIMC;
  293. lpIMC = ImmLockIMC(hIMC);
  294. lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr);
  295. if(strlen(ss))
  296. {
  297. strcpy(GETLPRESULTSTR(lpCompStr),ss);
  298. //_tcscpy(GETLPRESULTSTR(lpCompStr),"高点");
  299. lpCompStr->dwResultStrLen =strlen(ss);
  300. {
  301. char sss[100];
  302. sprintf(sss,"%d",strlen(ss));
  303. // MessageBox(0,sss,0,0);
  304. }
  305. GnMsg.msg = WM_IME_COMPOSITION;//
  306. GnMsg.wParam = 0;
  307. GnMsg.lParam = GCS_RESULTSTR;
  308. GenerateMessage(hIMC, 0,(LPGENEMSG)&GnMsg);
  309. }
  310. }
  311. else
  312. {
  313. //向获得焦点的窗口发送字符串
  314. //发送过多的字符有可能失去挂钩
  315. int i;
  316. for(i=0;i<(int)strlen(ss);)
  317. {
  318. if(ss[i]<0)
  319. {
  320. SendMessage(GetFocus(), WM_IME_CHAR, MAKEWORD(ss[i+1],ss[i]), 0);
  321.     i+=2;
  322. }
  323. else
  324.  {
  325. SendMessage(GetFocus(), WM_IME_CHAR, MAKEWORD(0,ss[i]), 0);
  326. if(ss[i]==13&&ss[i+1]==10)
  327. i++;
  328. i++;
  329. }
  330. }
  331. }
  332.     return TRUE; 
  333. }