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

输入法编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "CMyWnd.h"
  4. #include "windows.h"
  5. #include "GIF.h"
  6. #include "math.h" 
  7. CGIF m_Gif;
  8. POINT pst;
  9. int tt;
  10. CMyWnd::~CMyWnd()
  11. {
  12. //strcpy(showtxt,"");
  13. delete inputWind;
  14. delete ConWind;
  15. //让程序暂时进行处理,以防止无效访问已经退出的代码段
  16. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  17. //DestroyWindow(m_hWnd);
  18. m_hWnd=NULL;
  19. }
  20. CMyWnd::CMyWnd()
  21. {
  22. int i;
  23. inputWind=new inputHwnd;
  24. ConWind=new ConHwnd;
  25. inputWind->Main_CWnd=(void*)this;
  26. ConWind->Main_CWnd=(void*)this;
  27. x=y=50;
  28. for(i=0;i<50;i++)
  29. Arrppc[i] =NULL;
  30. TimePPch=NULL;
  31. StartPPch=NULL;
  32. WinStartPos=myResM.ReadRegPos("CMyWnd");
  33.     strcpy(inputWind->showtxt,"");
  34. settxt("名称:极速五笔 1.;版权:王华 1.;发行:2006.12.28 1.;E-m:whln669@sohu.com 1.;版本:2.10 1.;");
  35. m_hWnd=NULL;
  36. WinHeight=myResM.WinHeight;
  37. WinWidth=myResM.WinWidth;
  38. ControlHeight=myResM.ControlHeight;
  39. fCanMove = FALSE;
  40. saveWM_LButton=0;
  41. saveCurLine=0;
  42. }
  43. BOOL CMyWnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
  44. {
  45. // 初始化全局字串
  46. WNDCLASSEX wcex;
  47. //MessageBox(0,"Main create",0,0);
  48. wcex.cbSize = sizeof(WNDCLASSEX); 
  49. wcex.style = CS_IME;
  50. wcex.lpfnWndProc = InitProc;
  51. wcex.cbClsExtra = 0;
  52. wcex.cbWndExtra = 12;
  53. wcex.hInstance = hInstance;
  54. wcex.hIcon = 0;
  55. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  56. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  57. wcex.lpszMenuName = 0;
  58. wcex.lpszClassName = szClassName;
  59. wcex.hIconSm = 0;
  60. hInst=hInstance;
  61. RegisterClassEx(&wcex);
  62. {
  63.    m_hWnd=CreateWindowEx( 0,
  64. szClassName,NULL,
  65. WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
  66. WinStartPos.x,WinStartPos.y,WinWidth,WinHeight,
  67. hWndParent,NULL,hInstance,this);
  68. ShowWindow(m_hWnd,SW_SHOWNOACTIVATE);
  69. UpdateWindow(m_hWnd);
  70. }
  71. // MessageBox(0,"Main create",0,0);
  72. myHwndParent=hWndParent;
  73. inputWind->Create("inputWind",szTitle,hInstance,hWndParent,dwStyle,dwExStyle,hMenu);
  74. ConWind->Create("ConHwnd",szTitle,hInstance,hWndParent,dwStyle,dwExStyle,hMenu);
  75. Main_hWnd=&m_hWnd;
  76. Cont_hWnd=&ConWind->m_hWnd;
  77. inpu_hWnd=&inputWind->m_hWnd;
  78. inputWind->Main_hWnd=&m_hWnd;
  79. inputWind->Cont_hWnd=&ConWind->m_hWnd;
  80. inputWind->inpu_hWnd=&inputWind->m_hWnd;
  81. ConWind->Main_hWnd=&m_hWnd;
  82. ConWind->Cont_hWnd=&ConWind->m_hWnd;
  83. ConWind->inpu_hWnd=&inputWind->m_hWnd;
  84. return m_hWnd != NULL;
  85. }
  86. CMyWnd::TimeShowText(int line)
  87. {
  88. HDC hdc;
  89. int t;
  90. int lineposy;
  91. static len;
  92. static SIZE sz;
  93. hdc=myResM.Texthdc;
  94. if(saveCurLine<=0)
  95. return 0;
  96. if(page.Chistr[saveCurLine-1][0]==0)
  97. return 0;
  98.  
  99. {
  100. StartPPch=page.Chistr[saveCurLine-1];
  101. TimePPch=StartPPch;
  102. len=strlen(TimePPch);
  103. GetTextExtentExPoint(hdc,TimePPch,len,WinWidth,&t,0,&sz);
  104. }
  105. t=Txx+sz.cx;
  106. if(t<0)
  107. Txx=WinWidth-TextH;
  108. lineposy=(saveCurLine-1)*TextH;
  109.     //清除掉文本区
  110. BitBlt(hdc,0,lineposy-ControlHeight+TextH,myResM.WinWidth,TextH+5,myResM.BackDC,0,lineposy-ControlHeight+TextH,SRCCOPY);
  111. if(saveWM_LButton==0)
  112. BitBlt(hdc,0,lineposy,myResM.WinWidth,TextH,myResM.LinebarDC,0,0,SRCAND);
  113. else
  114. BitBlt(hdc,0,lineposy,myResM.WinWidth,TextH,myResM.LinebarDC1,0,0,SRCCOPY);
  115. SelectObject(hdc, myResM.hUIFont);
  116. SetTextColor(hdc,RGB(0,100,0));
  117. if(saveCurLine==1)
  118. TextOut(hdc,Txx,lineposy, TimePPch,len);
  119. else
  120. TextOut(hdc,Txx+20,lineposy, TimePPch,len);
  121. Txx-=1;
  122. return 0;
  123. }
  124.  LRESULT WINAPI CMyWnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
  125.  {
  126.   POINT pt;
  127.   RECT Rect;
  128.   COLORREF tColor;
  129.   int t;
  130.   static dely;
  131.   char gifname[256];
  132.   try {
  133.     switch (uMsg)
  134.     {
  135. case WM_CREATE:
  136. myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  137. initalizeWin();
  138. myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  139. GetModuleFileName(hInst,gifname,255);
  140. myGetPath(gifname);   //取路径 动画
  141. strcat(gifname,"1.gif");
  142. if(m_Gif.LoadGIF(gifname))
  143. ;//MessageBox(0,"map","m_Gif.LoadGIF",0);
  144. else
  145. SetTimer(m_hWnd,2,100,NULL);
  146.   break;
  147. case WM_TIMER:
  148. if(wParam==2)
  149. {
  150. KillTimer(m_hWnd,2);
  151. if(x>90)
  152. x--;
  153. if(x<90)
  154. x++;
  155. if(y>50)
  156. y--;
  157. if(y<50)
  158. y++;
  159. pt.x=x-30;
  160. pt.y=y-30;
  161. if (m_Gif.GetImageInfo (Rect,tColor,tt-1))
  162. {
  163.      BitBlt (WindDC,0,ControlHeight,myResM.WinWidth, myResM.WinHeight,myResM.Texthdc,0,0,SRCCOPY);
  164. }
  165. else
  166. {
  167. //if(m_Gif.m_ImageList.m_nCount<0)
  168. // MessageBox(0,"header",0,0);
  169. GetModuleFileName(hInst,gifname,255);
  170. myGetPath(gifname);   //取路径 动画
  171. strcat(gifname,"1.gif");
  172. m_Gif.LoadGIF( gifname);
  173. }
  174. if (!m_Gif.ShowImage (WindDC,pt,tt++))
  175. {
  176. tt = 0;
  177. m_Gif.ShowImage (WindDC,pt,tt++);
  178. }
  179. SetTimer(m_hWnd,2,100,NULL);
  180. }
  181. if(wParam==1)
  182. {
  183. TimeShowText(0);
  184. if (!m_Gif.GetImageInfo (Rect,tColor,tt-1))
  185.      BitBlt (WindDC,0,ControlHeight,myResM.WinWidth, myResM.WinHeight,myResM.Texthdc,0,0,SRCCOPY);
  186. }
  187. break;
  188. case WM_PAINT://绘制窗口
  189. myPaintTxt(0,saveWM_LButton);
  190. break;
  191.         case WM_SETCURSOR:
  192. if ((HIWORD(lParam) == WM_MOUSEMOVE))
  193. {
  194. SetTimer(m_hWnd,1,20,NULL);
  195. SetCapture(m_hWnd);
  196. //SetWindowLong(m_hWnd,4,1);//GWL_HINSTANCE DWL_MSGRESULT
  197. }
  198. else 
  199. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  200. break;
  201.         case WM_RBUTTONDOWN:
  202. myResM.FixBackPic();
  203. break;
  204.         case WM_LBUTTONDOWN:
  205. y=lParam/0x10000;
  206. x=lParam%0x10000;
  207. t=(y-ControlHeight)/TextH+1;
  208. saveWM_LButton=1;
  209. GetCursorPos( &pt );
  210. GetWindowRect(m_hWnd,&WinRect);
  211. Cursordif.x = pt.x - WinRect.left;//得到相对位置
  212. Cursordif.y = pt.y - WinRect.top;
  213. myPaintTxt(t,saveWM_LButton);
  214. fCanMove=1;
  215. break;
  216.         case WM_LBUTTONUP:
  217. y=lParam/0x10000;
  218. x=lParam%0x10000;
  219. t=(y-ControlHeight)/TextH;
  220. saveCurLine=t+1;
  221. if( fCanMove) 
  222. {
  223. GetCursorPos( &pt );
  224. pt.x=pt.x - Cursordif.x;
  225. pt.y=pt.y - Cursordif.y;
  226. MyWinMove(pt);
  227. fCanMove = FALSE;
  228. }
  229. if(saveWM_LButton==1&&inputWind->showtxt[0]!=0)
  230. {
  231. keybd_event(49 + t, 0, 0, 1);
  232. }
  233. myPaintTxt(0,saveWM_LButton);
  234. saveWM_LButton=0;
  235.            
  236. break;
  237.         
  238.         case WM_MOUSEMOVE:
  239. saveWM_LButton=0;
  240. y=lParam/0x10000;
  241. x=lParam%0x10000;
  242. t=(y-ControlHeight)/TextH+1;
  243. saveCurLine=t;
  244. Txx=0;
  245. if(fCanMove==TRUE)
  246. {
  247. GetCursorPos( &pt );//取得光标位置
  248. pt.x=pt.x - Cursordif.x;
  249. pt.y=pt.y - Cursordif.y;
  250. MyWinMove(pt);
  251. myPaintTxt(0,saveWM_LButton);
  252. //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
  253. //在不移动时可以使用它。
  254. }
  255. else
  256. {
  257. //光标的设置
  258. myMouseMove(t,saveWM_LButton);    //根据选择区域设置光标
  259. }
  260.    //测试鼠标是否移出窗口
  261. if(PtInRegion(myResM.WinRGN2,x,y)==0)
  262. {
  263. KillTimer(m_hWnd,1);
  264. myPaintTxt(0,0);
  265. }
  266. if(fCanMove==FALSE)
  267. {   
  268. //SetWindowLong(m_hWnd,4,0);
  269. ReleaseCapture();
  270. }
  271. break;
  272. case WM_DESTROY:
  273. //SendMessage(m_hWnd,WM_CLOSE,0,0);
  274. if(myHwndParent==NULL)
  275. {
  276. //PostQuitMessage(0);
  277. }
  278. else
  279. {
  280. SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  281. DestroyWindow(m_hWnd);
  282. }
  283. break;
  284.         default:
  285.                 return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  286.             break;
  287.     }
  288.     return 0;
  289.    
  290.   } catch (...) {
  291. //   delete (CMyWnd*)Main_CWnd;
  292. //   SetWindowLong(*Main_hWnd,GWL_WNDPROC,(long)endProc);
  293.  // DestroyWindow(*Main_hWnd);
  294. //   SetWindowLong(*inpu_hWnd,GWL_WNDPROC,(long)endProc);
  295.  // DestroyWindow(*inpu_hWnd);
  296. //   SetWindowLong(*Cont_hWnd,GWL_WNDPROC,(long)endProc);
  297. //   SetLastError(0);
  298.   //MessageBox(0,"mainError","有错误发生",0);
  299.   }
  300.    return 1;
  301. }
  302. //根据选择区域设置光标
  303. CMyWnd::myMouseMove(int linemove,int act)
  304. {
  305. PAINTSTRUCT ps;
  306. SetBkMode(WindDC,TRANSPARENT);
  307. BeginPaint(m_hWnd, &ps);
  308. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(32649)));
  309.    myPaintTxt(linemove,act);
  310. EndPaint(m_hWnd, &ps);
  311. }
  312. //窗口的初始化
  313. CMyWnd::initalizeWin()
  314. {
  315. WindDC=GetDC(m_hWnd);
  316. SelectObject(WindDC, myResM.hUIFont);
  317. TextH=GetTabbedTextExtent(WindDC,"1",1,0,0)/0x10000; 
  318. myResM.InitResManager(WindDC,hInst);
  319. SetWindowRgn(m_hWnd,myResM.WinRGN1,TRUE);
  320. }
  321.  
  322. int CMyWnd::myPaintTxt(int linemove ,int act )
  323. {
  324. int line=0;
  325. char shtemss[1500];
  326. PAINTSTRUCT ps;
  327. HDC hdc;
  328.    RECT LoWinRect;
  329. long h,w;
  330. GetWindowRect(m_hWnd,&LoWinRect); 
  331.    w=LoWinRect.right-LoWinRect.left;
  332. h=LoWinRect.bottom-LoWinRect.top;
  333.     //清除掉文本区
  334. hdc=myResM.Texthdc;
  335. BitBlt(hdc,0,0,myResM.WinWidth,myResM.WinHeight,myResM.BackDC,0,0,SRCCOPY);
  336. if(linemove>0 && linemove<6)
  337. {
  338. if(act==0)
  339. BitBlt(hdc,0,(linemove-1)*TextH,myResM.WinWidth,TextH,myResM.LinebarDC,0,0,SRCAND);
  340. else
  341. BitBlt(hdc,0,(linemove-1)*TextH,myResM.WinWidth,TextH,myResM.LinebarDC1,0,0,SRCCOPY);
  342. }
  343. for(line=0;line<5&&page.Chistr[line][0]!=0;line++)
  344. {
  345. // MessageBox(0,ss,"dd",0);
  346. if(page.Proprity[line]=='1')
  347. SetTextColor(hdc,RGB(0,0,255));
  348.     if(page.Proprity[line]=='2')
  349. SetTextColor(hdc,RGB(0,0,0));
  350.     if(page.Proprity[line]=='3')
  351. SetTextColor(hdc,RGB(0,100,0));
  352. sprintf(shtemss,"%d:%s%s",line+1,page.Chistr[line],page.Enstr[line]);
  353. if(line==0)
  354. TextOut(hdc,5,line*TextH, shtemss,strlen(shtemss));
  355. else
  356. TextOut(hdc,25,line*TextH, shtemss,strlen(shtemss));
  357. }
  358. BeginPaint(m_hWnd, &ps);
  359. BitBlt(WindDC,0,ControlHeight,WinWidth,WinHeight,hdc,0,0,SRCCOPY);
  360. EndPaint(m_hWnd, &ps);
  361. // if(line<3)
  362. // MoveWindow(m_hWnd,LoWinRect.left,LoWinRect.top,w,ControlHeight+TextH*2,TRUE);
  363. // else
  364. // MoveWindow(m_hWnd,LoWinRect.left,LoWinRect.top,WinWidth,WinHeight,TRUE);
  365. }
  366. //特殊字符处理,
  367. int CMyWnd:: specsif(char *ss)
  368. {
  369. int i,t,t1,t2,len;
  370. //特殊字符处理,以便支持特殊字符了输入
  371. len=strlen(ss);
  372. if(len>=4)
  373. {
  374. t=-1;
  375. for(i=0;i<len;i++)
  376. {
  377. t++;
  378. t1=t2=-1;
  379. if(ss[i]=='0' && (ss[i+1]=='x'||ss[i+1]=='X') && i<len-2 )
  380. {
  381. if(ss[i+2]>='0' && ss[i+2]<='9')
  382. t1=ss[i+2]-'0';
  383. if(ss[i+2]>='a' && ss[i+2]<='f')
  384. t1=ss[i+2]-'a'+10;
  385. if(ss[i+2]>='A' && ss[i+2]<='F')
  386. t1=ss[i+2]-'A'+10;
  387. if(ss[i+3]>='0' && ss[i+3]<='9')
  388. t2=ss[i+3]-'0';
  389. if(ss[i+3]>='a' && ss[i+3]<='f')
  390. t2=ss[i+3]-'a'+10;
  391. if(ss[i+3]>='A' && ss[i+3]<='F')
  392. t2=ss[i+3]-'A'+10;
  393. }
  394. if(t1!=-1 && t2!=-1)
  395. {
  396. //char tem[100];
  397. //sprintf(tem,"%d %d",t1,t2);
  398. t1=t1*16+t2;
  399. ss[t]=t1;
  400. //MessageBox(0,tem,ss,0);
  401. i=i+3;
  402. }
  403. else
  404. ss[t]=ss[i];
  405. }
  406. ss[t+1]=0;
  407. }
  408. return 0;
  409. }
  410. int CMyWnd:: settxt(char *ss)
  411. {
  412. int i,len,line=0;
  413. char *ppc;
  414. if(ss!=NULL)
  415. strcpy(showtxt,ss);
  416. else
  417. showtxt[0]=0;
  418. ppc=showtxt;
  419. len=strlen(ppc);
  420. for(i=0;i<5;i++)
  421. {
  422. page.Enstr[i][0]=0;
  423. page.Chistr[i][0]=0;
  424. page.Proprity[i]=0;
  425. }
  426. for(i=0;i<len;i++)
  427. {
  428. if(showtxt[i]==';')
  429. {
  430. showtxt[i]=0;
  431. strcpy(page.Enstr[line],ppc);
  432. ppc=&showtxt[i+1];
  433. line++;
  434. if(line==5)
  435. break;
  436. }
  437. if(showtxt[i]==' ')
  438. {
  439. showtxt[i]=0;
  440. strcpy(page.Chistr[line],ppc);
  441. //特殊字符处理
  442. specsif(page.Chistr[line]);
  443. page.Proprity[line]=showtxt[i+1];
  444. ppc=&showtxt[i+2];
  445. }
  446. }
  447. return 0;
  448. }
  449. int CMyWnd::MyWinMove( POINT pt)
  450. {
  451. myResM.WriteRegPos("CMyWnd",pt);
  452. myResM.WriteRegPos("ConHwnd",pt);
  453. myResM.WriteRegPos("inputHwnd",pt);
  454. SetWindowPos(m_hWnd, 0,  pt.x, pt.y,0, 0, 21);
  455. SetWindowPos(ConWind->m_hWnd,  0, pt.x, pt.y,0, 0, 21);
  456. SetWindowPos(inputWind->m_hWnd,0, pt.x, pt.y,0, 0, 21);
  457. return 0;
  458. }
  459. int CMyWnd::RedrawWin()
  460. {
  461. RECT  upwin;
  462. GetWindowRect(m_hWnd,&upwin);
  463. upwin.bottom=upwin.bottom-ControlHeight;
  464. RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
  465. return 0;
  466. }
  467. int CMyWnd::myGetPath(char *filefullname)
  468. {
  469. int  i,lon;
  470. lon=strlen(filefullname);
  471. for(i=lon;i>0;i--)
  472. if(filefullname[i]=='\')
  473. {
  474. filefullname[i+1]=0;
  475. break;
  476. }
  477. return 0;
  478. }