抓鼠标的猫(Win32实现,Codeblocks+GCC编译)

时间:2021-10-22 00:21:15

程序效果:

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

抓鼠标的猫(Win32实现,Codeblocks+GCC编译)

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

抓鼠标的猫(Win32实现,Codeblocks+GCC编译)

代码:

//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