Program effect :

The cat's eyes follow the mouse all the time :

When the mouse passes near the cat's right foot , The cat will catch the mouse .( not used Hook)

Code :

//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;// radius
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 The diameter of
{
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/;// radius
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)
{
/**< Draw your mouth */
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);// Left arm
Ellipse(hdc,width/*-width/,height/*,width/*+width/,height*1.1);// Right arm
return ;
}
SelectObject (hdc, gray_brush) ;
Ellipse(hdc,width/*-width/,height/*,width/*+width/,height*1.1);// Right arm
Ellipse(hdc,width/-width/,height-height/,width/+width/,height);// Left arm
/**< Painting feet */
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);
/**< Draw your mouth */
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*/);// The body
/**< Draw a face */
Ellipse(hdc,,height/,width*/,height*/);
/**< Draw ears */
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, ) ;
/**< Draw a beard */
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=;
}
/**< Draw eyes */
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) ;
}

The program was written in the first semester of junior year ,Windows Programming Course assessment assignments .

2016.4.12 Update blog .

END

The cat grabbing the mouse (Win32 Realization ,Codeblocks+GCC compile ) More articles about

  1. LAN chess game (C++ Realization , Use Socket, Interface to use Win32,CodeBlocks+GCC compile )

    Catalog results Run the renderings The process 1. The first question is what the two ends of chess should look like ? 2. The next question is how to express , How to store ? 3. And then how to communicate ? Code main.cpp chinese_chess.h ...

  2. tetris (Win32 Realization ,Codeblocks+GCC compile )

    origin : Play with Codeblocks I think it's good when I bring my own Tetris , But there's a time limit . So I want to write another one myself . Program effect : primary coverage : There is one in the program board Array , There are parts to show , There are also parts that are not shown , The part not shown ...

  3. Simple lexical analysis and grammatical analysis (C++ Realization ,CodeBlocks+GCC compile )

    explain : The language of analysis is SNL Language , See < Compiler design and implementation >(  Liu Lei . Jin Ying . Zhang . Zhang Hehua . Edited by Dan Dan ) Lexical analysis is an automaton that implements lexical analysis Syntax analysis uses recursive descent Running results : Lexical analysis obtain ...

  4. In the folder In the specified type file Find string (CodeBlocks+GCC compile , Console program , Only in Windows Up operation )

    explain : Program usage  io.h  Medium  _findfirst  and  _findnext  Function to traverse the folder , So the program can only be in  Windows  Next use . Program traverses the current folder , Recursive traversal of the folder . At the same time check traversal to ...

  5. GLine game (Win32GUI Realization ,CodeBlocks+GCC compile )

    The rules of the game : stay 10X10 There are five colors on the chessboard . Click on a piece , Click on an empty grid , If there's a path between the two , The pieces will move into the empty space . Every time you move , Three pieces will be added to the chessboard , Its location and color are random . Dangheng ...

  6. Socket Server and client (C++,CodeBlocks+GCC compile )

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

  7. Shell(C++ Realization ,CodeBlocks+GCC compile )

    Program effect : only login .cd .ls .cat  Four orders . And only in Windows Run under . Code : //main.cpp 1 #include <iostream> #inc ...

  8. GCC Compiler optimization guide ( turn )

    GCC Compiler optimization guide ( turn ) http://www.jinbuguo.com/linux/optimize_guide.html author : Jin Bu Guo Copyright notice The author of this article is a strong supporter of the open source concept , So although this article ...

  9. GCC Compiler optimization guide

    from : http://www.jinbuguo.com/linux/optimize_guide.html GCC Compiler optimization guide author : Jin Bu Guo [www.jinbuguo.com] Copyright notice The author of this article is ...

Random recommendation

  1. php Read excel Content

    Use php Read excel Contents of the file 1. download PHPExcel class 2. Code : header("Content-type:text/html;charset=utf-8");req ...

  2. The serializable class does not declare a static final serialVersionUID field of type long

    Under compile Java The program , appear The serializable class  does not declare a static final serialVersionUID field of typ ...

  3. Ruby in , An interesting example of class methods and instance methods

    The initial code is as follows : class Object def abc p "instance abc" end def self.abc p "class abc" en ...

  4. Coding artifact Sublime Text Package management tools and extensions

    Sublime Text It's recognized by programmers as the magic of coding , With beautiful user interface and powerful functions , For example, code thumbnail , Multiple choice , Quick commands, etc . You can also customize key bindings , Menus and toolbars .Sublime Text Its main functions include : spell ...

  5. Use classes to simulate C The assignment of style + Return value

    This method is better : class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): ...

  6. Lua Medium userdata

    [ Let's start here ] Before I publish <Lua Types and values in > In this article , Some readers left me a message , say : You should sum it up Lua Medium function and userdata type . Now it's time to sum up . about functio ...

  7. Echarts Detailed explanation of histogram configuration

    1. Basic histogram // Specify configuration items and data for the chart var option = { // ---- title ----- title: { text: ' Main title ', textStyle: { color: 're ...

  8. TeXstudio+TexLive Cross reference

    LaTeX Introduction to cross reference system  https://www.cnblogs.com/wenbosheng/p/9537774.html Generally speaking, you need to run the typesetting command twice to generate the reference number . The principle behind this is , First of all ...

  9. 【bzoj】2733: [HNOI2012] There is no country

    Description There is no country to contain n Island , Number from 1 To n, Each island has its own unique importance , According to the degree of importance To put this n Island ranking , Rank use 1 To n To express . Some islands are connected by huge bridges , Through the bridge you can ...

  10. goim Source code analysis and secondary development -comet Analysis of a

    Because to finish a chat project , So we learn from goim, The first analysis is intended to be semi original , Excerpt first http://www.jianshu.com/p/8bd96a9a473d Some of his understanding , I'm writing this to better understand the project ...