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

输入法编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "QQHwnd.h"
  4. #include "windows.h"
  5. #include "CMyWnd.h"
  6. QQHwnd::QQHwnd(void*CMthis)
  7. {
  8. POINT pp[100];
  9. Main_CWnd=(CMyWnd*)CMthis;
  10. strcpy(showtxt,"");
  11. fCanMove=0;
  12. dailtimershow=0;
  13. WinWidth=200;
  14. WinHeight=20;
  15. pp[0].x=0; pp[0].y=20;
  16. pp[1].x=170; pp[1].y=20;
  17. pp[2].x=170; pp[2].y=0;
  18. pp[3].x=0; pp[3].y=0;
  19. WinRGN1=CreatePolygonRgn(pp,4,1);
  20. WinRGN2=CreatePolygonRgn(pp,4,1);
  21.  
  22. if(ValIsWinNT==1)
  23. {
  24. hUIFont = CreateFont(12, 0, 0, 0, FW_NORMAL,
  25. FALSE, FALSE, FALSE, ANSI_CHARSET,
  26. OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
  27. DEFAULT_PITCH, "宋体");
  28. }
  29. else
  30. {
  31. hUIFont = CreateFont(16, 0, 0, 0, FW_NORMAL,
  32. FALSE, FALSE, FALSE, ANSI_CHARSET,
  33. OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
  34. DEFAULT_PITCH, "宋体");
  35. }
  36. }
  37.  QQHwnd::~QQHwnd()
  38.  {
  39. try{
  40. // MessageBox(0,"~inputHwnd",0,0);
  41. DeleteObject(hUIFont);
  42. DeleteObject (WinRGN1);
  43. DeleteObject (WinRGN2);
  44. //可能在窗口处理程序中本窗口已被删除。
  45. //让程序暂时进行处理,以防止无效访问已经退出的代码段
  46. if(IsWindow(m_hWnd)){
  47. ReleaseDC(m_hWnd,WindDC);
  48. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  49. DestroyWindow(m_hWnd); 
  50. //m_hWnd=NULL;
  51. }
  52.   } catch (...) {
  53.        MessageBox(0,"~inputHwnd","有错误发生",0);
  54.   }
  55. //如果不消毁窗口当主程序,退出时窗口依然在运行,会引起系统崩溃
  56.  }
  57.  
  58. BOOL QQHwnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
  59. {
  60. // 初始化全局字串
  61. WNDCLASSEX wcex;
  62. wcex.cbSize = sizeof(WNDCLASSEX); 
  63. wcex.style = CS_IME;
  64. wcex.lpfnWndProc = InitProc;
  65. wcex.cbClsExtra = 0;
  66. wcex.cbWndExtra = 12;
  67. wcex.hInstance = hInstance;
  68. wcex.hIcon = 0;
  69. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  70. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  71. wcex.lpszMenuName = 0;
  72. wcex.lpszClassName = szClassName;
  73. wcex.hIconSm = 0;
  74. hInst=hInstance;
  75. RegisterClassEx(&wcex);
  76. {
  77.    m_hWnd=CreateWindowEx( 0,
  78. szClassName,NULL,
  79. WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
  80. 0,0,WinWidth,WinHeight,
  81. hWndParent,NULL,hInstance,this);
  82. ShowWindow(m_hWnd,SW_HIDE);
  83. //UpdateWindow(m_hWnd);
  84. }
  85. myHwndParent=hWndParent;
  86. return m_hWnd != NULL;
  87. }
  88.  LRESULT WINAPI QQHwnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
  89.  {
  90.   POINT pt;
  91.   long x,y;
  92.   static int tt;
  93.   try {
  94.     switch (uMsg)
  95.     {
  96. case WM_CREATE:
  97. //myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  98. initalizeWin();
  99. //myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  100.   break;
  101. case WM_PAINT://绘制窗口
  102.   myPaintTxt(0,0);
  103. break;
  104.         case WM_SETCURSOR:
  105. if ((HIWORD(lParam) == WM_MOUSEMOVE))
  106. {
  107. ShowWindow(m_hWnd, SW_HIDE);//收到鼠标立即隐藏
  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_LBUTTONDOWN:
  122. fCanMove = TRUE;
  123. break;
  124.         case WM_LBUTTONUP:
  125. if( fCanMove) 
  126. {
  127. GetCursorPos( &pt );
  128. pt.x=pt.x - Cursordif.x;
  129. pt.y=pt.y - Cursordif.y;
  130. MoveWindow(m_hWnd,pt.x ,pt.y ,WinSIZE.cx,WinSIZE.cy,TRUE);
  131. fCanMove = FALSE;
  132. }
  133. myPaintTxt(0,0);
  134. break;
  135.         
  136.         case WM_MOUSEMOVE:
  137. GetCursorPos( &pt );//取得光标位置
  138. y=lParam/0x10000;
  139. x=lParam%0x10000;
  140. if(fCanMove==TRUE)
  141. {
  142. MoveWindow(m_hWnd,pt.x - Cursordif.x, pt.y - Cursordif.y, WinSIZE.cx,WinSIZE.cy,TRUE);
  143. myPaintTxt(0,0);
  144. //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
  145. //在不移动时可以使用它。
  146. }
  147.    //测试鼠标是否移出窗口
  148. if(PtInRegion(WinRGN2,x,y)==0)
  149. {
  150. myPaintTxt(0,0);
  151. }
  152. if(fCanMove==FALSE )
  153. {   
  154. //SetWindowLong(m_hWnd,4,0);
  155. ReleaseCapture();
  156. }
  157. break;
  158. case WM_DESTROY:
  159. //SendMessage(m_hWnd,WM_CLOSE,0,0);
  160. if(myHwndParent==NULL)
  161. {
  162. //PostQuitMessage(0);
  163. }
  164. else
  165. {
  166. if(IsWindow(m_hWnd))
  167. //SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  168. DestroyWindow(m_hWnd);
  169. }
  170. break;
  171.         default:
  172.                 return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  173.             break;
  174.     }
  175.   } catch (...) {
  176.   MessageBox(0,"QQHwnd","IsWindow",0);
  177.   }
  178.     return 0;
  179. }
  180. //窗口的初始化
  181. QQHwnd::initalizeWin()
  182. {
  183. WindDC=GetDC(m_hWnd);
  184. DeleteObject(SelectObject(WindDC, hUIFont));
  185. SetBkMode(WindDC,TRANSPARENT);
  186.     //
  187. if(!MyIsWinNT())
  188. SetWindowRgn(m_hWnd,WinRGN1,TRUE);
  189. }
  190. int QQHwnd::myPaintTxt(int flag,int act )
  191. {
  192.   PAINTSTRUCT ps;
  193.     //清除掉文本区
  194. BeginPaint(m_hWnd, &ps);
  195.     if(ValIsWinNT==1)
  196. {
  197. BitBlt(WindDC,0,0,WinWidth,WinHeight ,((CMyWnd*)Main_CWnd)->myResM.oldBackDC,10, 0,SRCCOPY);
  198. TextOut(WindDC,2,2, showtxt,strlen(showtxt));
  199. }
  200. else
  201. {
  202. WindDC=GetDC(m_hWnd);
  203. SetBkMode(WindDC,TRANSPARENT);
  204. DeleteObject(SelectObject(WindDC, hUIFont));
  205. BitBlt(WindDC,0,0,WinWidth,WinHeight ,((CMyWnd*)Main_CWnd)->myResM.oldBackDC,10, 0,SRCCOPY);
  206. TextOut(WindDC,2,2, showtxt,strlen(showtxt));
  207.     ReleaseDC(m_hWnd,WindDC);
  208. }
  209. EndPaint(m_hWnd, &ps);
  210. }
  211. //设置需要显示的字符串
  212. // flage  1234上下左右
  213. int QQHwnd:: settxt(HWND phwnd,long x,long y,char *ss,int flage)
  214. {
  215. int len,line=0,t;
  216. char *ppc;
  217. long h,w;
  218. static int timenum=0;
  219. SIZE sz;
  220. RECT FullWinRect,phwndWinRect,CurWinRect;
  221. GetWindowRect(GetDesktopWindow(),&FullWinRect);
  222. GetWindowRect(phwnd,&phwndWinRect); 
  223. GetWindowRect(m_hWnd,&CurWinRect); 
  224. dailtimershow=5;
  225. //MessageBox(0,"inputError","IsWindow",0);
  226. if(ss!=NULL)
  227. strcpy(showtxt,ss);
  228. else
  229. {
  230. timenum=0;
  231. ShowWindow(m_hWnd, SW_HIDE);
  232. showtxt[0]=0;
  233. }
  234. if(showtxt[0]==0)
  235. {
  236. timenum=0;
  237. ShowWindow(m_hWnd, SW_HIDE);
  238. }
  239. else
  240. {
  241. if(timenum<5)
  242. {
  243. timenum++;
  244. return 0;
  245. }
  246. else
  247. {
  248. ppc=showtxt;
  249. len=strlen(ppc);
  250. if(ValIsWinNT==1)
  251. {
  252. GetTextExtentExPoint(WindDC,showtxt,len,100,&t,0,&sz);
  253. }
  254. else
  255. {
  256. WindDC=GetDC(m_hWnd);
  257. DeleteObject(SelectObject(WindDC, hUIFont));
  258. GetTextExtentExPoint(WindDC,showtxt,len,100,&t,0,&sz);
  259.     ReleaseDC(m_hWnd,WindDC);
  260. }
  261. w=sz.cx+4;
  262. h=sz.cy+4;
  263. switch(flage)
  264. {
  265. case 1:
  266. y=y+phwndWinRect.top-20;
  267. x=x+phwndWinRect.left;
  268. break;
  269. case 2:
  270. y=y+phwndWinRect.top+20;
  271. x=x+phwndWinRect.left;
  272. break;
  273. case 3:
  274. y=y+phwndWinRect.top;
  275. x=x+phwndWinRect.left-(CurWinRect.right-CurWinRect.left)-20;
  276. break;
  277. case 4:
  278. y=y+phwndWinRect.top;
  279. x=x+phwndWinRect.left+20;
  280. break;
  281. default:
  282. break;
  283. }
  284. // InvalidateRect(m_hWnd,NULL,FALSE);
  285. MoveWindow(m_hWnd,x,y,w,h,TRUE);
  286. myPaintTxt(0,0 );
  287. ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  288. }
  289. }
  290. return 0;
  291. }
  292. int QQHwnd:: disabledailtimer()
  293. {
  294. if(dailtimershow>0)
  295. dailtimershow--;
  296. return dailtimershow;
  297. }
  298. int QQHwnd:: settxttimer(HWND phwnd,long x,long y,char *ss)
  299. {
  300. int len,line=0,t;
  301. char *ppc;
  302. long h,w;
  303. SIZE sz;
  304. if(dailtimershow==0)
  305. {
  306. RECT FullWinRect,phwndWinRect,CurWinRect;
  307. GetWindowRect(GetDesktopWindow(),&FullWinRect);
  308. GetWindowRect(phwnd,&phwndWinRect); 
  309. GetWindowRect(m_hWnd,&CurWinRect); 
  310. //MessageBox(0,"inputError","IsWindow",0);
  311. if(ss!=NULL)
  312. strcpy(showtxt,ss);
  313. else
  314. {
  315. ShowWindow(m_hWnd, SW_HIDE);
  316. showtxt[0]=0;
  317. }
  318. if(showtxt[0]==0)
  319. {
  320. ShowWindow(m_hWnd, SW_HIDE);
  321. }
  322. else
  323. {
  324. ppc=showtxt;
  325. len=strlen(ppc);
  326. if(ValIsWinNT==1)
  327. {
  328. GetTextExtentExPoint(WindDC,showtxt,len,100,&t,0,&sz);
  329. }
  330. else
  331. {
  332. WindDC=GetDC(m_hWnd);
  333. DeleteObject(SelectObject(WindDC, hUIFont));
  334. GetTextExtentExPoint(WindDC,showtxt,len,100,&t,0,&sz);
  335.     ReleaseDC(m_hWnd,WindDC);
  336. }
  337. w=sz.cx+4;
  338. h=sz.cy+4;
  339. y=y+phwndWinRect.top-h;
  340. x=x+phwndWinRect.left;
  341. MoveWindow(m_hWnd,x,y,w,h,TRUE);
  342. myPaintTxt(0,0 );
  343. ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  344. }
  345. }
  346. return 0;
  347. }
  348. //设置需要显示的字符串
  349. int QQHwnd:: settxt(char *ss)
  350. {
  351. int len,line=0,t;
  352. char *ppc;
  353. long h,w;
  354. SIZE sz;
  355. RECT CurWinRect;
  356. GetWindowRect(m_hWnd,&CurWinRect); 
  357. //MessageBox(0,"inputError","IsWindow",0);
  358. if(ss!=NULL)
  359. strcpy(showtxt,ss);
  360. else
  361. {
  362. ShowWindow(m_hWnd, SW_HIDE);
  363. showtxt[0]=0;
  364. }
  365. if(showtxt[0]==0)
  366. {
  367. ShowWindow(m_hWnd, SW_HIDE);
  368. }
  369. else
  370. {
  371. ppc=showtxt;
  372. len=strlen(ppc);
  373. if(ValIsWinNT==1)
  374. {
  375. GetTextExtentExPoint(WindDC,showtxt,len,100,&t,0,&sz);
  376. }
  377. else
  378. {
  379. WindDC=GetDC(m_hWnd);
  380. DeleteObject(SelectObject(WindDC, hUIFont));
  381. GetTextExtentExPoint(WindDC,showtxt,len,100,&t,0,&sz);
  382.     ReleaseDC(m_hWnd,WindDC);
  383. }
  384. w=sz.cx+4;
  385. h=sz.cy+4;
  386. MoveWindow(m_hWnd,CurWinRect.left,CurWinRect.top,w,h,TRUE);
  387. myPaintTxt(0,0);
  388. }
  389. return 0;
  390. }
  391. int QQHwnd::RedrawWin()
  392. {
  393. RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
  394. return 0;
  395. }
  396. //判断系统是否NT