程序效果:

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

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

代码:

//main.cpp
1 #include <windows.h>
#include <math.h>
//#include <iostream>
//using namespace std;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
double WIDTH=,HEIGHT=;
double px=0.0,py=0.0;
double ppx=0.0,ppy=0.0;
POINT sp;
int cat=;
double width,height;
PAINTSTRUCT ps ;
RECT rect ;
POINT pts[];
double left_eye_px=0.0,right_eye_px=0.0;
double eye_py=0.0;
double eye_r=0.0;
double k=0.0;
double ball_hr=0.0;//半径
double deltax=0.0;
POINT p,pp;
int initGraph=;
LPPOINT catp;
RECT eye_rect;
HBRUSH gray_brush =CreateSolidBrush (RGB(,,));
HBRUSH white_brush =CreateSolidBrush (RGB(,,));
HBRUSH black_brush =CreateSolidBrush (RGB(,,));
int drawRound(HDC hdc,int x,int y,int r) //r 直径
{
Ellipse(hdc,x-r/,y-r/,x+r/,y+r/);
return ;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("Mouse Hook"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
WIDTH, // initial x size
HEIGHT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} int init(HWND hwnd)
{
GetClientRect (hwnd, &rect) ;
width=rect.right-rect.left ;
height=rect.bottom-rect.top ;
left_eye_px=width*/-height/;
right_eye_px=width*/+height/;
eye_py=height*/-height/;
eye_r=height/;
ball_hr=eye_r/;//半径
px=left_eye_px;
py=eye_py;
ppx=right_eye_px;
ppy=eye_py;
eye_rect.left=left_eye_px-eye_r/;
eye_rect.right=right_eye_px+eye_r/;
eye_rect.top=eye_py-eye_r/;
eye_rect.bottom=eye_py+eye_r/;
return ;
}
int drawHand(HDC hdc)
{
/**< 画嘴 */
if(cat==)
{
MoveToEx (hdc, width/+width/,height/*, NULL) ;
LineTo (hdc, width*/-width/, height/*) ;
SelectObject (hdc,gray_brush ) ;
Ellipse(hdc,width/-width/,height/*,width/+width/,height*1.1);//左胳膊
Ellipse(hdc,width/*-width/,height/*,width/*+width/,height*1.1);//右胳膊
return ;
}
SelectObject (hdc, gray_brush) ;
Ellipse(hdc,width/*-width/,height/*,width/*+width/,height*1.1);//右胳膊
Ellipse(hdc,width/-width/,height-height/,width/+width/,height);//左胳膊
/**< 画脚掌 */
Ellipse(hdc,width/-width/,height-height/,width/+width/,height);
MoveToEx (hdc, width/-width/,height-height/, NULL) ;
LineTo (hdc, width/-width/, height-height/) ;
MoveToEx (hdc, width/,height-height/, NULL) ;
LineTo (hdc, width/, height-height/) ;
MoveToEx (hdc, width/+width/,height-height/, NULL) ;
LineTo (hdc, width/+width/, height-height/) ;
Arc( hdc, width/-width/, height-height/,width/+width/, height,width/+width/, height,width/-width/, height);
/**< 画嘴 */
Arc( hdc, width*/+width/, height/*,width*/-width/, height/*,width*/, height/*, width*/, height/*);
return ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
POINT pc;
HDC hdc;
switch (message)
{
case WM_CREATE:
init(hwnd);
initGraph =;
InvalidateRect (hwnd,NULL, true) ;
return ;
case WM_SIZE:
init(hwnd);
initGraph =;
InvalidateRect (hwnd,NULL, true) ;
return ;
case WM_MOVE:
return ;
case WM_LBUTTONDOWN:
GetCursorPos(&pc);
if(pc.x==sp.x&&pc.y==sp.y)
{
cat=;
SetCursorPos(sp.x,sp.y-height/);
ReleaseCapture () ;
initGraph=;
InvalidateRect (hwnd,NULL, true) ;
}
return ;
case WM_MOUSEMOVE:
if(cat==)
{
SetCursorPos(sp.x,sp.y);
return ;
}
GetCursorPos(&p);
pp=p;
ScreenToClient(hwnd,&pp);
if((pp.x>width/-width/)&&(pp.x<width/+width/)&&(pp.y>height-height/)&&(pp.y<height))
{
SetCapture (hwnd) ;
sp.x=width/;
sp.y=height-height/;
ClientToScreen(hwnd,&sp);
cat=;
initGraph=;
InvalidateRect (hwnd,NULL, true) ;
return ;
}
k=999.0;
if(pp.x-left_eye_px!=)
{
k=(pp.y-eye_py)/(pp.x-left_eye_px);
}
deltax=(ball_hr/)/(+k*k);
deltax=sqrt(deltax);
if(!(pp.x>left_eye_px))
{
deltax=(-1.0)*deltax;
}
px=left_eye_px+deltax*;
deltax=(ball_hr/)/(+/(k*k));
deltax=sqrt(deltax);
if(!(pp.y>eye_py))
{
deltax=(-1.0)*deltax;
}
py=eye_py+deltax*;
k=999.0;
if(pp.x-right_eye_px!=)
{
k=(pp.y-eye_py)/(pp.x-right_eye_px);
}
deltax=(ball_hr/)/(+k*k);
deltax=sqrt(deltax);
if(!(pp.x>right_eye_px))
{
deltax=(-1.0)*deltax;
}
ppx=right_eye_px+deltax*;
deltax=(ball_hr/)/(+/(k*k));
deltax=sqrt(deltax);
if(!(pp.y>eye_py))
{
deltax=(-1.0)*deltax;
}
ppy=eye_py+deltax*;
InvalidateRect (hwnd, &eye_rect, false) ;
return ;
case WM_PAINT:
hdc=BeginPaint (hwnd,&ps) ;
if(initGraph==)
{
SelectObject (hdc, gray_brush) ;
Ellipse(hdc,width/,height/,width-width/,height*/);//身体
/**< 画脸 */
Ellipse(hdc,,height/,width*/,height*/);
/**< 画耳朵 */
pts[].x =width/;
pts[].y =height/;
pts[].x =width/;
pts[].y =height/*;
pts[].x =width/*;
pts[].y =height/*;
Polygon (hdc, pts, ) ;
pts[].x =width/*;
pts[].y =height/;
pts[].x =width/*;
pts[].y =height/*;
pts[].x =width/*;
pts[].y =height/*;
Polygon (hdc, pts, ) ;
/**< 画胡子 */
MoveToEx (hdc, , height/*, NULL) ;
LineTo (hdc, width/*,height/*) ;
MoveToEx (hdc, , height/*, NULL) ;
LineTo (hdc, width/*,height/*) ;
MoveToEx (hdc, , height/*, NULL) ;
LineTo (hdc, width/*,height/*) ;
MoveToEx (hdc, width, height/*, NULL) ;
LineTo (hdc, width/*,height/*) ;
MoveToEx (hdc, width, height/*, NULL) ;
LineTo (hdc, width/*,height/*) ;
MoveToEx (hdc, width, height/*, NULL) ;
LineTo (hdc, width/*,height/*) ;
drawHand(hdc);
initGraph=;
}
/**< 画眼睛 */
SelectObject (hdc, white_brush) ;
drawRound(hdc,left_eye_px,eye_py,eye_r);
drawRound(hdc,right_eye_px,eye_py,eye_r);
SelectObject (hdc, black_brush) ;
drawRound(hdc,px,py,ball_hr*);
drawRound(hdc,ppx,ppy,ball_hr*);
EndPaint (hwnd, &ps) ;
return ;
case WM_DESTROY:
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

程序写于大三上学期,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. 兼容性背景颜色半透明CSS代码(不影响内部子元素)

    如何简单兼容性的实现父元素是半透明背景色,而子元素不受影响. 兼容所有浏览器的背景颜色半透明CSS代码: background-color: rgba(, , , .); filter: progid ...

  2. spring Mvc + Mybatis 中使用junit

    在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/ja ...

  3. 关于在云服务器上部署tomcat笔记

    tomcat无法启动的原因有:1.由于在tomcat里JVM没有配好,他默认的是批向c:\program file\Java下的那一个,如果你确认 这没有删掉,JVM指向你自己安装的jdk\jre试试 ...

  4. python之最强王者(1)——python入门简介

    1.Python简介 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像Pe ...

  5. 【系统架构】IT职业技能图谱(点开大图查看)

    本文地址 1 程序开发语言综述 2 iOS开发工程师必备技能 3 运维工程师必备技能 4 前端工程师必备技能 5 大数据工程师必备技能 6 云计算工程师必备技能 7 安全工程师必备技能 8 移动无线测 ...

  6. GJM :异步Socket [转载]

    原帖地址:http://blog.csdn.net/awinye/article/details/537264 原文作者:Awinye 目录(?)[-] 转载请原作者联系 Overview of So ...

  7. javascript中的一些核心知识点以及需要注意的地方

    前言 近期杂事甚多,这些事情的积累对知识体系的提升有好处,但是却不能整理出来,也整理不出来 比如说我最近研究的Hybrid在线联调方案便过于依赖于业务,就算分享也不会有人读懂,若是抽一点来分享又意义不 ...

  8. Javascript中构造函数与new命令

    典型的面向对象编程语言(比如C++和Java),存在“类”(class)这个概念.所谓“类”就是对象的模板,对象就是“类”的实例.但是,在JavaScript语言的对象体系,不是基于“类”的,而是基于 ...

  9. 列表屏幕(List Screen)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. 转载 Android 多线程处理之多线程用法大集合

    handler.post(r)其实这样并不会新起线程,只是执行的runnable里的run()方法,却没有执行start()方法,所以runnable走的还是UI线程. 1.如果像这样,是可以操作ui ...