程序效果:

猫的眼睛一直跟着鼠标移动:

鼠标经过猫的右脚附近时,猫会抓住鼠标。(未使用Hook)

代码:

  1. //main.cpp
    1 #include <windows.h>
  2. #include <math.h>
  3. //#include <iostream>
  4. //using namespace std;
  5. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  6. double WIDTH=,HEIGHT=;
  7. double px=0.0,py=0.0;
  8. double ppx=0.0,ppy=0.0;
  9. POINT sp;
  10. int cat=;
  11. double width,height;
  12. PAINTSTRUCT ps ;
  13. RECT rect ;
  14. POINT pts[];
  15. double left_eye_px=0.0,right_eye_px=0.0;
  16. double eye_py=0.0;
  17. double eye_r=0.0;
  18. double k=0.0;
  19. double ball_hr=0.0;//半径
  20. double deltax=0.0;
  21. POINT p,pp;
  22. int initGraph=;
  23. LPPOINT catp;
  24. RECT eye_rect;
  25. HBRUSH gray_brush =CreateSolidBrush (RGB(,,));
  26. HBRUSH white_brush =CreateSolidBrush (RGB(,,));
  27. HBRUSH black_brush =CreateSolidBrush (RGB(,,));
  28. int drawRound(HDC hdc,int x,int y,int r) //r 直径
  29. {
  30. Ellipse(hdc,x-r/,y-r/,x+r/,y+r/);
  31. return ;
  32. }
  33. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  34. PSTR szCmdLine, int iCmdShow)
  35. {
  36. static TCHAR szAppName[] = TEXT ("HelloWin") ;
  37. HWND hwnd ;
  38. MSG msg ;
  39. WNDCLASS wndclass ;
  40. wndclass.style = CS_HREDRAW | CS_VREDRAW ;
  41. wndclass.lpfnWndProc = WndProc ;
  42. wndclass.cbClsExtra = ;
  43. wndclass.cbWndExtra = ;
  44. wndclass.hInstance = hInstance ;
  45. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  46. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
  47. wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  48. wndclass.lpszMenuName = NULL ;
  49. wndclass.lpszClassName = szAppName ;
  50. if (!RegisterClass (&wndclass))
  51. {
  52. MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  53. szAppName, MB_ICONERROR) ;
  54. return ;
  55. }
  56. hwnd = CreateWindow (szAppName, // window class name
  57. TEXT ("Mouse Hook"), // window caption
  58. WS_OVERLAPPEDWINDOW, // window style
  59. CW_USEDEFAULT, // initial x position
  60. CW_USEDEFAULT, // initial y position
  61. WIDTH, // initial x size
  62. HEIGHT, // initial y size
  63. NULL, // parent window handle
  64. NULL, // window menu handle
  65. hInstance, // program instance handle
  66. NULL) ; // creation parameters
  67. ShowWindow (hwnd, iCmdShow) ;
  68. UpdateWindow (hwnd) ;
  69. while (GetMessage (&msg, NULL, , ))
  70. {
  71. TranslateMessage (&msg) ;
  72. DispatchMessage (&msg) ;
  73. }
  74. return msg.wParam ;
  75. }
  76.  
  77. int init(HWND hwnd)
  78. {
  79. GetClientRect (hwnd, &rect) ;
  80. width=rect.right-rect.left ;
  81. height=rect.bottom-rect.top ;
  82. left_eye_px=width*/-height/;
  83. right_eye_px=width*/+height/;
  84. eye_py=height*/-height/;
  85. eye_r=height/;
  86. ball_hr=eye_r/;//半径
  87. px=left_eye_px;
  88. py=eye_py;
  89. ppx=right_eye_px;
  90. ppy=eye_py;
  91. eye_rect.left=left_eye_px-eye_r/;
  92. eye_rect.right=right_eye_px+eye_r/;
  93. eye_rect.top=eye_py-eye_r/;
  94. eye_rect.bottom=eye_py+eye_r/;
  95. return ;
  96. }
  97. int drawHand(HDC hdc)
  98. {
  99. /**< 画嘴 */
  100. if(cat==)
  101. {
  102. MoveToEx (hdc, width/+width/,height/*, NULL) ;
  103. LineTo (hdc, width*/-width/, height/*) ;
  104. SelectObject (hdc,gray_brush ) ;
  105. Ellipse(hdc,width/-width/,height/*,width/+width/,height*1.1);//左胳膊
  106. Ellipse(hdc,width/*-width/,height/*,width/*+width/,height*1.1);//右胳膊
  107. return ;
  108. }
  109. SelectObject (hdc, gray_brush) ;
  110. Ellipse(hdc,width/*-width/,height/*,width/*+width/,height*1.1);//右胳膊
  111. Ellipse(hdc,width/-width/,height-height/,width/+width/,height);//左胳膊
  112. /**< 画脚掌 */
  113. Ellipse(hdc,width/-width/,height-height/,width/+width/,height);
  114. MoveToEx (hdc, width/-width/,height-height/, NULL) ;
  115. LineTo (hdc, width/-width/, height-height/) ;
  116. MoveToEx (hdc, width/,height-height/, NULL) ;
  117. LineTo (hdc, width/, height-height/) ;
  118. MoveToEx (hdc, width/+width/,height-height/, NULL) ;
  119. LineTo (hdc, width/+width/, height-height/) ;
  120. Arc( hdc, width/-width/, height-height/,width/+width/, height,width/+width/, height,width/-width/, height);
  121. /**< 画嘴 */
  122. Arc( hdc, width*/+width/, height/*,width*/-width/, height/*,width*/, height/*, width*/, height/*);
  123. return ;
  124. }
  125. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  126. {
  127. POINT pc;
  128. HDC hdc;
  129. switch (message)
  130. {
  131. case WM_CREATE:
  132. init(hwnd);
  133. initGraph =;
  134. InvalidateRect (hwnd,NULL, true) ;
  135. return ;
  136. case WM_SIZE:
  137. init(hwnd);
  138. initGraph =;
  139. InvalidateRect (hwnd,NULL, true) ;
  140. return ;
  141. case WM_MOVE:
  142. return ;
  143. case WM_LBUTTONDOWN:
  144. GetCursorPos(&pc);
  145. if(pc.x==sp.x&&pc.y==sp.y)
  146. {
  147. cat=;
  148. SetCursorPos(sp.x,sp.y-height/);
  149. ReleaseCapture () ;
  150. initGraph=;
  151. InvalidateRect (hwnd,NULL, true) ;
  152. }
  153. return ;
  154. case WM_MOUSEMOVE:
  155. if(cat==)
  156. {
  157. SetCursorPos(sp.x,sp.y);
  158. return ;
  159. }
  160. GetCursorPos(&p);
  161. pp=p;
  162. ScreenToClient(hwnd,&pp);
  163. if((pp.x>width/-width/)&&(pp.x<width/+width/)&&(pp.y>height-height/)&&(pp.y<height))
  164. {
  165. SetCapture (hwnd) ;
  166. sp.x=width/;
  167. sp.y=height-height/;
  168. ClientToScreen(hwnd,&sp);
  169. cat=;
  170. initGraph=;
  171. InvalidateRect (hwnd,NULL, true) ;
  172. return ;
  173. }
  174. k=999.0;
  175. if(pp.x-left_eye_px!=)
  176. {
  177. k=(pp.y-eye_py)/(pp.x-left_eye_px);
  178. }
  179. deltax=(ball_hr/)/(+k*k);
  180. deltax=sqrt(deltax);
  181. if(!(pp.x>left_eye_px))
  182. {
  183. deltax=(-1.0)*deltax;
  184. }
  185. px=left_eye_px+deltax*;
  186. deltax=(ball_hr/)/(+/(k*k));
  187. deltax=sqrt(deltax);
  188. if(!(pp.y>eye_py))
  189. {
  190. deltax=(-1.0)*deltax;
  191. }
  192. py=eye_py+deltax*;
  193. k=999.0;
  194. if(pp.x-right_eye_px!=)
  195. {
  196. k=(pp.y-eye_py)/(pp.x-right_eye_px);
  197. }
  198. deltax=(ball_hr/)/(+k*k);
  199. deltax=sqrt(deltax);
  200. if(!(pp.x>right_eye_px))
  201. {
  202. deltax=(-1.0)*deltax;
  203. }
  204. ppx=right_eye_px+deltax*;
  205. deltax=(ball_hr/)/(+/(k*k));
  206. deltax=sqrt(deltax);
  207. if(!(pp.y>eye_py))
  208. {
  209. deltax=(-1.0)*deltax;
  210. }
  211. ppy=eye_py+deltax*;
  212. InvalidateRect (hwnd, &eye_rect, false) ;
  213. return ;
  214. case WM_PAINT:
  215. hdc=BeginPaint (hwnd,&ps) ;
  216. if(initGraph==)
  217. {
  218. SelectObject (hdc, gray_brush) ;
  219. Ellipse(hdc,width/,height/,width-width/,height*/);//身体
  220. /**< 画脸 */
  221. Ellipse(hdc,,height/,width*/,height*/);
  222. /**< 画耳朵 */
  223. pts[].x =width/;
  224. pts[].y =height/;
  225. pts[].x =width/;
  226. pts[].y =height/*;
  227. pts[].x =width/*;
  228. pts[].y =height/*;
  229. Polygon (hdc, pts, ) ;
  230. pts[].x =width/*;
  231. pts[].y =height/;
  232. pts[].x =width/*;
  233. pts[].y =height/*;
  234. pts[].x =width/*;
  235. pts[].y =height/*;
  236. Polygon (hdc, pts, ) ;
  237. /**< 画胡子 */
  238. MoveToEx (hdc, , height/*, NULL) ;
  239. LineTo (hdc, width/*,height/*) ;
  240. MoveToEx (hdc, , height/*, NULL) ;
  241. LineTo (hdc, width/*,height/*) ;
  242. MoveToEx (hdc, , height/*, NULL) ;
  243. LineTo (hdc, width/*,height/*) ;
  244. MoveToEx (hdc, width, height/*, NULL) ;
  245. LineTo (hdc, width/*,height/*) ;
  246. MoveToEx (hdc, width, height/*, NULL) ;
  247. LineTo (hdc, width/*,height/*) ;
  248. MoveToEx (hdc, width, height/*, NULL) ;
  249. LineTo (hdc, width/*,height/*) ;
  250. drawHand(hdc);
  251. initGraph=;
  252. }
  253. /**< 画眼睛 */
  254. SelectObject (hdc, white_brush) ;
  255. drawRound(hdc,left_eye_px,eye_py,eye_r);
  256. drawRound(hdc,right_eye_px,eye_py,eye_r);
  257. SelectObject (hdc, black_brush) ;
  258. drawRound(hdc,px,py,ball_hr*);
  259. drawRound(hdc,ppx,ppy,ball_hr*);
  260. EndPaint (hwnd, &ps) ;
  261. return ;
  262. case WM_DESTROY:
  263. PostQuitMessage () ;
  264. return ;
  265. }
  266. return DefWindowProc (hwnd, message, wParam, lParam) ;
  267. }

程序写于大三上学期,Windows程序设计 课程考核作业。

2016.4.12更新博客。

END

抓鼠标的猫(Win32实现,Codeblocks+GCC编译)的更多相关文章

  1. 局域网象棋游戏(C++实现,使用Socket,界面使用Win32,CodeBlocks+GCC编译)

    目录 成果 运行效果图 过程 1. 首先的问题是下棋的两端应该是什么样的? 2. 接下来的问题是怎么表示,怎么存储? 3. 然后应该怎么通信呢? 代码 main.cpp chinese_chess.h ...

  2. 俄罗斯方块(Win32实现,Codeblocks+GCC编译)

    缘起: 在玩Codeblocks自带的俄罗斯方块时觉得不错,然而有时间限制.所以想自己再写一个. 程序效果: 主要内容: 程序中有一个board数组,其中有要显示的部分,也有不显示的部分,不显示的部分 ...

  3. 简单的词法分析和语法分析(C++实现,CodeBlocks+GCC编译)

    说明: 分析的语言是SNL语言,详见<编译程序的设计与实现>( 刘磊.金英.张晶.张荷花.单郸编著) 词法分析就是实现了词法分析的自动机 语法分析使用递归下降法 运行结果: 词法分析 得到 ...

  4. 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)

    说明: 程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用. 程序遍历当前文件夹,对其中的文件夹执行递归遍历.同时检查遍历到 ...

  5. GLine游戏(Win32GUI实现,CodeBlocks+GCC编译)

    游戏规则: 在10X10的棋盘上有五种颜色的棋子. 点击一个棋子,再点击一个空格子,如果两者之间有一条路径的话,棋子会移动到空格子内. 每移动一次,棋盘上会增加三个棋子,其位置和颜色都是随机的. 当横 ...

  6. Socket服务端和客户端(C++,CodeBlocks+GCC编译)

    //main.cpp 1 #include "j_socket.h" #include <stdio.h> #include <pthread.h> ; j ...

  7. Shell(C++实现,CodeBlocks+GCC编译)

    程序效果: 只实现了login .cd .ls .cat 四个命令.而且只能在 Windows 下运行. 代码: //main.cpp 1 #include <iostream> #inc ...

  8. GCC 编译优化指南(转)

    GCC 编译优化指南(转) http://www.jinbuguo.com/linux/optimize_guide.html 作者:金步国 版权声明 本文作者是一位开源理念的坚定支持者,所以本文虽然 ...

  9. GCC 编译优化指南

    转自: http://www.jinbuguo.com/linux/optimize_guide.html GCC 编译优化指南 作者:金步国[www.jinbuguo.com] 版权声明 本文作者是 ...

随机推荐

  1. python之消息队列

    引言 你是否遇到过两个(多个)系统间需要通过定时任务来同步某些数据?你是否在为异构系统的不同进程间相互调用.通讯的问题而苦恼.挣扎?如果是,那么恭喜你,消息服务让你可以很轻松地解决这些问题.消息服务擅 ...

  2. ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

    本文将介绍使用NLOG.Elmah结合ElasticSearch实现分布式日志管理. 一.ElasticSearch简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布 ...

  3. 1、ASP.NET MVC入门到精通——新语法

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在学习ASP.NET MVC之前,有必要先了解一下C#3.0所带来的新的语法特性,这一点尤为重要,因为在MVC项目中我们利用C#3.0的新特 ...

  4. [连载]《C#通讯(串口和网络)框架的设计与实现》-3.设备驱动的设计

    目       录 第三章           设备驱动的设计... 2 3.1           初始化设备... 4 3.2           运行设备接口设计... 4 3.3        ...

  5. Android开发5:应用程序窗口小部件App Widgets的实现

    前言 本次主要是实现一个Android应用,实现静态广播.动态广播两种改变 widget内容的方法,即在上篇博文中实验的基础上进行修改,所以此次实验的重点是AppWidget小部件的实现啦~ 首先,我 ...

  6. Microsoft Dynamics AX 7 新特性探索 - Demo 部署(Part 1)

    Dynamics AX 7已经发布了一段时间了,我们知道这次微软为我们带来了许多令人激动的新特性.在这个系列里,Reinhard将揭开New Dynamics AX的神秘面纱,和大家一起探索这些新的特 ...

  7. 自动显示隐藏布局的listView

    借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示 ...

  8. Java 内部类的阐述

    创建一个Computer抽象类:用来在Test类中创建匿名抽象类 package com.zhiyou; public abstract class Computer { int a = 1; /** ...

  9. 遇到别人留下的storyboard的,你需要一个引导图,但是不知道怎么跳转.

    首先在AppDeledate.m文件里是这样. { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds] ...

  10. Linux LVM学习总结——放大LV容量

    本篇介绍LVM管理中的命令lvresize,我们先创建一个卷组VG VolGroup02,它建立在磁盘/dev/sdc (大小为8G)上.创建逻辑卷LV时,我们故意只使用了一小部分.具体情况如下所示 ...