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

输入法编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "inputHwnd.h"
  4. #include "windows.h"
  5. #include "ResManager.h"
  6. #include "math.h"
  7. #include "CMyWnd.h"
  8. BOOL MyIsWinNT() ;
  9. inputHwnd::inputHwnd(void*CMthis)
  10. {
  11. POINT pp[100];
  12. Main_CWnd=(CMyWnd*)CMthis;
  13. myResM=&((CMyWnd*)Main_CWnd)->myResM;
  14. showtxt[0]=0;
  15. Saveshowtxt[0]=1;
  16. fCanMove=0;
  17. if(myResM->textflage[0]=='1')
  18. ControlHeight=35;
  19. else
  20. ControlHeight=24;
  21. WinHeight=100;
  22. WinWidth=170;
  23. WinStartPos=ReadRegPos("inputHwnd");
  24. pp[0].x=0; pp[0].y=35;
  25. pp[1].x=95; pp[1].y=35;
  26. pp[2].x=70; pp[2].y=0;
  27. pp[3].x=0; pp[3].y=0;
  28. WinRGN1=CreatePolygonRgn(pp,4,1);
  29. WinRGN2=CreatePolygonRgn(pp,4,1);
  30. hUIFont = CreateFont(25, 0, 0, 0, FW_NORMAL,
  31. FALSE, FALSE, FALSE, ANSI_CHARSET,
  32. OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
  33. DEFAULT_PITCH, "宋体");//"Times New Roman"
  34. }
  35.  inputHwnd::~inputHwnd()
  36.  {
  37. try{
  38. DeleteObject(hUIFont);
  39. DeleteObject (WinRGN1);
  40. DeleteObject (WinRGN2);
  41. DeleteDC (BackDC);
  42. DeleteObject(BackhBmp);
  43. DeleteDC (Texthdc);
  44. DeleteObject(Texthdcbmp);
  45. //可能在窗口处理程序中本窗口已被删除。
  46. //让程序暂时进行处理,以防止无效访问已经退出的代码段
  47. if(IsWindow(m_hWnd)){
  48. ReleaseDC(m_hWnd,WindDC);
  49. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  50. DestroyWindow(m_hWnd); 
  51. //m_hWnd=NULL;
  52. }
  53.   } catch (...) {
  54.        MessageBox(0,"~inputHwnd","有错误发生",0);
  55.   }
  56. //如果不消毁窗口当主程序,退出时窗口依然在运行,会引起系统崩溃
  57.  }
  58.  
  59. BOOL inputHwnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
  60. {
  61. // 初始化全局字串
  62. WNDCLASSEX wcex;
  63. wcex.cbSize = sizeof(WNDCLASSEX); 
  64. wcex.style = CS_IME;
  65. wcex.lpfnWndProc = InitProc;
  66. wcex.cbClsExtra = 0;
  67. wcex.cbWndExtra = 12;
  68. wcex.hInstance = hInstance;
  69. wcex.hIcon = 0;
  70. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  71. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  72. wcex.lpszMenuName = 0;
  73. wcex.lpszClassName = szClassName;
  74. wcex.hIconSm = 0;
  75. hInst=hInstance;
  76. RegisterClassEx(&wcex);
  77. {
  78.    m_hWnd=CreateWindowEx( 0,
  79. szClassName,NULL,
  80. WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
  81. WinStartPos.x,WinStartPos.y,WinWidth,ControlHeight,
  82. hWndParent,NULL,hInstance,this);
  83. ShowWindow(m_hWnd,SW_SHOWNOACTIVATE);
  84. UpdateWindow(m_hWnd);
  85.    //使白色透明掉
  86. }
  87. return m_hWnd != NULL;
  88. }
  89.  LRESULT WINAPI inputHwnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
  90.  {
  91.   POINT pt;
  92.   long x,y;
  93.   static int tt;
  94.   try {
  95.     switch (uMsg)
  96.     {
  97. case WM_CREATE:
  98. //myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  99. initalizeWin();
  100. myset.mySetWindow(m_hWnd,RGB(255,255,255),myResM->clarityflage,LWA_ALPHA|LWA_COLORKEY);
  101.   break;
  102. case WM_PAINT://绘制窗口
  103.   myPaintTxt(0,0);
  104. break;
  105.         case WM_SETCURSOR:
  106. if ((HIWORD(lParam) == WM_MOUSEMOVE))
  107. {
  108. GetCursorPos( &pt );
  109. GetWindowRect(m_hWnd,&WinRect);
  110. WinSIZE.cx = WinRect.right - WinRect.left;
  111. WinSIZE.cy = WinRect.bottom - WinRect.top;
  112. Cursordif.x = pt.x - WinRect.left;//得到相对位置
  113. Cursordif.y = pt.y - WinRect.top;
  114. SetCapture(m_hWnd);
  115. //SetWindowLong(m_hWnd,4,1);//GWL_HINSTANCE DWL_MSGRESULT
  116. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(32649)));
  117. }
  118. else 
  119. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  120. break;
  121.         case WM_RBUTTONUP:
  122. Popup();
  123. break;
  124.         case WM_LBUTTONDOWN:
  125. fCanMove = TRUE;
  126. break;
  127.         case WM_LBUTTONUP:
  128. if( fCanMove) 
  129. {
  130. GetCursorPos( &pt );
  131. pt.x=pt.x - Cursordif.x;
  132. pt.y=pt.y - Cursordif.y;
  133. myResM->WriteRegPos("inputHwnd",pt);
  134. MoveWindow(m_hWnd,pt.x ,pt.y ,WinSIZE.cx,WinSIZE.cy,TRUE);
  135. fCanMove = FALSE;
  136. }
  137. myPaintTxt(0,0);
  138. break;
  139.         
  140.         case WM_MOUSEMOVE:
  141. GetCursorPos( &pt );//取得光标位置
  142. y=lParam/0x10000;
  143. x=lParam%0x10000;
  144. if(fCanMove==TRUE)
  145. {
  146. MoveWindow(m_hWnd,pt.x - Cursordif.x, pt.y - Cursordif.y, WinSIZE.cx,WinSIZE.cy,TRUE);
  147. myPaintTxt(0,0);
  148. //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
  149. //在不移动时可以使用它。
  150. }
  151.    //测试鼠标是否移出窗口
  152. if(PtInRegion(WinRGN2,x,y)==0)
  153. {
  154. myPaintTxt(0,0);
  155. }
  156. if(fCanMove==FALSE )
  157. {   
  158. //SetWindowLong(m_hWnd,4,0);
  159. ReleaseCapture();
  160. }
  161. break;
  162. case WM_DESTROY:
  163. //SendMessage(m_hWnd,WM_CLOSE,0,0);
  164. if(myHwndParent==NULL)
  165. {
  166. //PostQuitMessage(0);
  167. }
  168. else
  169. {
  170. if(IsWindow(m_hWnd))
  171. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  172. DestroyWindow(m_hWnd);
  173. }
  174. break;
  175.         default:
  176.                 return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  177.             break;
  178.     }
  179.   } catch (...) {
  180. //   MessageBox(0,"inputError","有错误发生",0);
  181.   }
  182.     return 0;
  183. }
  184. //窗口的初始化
  185. inputHwnd::initalizeWin()
  186. {
  187. WindDC=GetDC(m_hWnd);
  188. Texthdc = CreateCompatibleDC(WindDC);
  189. Texthdcbmp=CreateCompatibleBitmap(WindDC,WinWidth,WinHeight);
  190. DeleteObject(SelectObject(Texthdc, hUIFont));
  191. DeleteObject(SelectObject(Texthdc,Texthdcbmp));
  192. SetBkMode(Texthdc,TRANSPARENT);
  193. DeleteObject(SelectObject(WindDC, hUIFont));
  194. TextH=GetTabbedTextExtent(WindDC,"1",1,0,0)/0x10000; 
  195.     //
  196. if(!MyIsWinNT())  //不是NT就进行区域设定
  197. SetWindowRgn(m_hWnd,WinRGN1,TRUE);
  198. BackhBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCE(IDB_input));
  199. BackDC = CreateCompatibleDC(WindDC);
  200. DeleteObject(SelectObject(BackDC,BackhBmp));
  201. txtpic.StartHDC(WindDC,100,80);
  202. if(txtpic.loadPic(hInst,IDB_txt)==NULL)
  203. MessageBox(0,txtpic.Picname,"图片不能装入",0);
  204. }
  205. int inputHwnd::myPaintTxt(int flag,int act )
  206. {
  207.   PAINTSTRUCT ps;
  208.   HDC hdc;
  209.     //清除掉文本区
  210. hdc=Texthdc;
  211. if(strcmp(showtxt,Saveshowtxt)!=0)
  212. {
  213. if(myResM->textflage[0]!='1')
  214. {
  215. BitBlt(hdc,0,0,WinWidth,WinHeight,BackDC,0,0,SRCCOPY);
  216. TextOut(hdc,5,0, showtxt,strlen(showtxt));
  217. }
  218. else
  219. {
  220. OutTextPic(showtxt,0,0,hdc,txtpic.hdc,10,20,30,1,myResM->reBackDC);
  221. }
  222. strcmp(Saveshowtxt,showtxt);
  223. }
  224. BeginPaint(m_hWnd, &ps);
  225.     WindDC=GetDC(m_hWnd);
  226. BitBlt(WindDC,0,0,WinWidth,WinHeight,hdc,0,0,SRCCOPY);
  227.     ReleaseDC(m_hWnd,WindDC);
  228. EndPaint(m_hWnd, &ps);
  229. }
  230. int inputHwnd::RedrawWin()
  231. {
  232. RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
  233. return 0;
  234. }
  235. int inputHwnd::OutTextPic(char *ss,long x,long y,HDC hDC1,HDC hDC2, long dw , long dh,double a, double zoom,HDC backhdc)
  236. {
  237. int i,index;
  238. static myHDC tt,temtt,pp;
  239. static int flage=0;
  240. char temss[100];
  241. long r,outdw;
  242. strcpy(temss,ss);
  243. r=(long)sqrt(dw*dw+dh*dh);
  244. outdw=dw*2*strlen(temss)+r*2;
  245. {
  246. HBRUSH hBrush;
  247. HRGN hRgn;
  248. hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
  249. hRgn=CreateRectRgn(0,0,1000,1000);
  250. FillRgn(hDC1,hRgn, hBrush);
  251. FillRgn(temtt.hdc ,hRgn, hBrush);
  252. DeleteObject (hRgn);
  253. }
  254. BitBlt(hDC1,0,0,110,ControlHeight,backhdc,0,0,SRCCOPY);
  255. if(flage==0)
  256. {
  257. tt.StartHDC(hDC2,800,800);
  258. temtt.StartHDC(hDC1,800,800);
  259. pp.StartHDC(hDC1,800,800);
  260. flage=1;
  261. }
  262. strupr(temss);
  263. for(i=0;i<(int)strlen(temss);i++)
  264. {
  265. index=temss[i]-65;
  266. OutPicture(r,r,tt.hdc,dw*2*index,0,hDC2,dw,dh,a);
  267. TransparentBlt2(temtt.hdc,dw*2*i,0,dw*2+r,r*2,tt.hdc,0,0,dw*2+r,r*2,RGB(255,255,255));
  268. //BitBlt(temtt.hdc,dw*2*i,0,dw*2+r,r*2,tt.hdc,0,0,SRCAND);//
  269. }
  270. BitBlt(pp.hdc,x,y,outdw,r*2,backhdc,x,y,SRCCOPY);
  271. TransparentBlt2(pp.hdc,0,0,(int)(outdw*zoom),(int)(r*2*zoom),temtt.hdc,0,0,outdw,r*2,RGB(255,255,255));
  272. BitBlt(hDC1,x,y,(int)((outdw-r)*zoom),(int)(r*2*zoom),pp.hdc,0,0,SRCCOPY);
  273. return 0;
  274. }
  275. //返回所选菜单项
  276. long inputHwnd::Popup()
  277. {
  278.   long iMenu,  nMenus,result;//retabout;
  279.   POINT p ;
  280.   char buffer [256]="";
  281.   long flag=0;
  282.   HMENU hMenu;
  283.   int CurentKbdNum=0;
  284.   char MenuName[20][256]={"特效文本","特效显示","旋转角度","自动旋转","背景设置"};
  285.     GetCursorPos(&p);
  286.     hMenu = CreatePopupMenu();
  287.     nMenus = 5;
  288.     for(iMenu = 1 ;iMenu<=nMenus;iMenu++)
  289. {
  290. switch (iMenu)
  291. {
  292. case 1:
  293. if(myResM->textflage[0]=='1')
  294. flag=MF_ENABLED|MF_CHECKED;
  295. else
  296. flag=MF_ENABLED;
  297. break;
  298. default:
  299.         flag=MF_GRAYED|MF_DISABLED;;
  300. break;
  301. }
  302.    AppendMenu (hMenu, flag, iMenu, MenuName[iMenu-1]);
  303.     }
  304.     iMenu = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON + TPM_LEFTALIGN + TPM_NONOTIFY + TPM_RETURNCMD, p.x, p.y, 0, GetForegroundWindow(), 0);
  305.     //得到菜单的选择字符串
  306.     result = GetMenuString(hMenu, (iMenu - 1), buffer, strlen(buffer), MF_BYPOSITION);
  307.     
  308.     DestroyMenu(hMenu);
  309. if(iMenu>0)
  310. {
  311. switch (iMenu)
  312. {
  313. case 1:
  314. {
  315. RECT Rect;
  316. GetWindowRect(m_hWnd,&Rect); 
  317. if(myResM->textflage[0]=='1')
  318. {
  319. ControlHeight=24;
  320. RegSaveString("textflage","0");
  321. myResM->textflage[0]='0';
  322. MoveWindow(m_hWnd,Rect.left,Rect.top,WinWidth,ControlHeight,TRUE);
  323. }
  324. else
  325. {
  326. ControlHeight=35;
  327. RegSaveString("textflage","1");
  328. myResM->textflage[0]='1';
  329. MoveWindow(m_hWnd,Rect.left,Rect.top,WinWidth,ControlHeight,TRUE);
  330. }
  331. }
  332. break;
  333. default:
  334. MessageBox(0,"过两天补上","功能暂时未编码",0);
  335. break;
  336. }
  337. }
  338. return  0;
  339. }