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

输入法编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "inputHwnd.h"
  4. #include "windows.h"
  5. #include "CMyWnd.h"
  6. BOOL MyIsWinNT() ;
  7. inputHwnd::inputHwnd()
  8. {
  9. POINT pp[100];
  10. showtxt[0]=0;
  11. fCanMove=0;
  12. WinStartPos=myResM.ReadRegPos("inputHwnd");
  13. WinHeight=myResM.WinHeight;
  14. WinWidth=myResM.WinWidth;
  15. ControlHeight=myResM.ControlHeight;
  16. pp[0].x=0; pp[0].y=ControlHeight;
  17. pp[1].x=94; pp[1].y=ControlHeight;
  18. pp[2].x=70; pp[2].y=0;
  19. pp[3].x=0; pp[3].y=0;
  20. WinRGN1=CreatePolygonRgn(pp,4,1);
  21. WinRGN2=CreatePolygonRgn(pp,4,1);
  22. hUIFont = CreateFont(20, 0, 0, 0, FW_NORMAL,
  23. FALSE, FALSE, FALSE, ANSI_CHARSET,
  24. OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
  25. DEFAULT_PITCH, "宋体");
  26. }
  27.  inputHwnd::~inputHwnd()
  28.  {
  29. SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  30. DestroyWindow(m_hWnd);
  31. // MessageBox(0,"~inputHwnd",0,0);
  32. m_hWnd=NULL;
  33. DeleteObject(hUIFont);
  34. DeleteObject (WinRGN1);
  35. //如果不消毁窗口当主程序,退出时窗口依然在运行,会引起系统崩溃
  36.  }
  37.  
  38. BOOL inputHwnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
  39. {
  40. // 初始化全局字串
  41. WNDCLASSEX wcex;
  42. wcex.cbSize = sizeof(WNDCLASSEX); 
  43. wcex.style = CS_IME;
  44. wcex.lpfnWndProc = InitProc;
  45. wcex.cbClsExtra = 0;
  46. wcex.cbWndExtra = 12;
  47. wcex.hInstance = hInstance;
  48. wcex.hIcon = 0;
  49. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  50. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  51. wcex.lpszMenuName = 0;
  52. wcex.lpszClassName = szClassName;
  53. wcex.hIconSm = 0;
  54. hInst=hInstance;
  55. RegisterClassEx(&wcex);
  56. {
  57.    m_hWnd=CreateWindowEx( 0,
  58. szClassName,NULL,
  59. WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
  60. WinStartPos.x,WinStartPos.y,WinWidth,ControlHeight,
  61. hWndParent,NULL,hInstance,this);
  62. ShowWindow(m_hWnd,SW_SHOWNOACTIVATE);
  63. UpdateWindow(m_hWnd);
  64.    //使白色透明掉
  65. }
  66. return m_hWnd != NULL;
  67. }
  68.  LRESULT WINAPI inputHwnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
  69.  {
  70.   POINT pt;
  71.   long x,y;
  72.   static int tt;
  73.   try {
  74.     switch (uMsg)
  75.     {
  76. case WM_CREATE:
  77. myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  78. initalizeWin();
  79. myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
  80.   break;
  81. case WM_PAINT://绘制窗口
  82.   myPaintTxt(0,0);
  83. break;
  84.         case WM_SETCURSOR:
  85. if ((HIWORD(lParam) == WM_MOUSEMOVE))
  86. {
  87. GetCursorPos( &pt );
  88. GetWindowRect(m_hWnd,&WinRect);
  89. WinSIZE.cx = WinRect.right - WinRect.left;
  90. WinSIZE.cy = WinRect.bottom - WinRect.top;
  91. Cursordif.x = pt.x - WinRect.left;//得到相对位置
  92. Cursordif.y = pt.y - WinRect.top;
  93. SetCapture(m_hWnd);
  94. //SetWindowLong(m_hWnd,4,1);//GWL_HINSTANCE DWL_MSGRESULT
  95. SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(32649)));
  96. }
  97. else 
  98. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  99. break;
  100.         case WM_LBUTTONDOWN:
  101. fCanMove = TRUE;
  102. break;
  103.         case WM_LBUTTONUP:
  104. if( fCanMove) 
  105. {
  106. GetCursorPos( &pt );
  107. myResM.WriteRegPos("inputHwnd",pt);
  108. MoveWindow(m_hWnd,pt.x - Cursordif.x,pt.y - Cursordif.y,WinSIZE.cx,WinSIZE.cy,TRUE);
  109. fCanMove = FALSE;
  110. }
  111. myPaintTxt(0,0);
  112. break;
  113.         
  114.         case WM_MOUSEMOVE:
  115. GetCursorPos( &pt );//取得光标位置
  116. y=lParam/0x10000;
  117. x=lParam%0x10000;
  118. if(fCanMove==TRUE)
  119. {
  120. MoveWindow(m_hWnd,pt.x - Cursordif.x, pt.y - Cursordif.y, WinSIZE.cx,WinSIZE.cy,TRUE);
  121. myPaintTxt(0,0);
  122. //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
  123. //在不移动时可以使用它。
  124. }
  125.    //测试鼠标是否移出窗口
  126. if(PtInRegion(WinRGN2,x,y)==0)
  127. {
  128. myPaintTxt(0,0);
  129. }
  130. if(fCanMove==FALSE )
  131. {   
  132. //SetWindowLong(m_hWnd,4,0);
  133. ReleaseCapture();
  134. }
  135. break;
  136. case WM_DESTROY:
  137. //SendMessage(m_hWnd,WM_CLOSE,0,0);
  138. if(myHwndParent==NULL)
  139. {
  140. //PostQuitMessage(0);
  141. }
  142. else
  143. {
  144. SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
  145. DestroyWindow(m_hWnd);
  146. }
  147. break;
  148.         default:
  149.                 return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  150.             break;
  151.     }
  152.   } catch (...) {
  153.   if(IsWindow(m_hWnd))
  154.   {
  155.   ShowWindow(m_hWnd,SW_SHOW);
  156. //   MessageBox(0,"inputError","IsWindow",0);
  157.   }
  158. //   delete (CMyWnd*)Main_CWnd;
  159. ///   SetWindowLong(*Main_hWnd,GWL_WNDPROC,(long)endProc);
  160. //   DestroyWindow(*Main_hWnd);
  161. //   SetWindowLong(*inpu_hWnd,GWL_WNDPROC,(long)endProc);
  162. //   DestroyWindow(*inpu_hWnd);
  163. //   SetWindowLong(*Cont_hWnd,GWL_WNDPROC,(long)endProc);
  164. //   DestroyWindow(*Cont_hWnd);
  165. //   MessageBox(0,"inputError","有错误发生",0);
  166.   }
  167.     return 0;
  168. }
  169. //窗口的初始化
  170. inputHwnd::initalizeWin()
  171. {
  172. WindDC=GetDC(m_hWnd);
  173. Texthdc = CreateCompatibleDC(WindDC);
  174. Texthdcbmp=CreateCompatibleBitmap(WindDC,WinWidth,ControlHeight);
  175. SelectObject(Texthdc, hUIFont);
  176. DeleteObject(SelectObject(Texthdc,Texthdcbmp));
  177. SetBkMode(Texthdc,TRANSPARENT);
  178. SelectObject(WindDC, hUIFont);
  179. TextH=GetTabbedTextExtent(WindDC,"1",1,0,0)/0x10000; 
  180.     //
  181. if(!MyIsWinNT())
  182. SetWindowRgn(m_hWnd,WinRGN1,TRUE);
  183. BackhBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCE(IDB_Back));
  184. BackDC = CreateCompatibleDC(WindDC);
  185. DeleteObject(SelectObject(BackDC,BackhBmp));
  186. }
  187. int inputHwnd::myPaintTxt(int flag,int act )
  188. {
  189.   PAINTSTRUCT ps;
  190.   HDC hdc;
  191.     //清除掉文本区
  192. hdc=Texthdc;
  193. BitBlt(hdc,0,0,WinWidth,WinHeight,BackDC,0,WinHeight-ControlHeight,SRCCOPY);
  194. TextOut(hdc,5,0, showtxt,strlen(showtxt));
  195. /*
  196. HRGN Rgn1, Rgn2;
  197.     Rgn1 = CreateRectRgn(0, 0, 100, 300);
  198.     BeginPath(WindDC);
  199.       TextOut(hdc,5,0, showtxt,strlen(showtxt));
  200.     EndPath(WindDC);
  201.     Rgn2 = PathToRegion(WindDC);
  202.   
  203.     CombineRgn(Rgn1, Rgn2, WinRGN1, 2);
  204.     SetWindowRgn(WindDC, hRgn, TRUE);
  205.     DeleteObject(hRgn);
  206. */
  207. BeginPaint(m_hWnd, &ps);
  208. BitBlt(WindDC,0,0,WinWidth,WinHeight,hdc,0,0,SRCCOPY);
  209. EndPaint(m_hWnd, &ps);
  210. }
  211. int inputHwnd::RedrawWin()
  212. {
  213. RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
  214. return 0;
  215. }
  216. //判断系统是否NT
  217. BOOL MyIsWinNT() 
  218. {
  219.    OSVERSIONINFO osvi;
  220.    osvi.dwOSVersionInfoSize = 148;
  221.    GetVersionEx(&osvi);
  222.   if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
  223.   return 1;
  224.   else
  225.       return 0;
  226. }