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

输入法编程

开发平台:

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. #include "QQHwnd.h"
  8. extern DllManager ChLib;//定义在主DLL中
  9. //窗口的初始化
  10. ConHwnd::initalizeWin()
  11. {
  12.     
  13. strcpy(ModleName[0],"基本优先");
  14. strcpy(ModleName[1],"扩展优先");
  15. strcpy(ModleName[2],"五笔模式");
  16. strcpy(ModleName[3],"扩展模式");
  17. WindDC=GetDC(m_hWnd);
  18. DeleteObject(SelectObject(WindDC, hUIFont));
  19. Texthdc = CreateCompatibleDC(WindDC);
  20. Texthdcbmp=CreateCompatibleBitmap(WindDC,WinWidth,WinHeight);
  21. DeleteObject(SelectObject(Texthdc, hUIFont));
  22. DeleteObject(SelectObject(Texthdc,Texthdcbmp));
  23. SetBkMode(Texthdc,TRANSPARENT);
  24. TextH=GetTabbedTextExtent(WindDC,"1",1,0,0)/0x10000; 
  25. SetWindowRgn(m_hWnd,WinRGN1,TRUE);
  26. BackhBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCE(IDB_contrl));
  27. BackDC = CreateCompatibleDC(WindDC);
  28. DeleteObject(SelectObject(BackDC,BackhBmp));
  29. reBackDC = CreateCompatibleDC(WindDC);
  30. reBackhBmp=CreateCompatibleBitmap(WindDC,WinWidth,WinHeight);
  31. DeleteObject(SelectObject(reBackDC,reBackhBmp));
  32. BitBlt(reBackDC,0,0,WinWidth,WinHeight,BackDC,0,0,SRCCOPY);
  33. }
  34. ConHwnd::ConHwnd(void*CMthis,DWORD himc)
  35. {
  36. POINT pp[100];
  37. int offsetx,offsety;
  38. HRGN TemRGN;
  39. Main_CWnd=(CMyWnd*)CMthis;
  40. fCanMove=0;
  41. myResM=&((CMyWnd*)Main_CWnd)->myResM;
  42. WinStartPos=ReadRegPos("ConHwnd");
  43. WinStartPos.x=WinStartPos.x-31;
  44. WinStartPos.y=WinStartPos.y+15;
  45. WinHeight=120;
  46. WinWidth=100;
  47. controlflag=0;
  48. offsetx=31;
  49. offsety=5;
  50. pp[0].x=offsetx; pp[0].y=offsety;
  51. pp[1].x=offsetx; pp[1].y=offsety+80;
  52. pp[2].x=offsetx+23; pp[2].y=offsety+80;
  53. pp[3].x=offsetx+23; pp[3].y=offsety+20;
  54. TemRGN=CreatePolygonRgn(pp,4,1);
  55. WinRGN1=CreateEllipticRgn(4,62,55,113);
  56. CombineRgn(WinRGN1,WinRGN1,TemRGN,RGN_OR);
  57. DeleteObject(TemRGN);
  58. TemRGN=CreatePolygonRgn(pp,4,1);
  59. WinRGN2=CreateEllipticRgn(4,62,55,113);
  60. CombineRgn(WinRGN2,WinRGN2,TemRGN,RGN_OR);
  61. DeleteObject(TemRGN);
  62. hUIFont = CreateFont(20, 0, 0, 0, FW_NORMAL,
  63. FALSE, FALSE, FALSE, ANSI_CHARSET,
  64. OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
  65. DEFAULT_PITCH, "宋体");
  66. myHwndParent=NULL;
  67.     // it's NULL context.
  68. keyboard =new CIMESoftKbd;
  69. keyboard->initalize(himc);
  70. }
  71. ConHwnd::~ConHwnd()
  72. {
  73. try{
  74. //如果不消毁窗口当主程序,退出时窗口依然在运行,会引起系统崩溃
  75. DeleteObject (WinRGN1);
  76. DeleteObject (WinRGN2);
  77. DeleteObject(hUIFont);
  78. DeleteDC (BackDC);
  79. DeleteObject(BackhBmp);
  80. DeleteDC (reBackDC);
  81. DeleteObject(reBackhBmp);
  82. DeleteDC (Texthdc);
  83. DeleteObject(Texthdcbmp);
  84. delete keyboard;//软键盘的释放也可能引起致使错误,暂不释放。
  85. //可能在窗口处理程序中本窗口已被删除。
  86. //让程序暂时进行处理,以防止无效访问已经退出的代码段
  87. if(IsWindow(m_hWnd)){
  88. ReleaseDC(m_hWnd,WindDC);
  89. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  90. DestroyWindow(m_hWnd); 
  91. //m_hWnd=NULL;
  92. }
  93.   } catch (...) {
  94.        MessageBox(0,"~ConHwnd","有错误发生",0);
  95.   }
  96. }
  97. BOOL ConHwnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
  98. {
  99. // 初始化全局字串
  100. WNDCLASSEX wcex;
  101. wcex.cbSize = sizeof(WNDCLASSEX); 
  102. wcex.style = CS_IME;
  103. wcex.lpfnWndProc = InitProc;
  104. wcex.cbClsExtra = 0;
  105. wcex.cbWndExtra = 12;
  106. wcex.hInstance = hInstance;
  107. wcex.hIcon = 0;
  108. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  109. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  110. wcex.lpszMenuName = 0;
  111. wcex.lpszClassName = szClassName;
  112. wcex.hIconSm = 0;
  113. hInst=hInstance;
  114. RegisterClassEx(&wcex);
  115. {
  116.    m_hWnd=CreateWindowEx( 0,
  117. szClassName,NULL,
  118. WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
  119. WinStartPos.x,WinStartPos.y,WinWidth,WinHeight,
  120. hWndParent,NULL,hInstance,this);
  121. ShowWindow(m_hWnd,SW_SHOWNOACTIVATE);
  122. UpdateWindow(m_hWnd);
  123. }
  124. myHwndParent=hWndParent;
  125. return m_hWnd != NULL;
  126. }
  127.  LRESULT WINAPI ConHwnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
  128.  {
  129.   POINT pt;
  130.   int st;
  131.   long x,y;
  132.   static int moveflage=0;
  133.   QQHwnd  *QQ;
  134.   static int QQtimenum;
  135.   QQ=((CMyWnd*)Main_CWnd)->myQQWind;
  136.   try {
  137.     switch (uMsg)
  138.     {
  139. case WM_CREATE:
  140. //myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  141. initalizeWin();
  142. myset.mySetWindow(m_hWnd,RGB(255,255,255),myResM->clarityflage,LWA_ALPHA|LWA_COLORKEY);
  143.   break;
  144. case WM_PAINT://绘制窗口
  145.   myPaintTxt(0);
  146. break;
  147.         case WM_SETCURSOR:
  148. if ((HIWORD(lParam) == WM_MOUSEMOVE))
  149. {
  150. GetCursorPos( &pt );
  151. GetWindowRect(m_hWnd,&WinRect);
  152. WinSIZE.cx = WinRect.right - WinRect.left;
  153. WinSIZE.cy = WinRect.bottom - WinRect.top;
  154. Cursordif.x = pt.x - WinRect.left;//得到相对位置
  155. Cursordif.y = pt.y - WinRect.top;
  156. SetCapture(m_hWnd);
  157. //SetWindowLong(m_hWnd,4,1);//GWL_HINSTANCE DWL_MSGRESULT
  158. //SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
  159. }
  160. else 
  161. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  162. break;
  163.         case WM_LBUTTONDOWN:
  164. fCanMove = TRUE;
  165. moveflage=0;
  166. break;
  167. case WM_RBUTTONUP:
  168. GetCursorPos( &pt );//取得光标位置
  169. y=lParam/0x10000;
  170. x=lParam%0x10000;
  171. st=getRgnFlage(x,y);
  172. if(st==3)
  173. {
  174. keyboard->Popup();
  175. }
  176. if(st==6)
  177. {
  178. ModlePopup(myResM->CurModleNum);
  179. }
  180. break;
  181. case WM_TIMER:
  182. break;
  183.         case WM_LBUTTONUP:
  184. GetCursorPos( &pt );//取得光标位置
  185. y=lParam/0x10000;
  186. x=lParam%0x10000;
  187. if( fCanMove) 
  188. {
  189. GetCursorPos( &pt );
  190. pt.x=pt.x - Cursordif.x;
  191. pt.y=pt.y - Cursordif.y;
  192. MoveWindow(m_hWnd,pt.x,pt.y,WinSIZE.cx,WinSIZE.cy,TRUE);
  193. WriteRegPos("ConHwnd",pt);
  194. fCanMove = FALSE;
  195. }
  196. if(moveflage<3)//有大量移动就不进行操作
  197. {
  198. st=getRgnFlage(x,y);
  199. switch(st)
  200. {
  201. case 1:
  202. case 2:
  203. TranStr(st);
  204. break;
  205. case 3:
  206. keyboard->ShowSoftKbd();
  207. break;
  208. case 4:
  209. controlflag=1;
  210. keybd_event(192,0,0,0);
  211. keybd_event(192,0,KEYEVENTF_KEYUP,0);
  212. break;
  213. case 5:
  214. ShowWindow(((CMyWnd*)Main_CWnd)->m_hWnd, SW_HIDE);
  215. ShowWindow(((CMyWnd*)Main_CWnd)->inputWind->m_hWnd, SW_HIDE);
  216. break;
  217. case 6://模式设置
  218. {
  219. char buffer[5];
  220. myResM->CurModleNum++;
  221. if(myResM->CurModleNum>4)
  222. myResM->CurModleNum=1;
  223. ChLib.SetIMEModle(myResM->CurModleNum);
  224. buffer[0]=myResM->CurModleNum+0x30;
  225. buffer[1]=0;
  226. RegSaveString("CurModleNum",buffer); 
  227. ((CMyWnd*)Main_CWnd)->ClearWin();
  228. QQ->settxt(m_hWnd,x,y,ModleName[myResM->CurModleNum-1],2);
  229. }
  230. break;
  231. case 7:
  232. RECT Rect;
  233. GetWindowRect(m_hWnd,&Rect); 
  234. if(Rect.bottom-Rect.top>20)
  235. MoveWindow(m_hWnd,Rect.left,Rect.top,WinWidth,20,TRUE);
  236. else
  237. MoveWindow(m_hWnd,Rect.left,Rect.top,WinWidth,WinHeight,TRUE);
  238. ShowWindow(((CMyWnd*)Main_CWnd)->m_hWnd, SW_HIDE);
  239. ShowWindow(((CMyWnd*)Main_CWnd)->inputWind->m_hWnd, SW_HIDE);
  240. break;
  241. default:
  242. break;
  243. }
  244. }
  245. myPaintTxt(0);
  246. break;
  247.         
  248.         case WM_MOUSEMOVE:
  249. moveflage++;
  250. GetCursorPos( &pt );//取得光标位置
  251. y=lParam/0x10000;
  252. x=lParam%0x10000;
  253. st=getRgnFlage(x,y);
  254. if(fCanMove==TRUE)
  255. {
  256. myPaintTxt(st);
  257. MoveWindow(m_hWnd,pt.x - Cursordif.x, pt.y - Cursordif.y, WinSIZE.cx,WinSIZE.cy,TRUE);
  258. //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
  259. //在不移动时可以使用它。
  260. QQ->settxt(m_hWnd,x,y,"",0);
  261. }
  262. else
  263. {
  264. if(st==0)
  265. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_SIZEALL )));
  266. else
  267. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
  268. switch(st)
  269. {
  270. case 1:
  271. {
  272. if(QQtimenum<15)
  273. {
  274. QQtimenum++;
  275. QQ->settxt(m_hWnd,x,y,"万能转换",4);
  276. }
  277. else
  278. {
  279. QQ->settxt(m_hWnd,x,y,"请选择内容",3);
  280. }
  281. }
  282.    break;
  283. case 2:
  284.    QQ->settxt(m_hWnd,x,y,"当前日期",4);
  285.    break;
  286. case 3:
  287.    QQ->settxt(m_hWnd,x,y,"软 键 盘",2);
  288.    break;
  289. case 4:
  290. if(((CMyWnd*)Main_CWnd)->mydd.appdictbool==1)
  291. QQ->settxt(m_hWnd,x,y,"造 词 中",3);
  292. else
  293. QQ->settxt(m_hWnd,x,y," 词  语 ",3);
  294.    break;
  295. case 5:
  296.    QQ->settxt(m_hWnd,x,y,"隐藏窗口",3);
  297.    break;
  298. case 6:
  299.    QQ->settxt(m_hWnd,x,y,ModleName[myResM->CurModleNum-1],2);
  300.    break;
  301. case 7:
  302.    QQ->settxt(m_hWnd,x,y,"隐藏",2);
  303.    break;
  304. default:
  305. QQtimenum=0;
  306.    QQ->settxt(m_hWnd,x,y,"",0);
  307.    break;
  308. }
  309. myPaintTxt(st);
  310.    //测试鼠标是否移出窗口
  311. if(PtInRegion(WinRGN2,x,y)==0)
  312. {
  313. QQ->settxt(m_hWnd,x,y,"",0);
  314. myPaintTxt(0);
  315. }
  316. if(fCanMove==FALSE )
  317. {   
  318. //SetWindowLong(m_hWnd,4,0);
  319. ReleaseCapture();
  320. }
  321. }
  322. break;
  323. case WM_DESTROY:
  324. //SendMessage(m_hWnd,WM_CLOSE,0,0);
  325. if(myHwndParent==NULL)
  326. ;//PostQuitMessage(0);
  327. else
  328. {
  329. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  330. if(IsWindow(m_hWnd))
  331. DestroyWindow(m_hWnd);
  332. }
  333. break;
  334.         default:
  335.                 return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  336.             break;
  337.     }
  338.   } catch (...) {
  339.        MessageBox(0,"ConHwndWindowProc","有错误发生",0);
  340.   }
  341.     return 0;
  342. }
  343. //确定指定位置所在的区域
  344. int ConHwnd::getRgnFlage(long x,long y)
  345. {
  346. int ret=0;
  347. long rgb,r,g,b;
  348. rgb=GetPixel(BackDC,x+63,y);
  349. r=GetRValue(rgb);
  350. g=GetGValue(rgb);
  351. b=GetBValue(rgb);
  352. if(r==255 && g==0 && b==0)
  353. ret=1;
  354. if(r==0 && g==255 && b==0)
  355. ret=2;
  356. if(r==0 && g==0 && b==255)
  357. ret=3;
  358. if(r==255 && g==255 && b==0)
  359. ret=4;
  360. if(r==0 && g==255 && b==255)
  361. ret=5;
  362. if(r==255 && g==0 && b==255)
  363. ret=6;
  364. if(r==0 && g==0 && b==128)
  365. ret=7;
  366.    return ret;
  367. }
  368. int ConHwnd::myPaintTxt(int select )
  369. {
  370.   PAINTSTRUCT ps;
  371.   HDC hdc;
  372.     static savselect=-1;
  373. int wd,wh,offsety=0,offsetx;
  374. wd=WinWidth;
  375. wh=WinHeight;
  376. offsetx=30;
  377. offsety=-10;
  378. hdc=Texthdc;
  379. if(select!=savselect)
  380. {
  381. savselect=select;
  382. BitBlt(BackDC,0,0,wd,wh,reBackDC,0,0,SRCCOPY);//SRCCOPY
  383. //清除掉文本区
  384. if(select==0)
  385. {
  386. BitBlt(hdc,0,0,wd,wh,BackDC,0,0,SRCCOPY);
  387. }
  388. else
  389. {
  390. switch(select)
  391. {
  392. case 1:
  393. offsety=-10;
  394. MoveToEx(BackDC,offsetx+5,offsety+25,NULL);
  395. LineTo(BackDC,offsetx+20,offsety+40);
  396. LineTo(BackDC,offsetx+20,offsety+55);
  397. LineTo(BackDC,offsetx+5,offsety+40);
  398. LineTo(BackDC,offsetx+5,offsety+25);
  399. break;
  400. case 2:
  401. offsety=15;
  402. MoveToEx(BackDC,offsetx+5,offsety+25,NULL);
  403. LineTo(BackDC,offsetx+20,offsety+40);
  404. LineTo(BackDC,offsetx+20,offsety+55);
  405. LineTo(BackDC,offsetx+5,offsety+40);
  406. LineTo(BackDC,offsetx+5,offsety+25);
  407. break;
  408. case 3:
  409. TransparentBlt2(BackDC,30,65,25,35,BackDC,5,174,25,35,RGB(255,255,255));
  410. break;
  411. case 4:
  412. TransparentBlt2(BackDC,7,62,35,25,BackDC,2,117,35,25,RGB(255,255,255));
  413. break;
  414. case 5:
  415. TransparentBlt2(BackDC,4,76,25,35,BackDC,5,213,25,35,RGB(255,255,255));
  416. break;
  417. case 6:
  418. TransparentBlt2(BackDC,17,88,35,25,BackDC,2,144,35,25,RGB(255,255,255));
  419. break;
  420. case 7:
  421. TransparentBlt2(BackDC,33,10,10,10,BackDC,96,10,10,10,RGB(255,255,255));
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. }
  428. BitBlt(hdc,0,0,wd,wh,BackDC,0,0,SRCCOPY);
  429. BeginPaint(m_hWnd, &ps);
  430.     WindDC=GetDC(m_hWnd);
  431. BitBlt(WindDC,0,0,wd,wh,hdc,0,0,SRCCOPY);
  432.     ReleaseDC(m_hWnd,WindDC);
  433. EndPaint(m_hWnd, &ps);
  434. }
  435. int ConHwnd::RedrawWin()
  436. {
  437. RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
  438. return 0;
  439. }
  440. int ConHwnd::TranStr(int selet)
  441. {
  442.     HIMC  hUICurIMC;
  443. char buf[1001]="";
  444. mytool jjj;
  445. int tt=0,rr=0;
  446.      hUICurIMC= (HIMC)GetWindowLong(myHwndParent,IMMGWL_IMC);
  447. if(hUICurIMC)
  448. {
  449. jjj.TranComStr(selet,buf);
  450. //{
  451. // char ss[1001]="";
  452. //sprintf(ss,"%d",strlen(buf));
  453. //MessageBox(0,ss,"d",0);
  454. //buf[80]=0;
  455. //}
  456. IMESenChiApp(hUICurIMC,buf);
  457. }
  458. return 0;
  459. }
  460. long ConHwnd::ModlePopup(int LCurModleNum)
  461. {
  462.   long iMenu,  result;
  463.   POINT p ;
  464.   char buffer [3]="";
  465.   long flag=0;
  466.   HMENU hMenu;
  467.     GetCursorPos(&p);
  468.     hMenu = CreatePopupMenu();
  469.     for(iMenu = 1 ;iMenu<5;iMenu++)
  470. {
  471.    flag=0;
  472.    if(LCurModleNum==iMenu)
  473.    flag|=MF_CHECKED;
  474.    else
  475.    flag|=MF_ENABLED;
  476.    AppendMenu (hMenu, flag, iMenu, ModleName[iMenu-1]);
  477.     }
  478.     iMenu = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON + TPM_LEFTALIGN + TPM_NONOTIFY + TPM_RETURNCMD, p.x, p.y, 0, GetForegroundWindow(), 0);
  479.     //得到菜单的选择字符串
  480.     result = GetMenuString(hMenu, (iMenu - 1), buffer, strlen(buffer), MF_BYPOSITION);
  481.     
  482.     DestroyMenu(hMenu);
  483. if(iMenu>0)
  484. {
  485. ChLib.SetIMEModle(iMenu);
  486. myResM->CurModleNum=iMenu;
  487. buffer[0]=myResM->CurModleNum+0x30;
  488. buffer[1]=0;
  489. RegSaveString("CurModleNum",buffer); 
  490. ((CMyWnd*)Main_CWnd)->ClearWin();
  491. }
  492. return  iMenu;
  493. }