底层的鼠标钩子

底层的鼠标钩子一.钩子钩子获取Windows的信息队列,然后处理数据。概念基本信息参考https://blog.csdn.net/camly/article/details/1752798 二.实现鼠标的劫持(00)原理     1.创建窗体->设置回调函数-》安装钩子》(写的比较粗糙哈)(01)实现#define_CRT_SECURE_NO_WARN…

一.钩子

钩子获取Windows的信息队列,然后处理数据。

概念基本信息参考 https://blog.csdn.net/camly/article/details/1752798 

二.实现鼠标的劫持

(00)原理

      1.创建窗体—>设置回调函数—》安装钩子—-》(写的比较粗糙哈)

(01)实现

#define _CRT_SECURE_NO_WARNINGS
//main.cpp
#include<windows.h>
#include<process.h>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
#include <comdef.h>
using namespace std;
#define WH_KEYBOARD_LL 13
#pragma comment(lib,"user32.lib")
#define IDC_CHK_MOUSE_MOVE              1004
#define IDC_CHK_MOUSE_WHEEL             1005
HHOOK  hkMouse = NULL; //鼠标钩子句柄;
HWND g_hWnd = NULL;//主窗口句柄
HANDLE hd[2];
PMSLLHOOKSTRUCT mouseHookStruct;
typedef char* TYPE;
typedef char* TITLE;
struct mouse_status
{
    TYPE type_str;
    int x;
    int y;
    HWND hwnd_id;
};


LRESULT CALLBACK LowLevelMouseProc(
    int nCode,      // hook code
    WPARAM wParam,  // message identifier
    LPARAM lParam   // mouse coordinates
)
{
    //有鼠标消息时,处理消息
    if (g_hWnd != NULL && nCode == HC_ACTION)
    {
        mouse_status *m=new mouse_status;
        m->type_str = new char[64];
        memset(m->type_str, 0, sizeof(char) * 64);
        if (wParam == WM_LBUTTONDOWN)//鼠标左键按下
        {
            strcpy(m->type_str,"WM_LBUTTONDOWN");
            
        }
        if (wParam == WM_LBUTTONUP)//鼠标左键弹起
        {
            strcpy(m->type_str, "WM_LBUTTONUP");
        }
        if (wParam == WM_RBUTTONDOWN)//鼠标右键按下
        {
            strcpy(m->type_str, "WM_RBUTTONDOWN");
        }

        if (wParam == WM_RBUTTONUP)//鼠标右键弹起
        {
            strcpy(m->type_str, "WM_RBUTTONUP");
        }

        if (wParam == WM_MOUSEMOVE)//鼠标移动
        {
            strcpy(m->type_str, "WM_MOUSEMOVE");
        }

        if (wParam == WM_MOUSEWHEEL)//鼠标滚轮
        {
            strcpy(m->type_str, "WM_MOUSEWHEEL");
        }
        //鼠标的位置
        mouseHookStruct = (PMSLLHOOKSTRUCT)lParam;
        m->x = mouseHookStruct->pt.x;
        m->y = mouseHookStruct->pt.y;
        //所在的窗口大小(测试案例)
        HWND hwnd = WindowFromPoint(mouseHookStruct->pt);
        LPRECT rect = new tagRECT;
        GetWindowRect(hwnd, rect);
        //所在窗体的标题
        char szTitle[100];
        GetWindowText(hwnd, szTitle, 100);

        char buf[512] = { 0 };
        FILE *fp = fopen("mouse.txt","a+");
        sprintf(buf,"%s # %p # %d # %d # %s 窗体大小:%d %d %d %d \n", szTitle,(void*)(m->hwnd_id),m->x,m->y,m->type_str,rect->top,rect->right,rect->bottom,rect->left);
        fputs(buf,fp);
        fclose(fp);
    }
    return CallNextHookEx(hkMouse, nCode, wParam, lParam);
}

//安装鼠标钩子,截获系统所有的鼠标消息
BOOL WINAPI StartHookMouse(HWND hWnd)
{
    g_hWnd = hWnd;
    hkMouse = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, GetModuleHandle(NULL), 0);
    if (hkMouse == NULL)
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

//卸载鼠标钩子
VOID WINAPI StopHookMouse()
{
    if (hkMouse != NULL)
    {
        UnhookWindowsHookEx(hkMouse);
    }
}


DWORD WINAPI MOUSE_MODE(HWND hWnd)
{


    StartHookMouse(hWnd);
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) != -1)                   //消息循环
    {
        TranslateMessage(&msg);//TranslateMessage函数将虚拟键消息转换成字符消息
        DispatchMessage(&msg);//该函数调度一个消息给窗口程序。通常调度从GetMessage取得的消息
    };
    StopHookMouse();
    return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//处理消息

//当前窗口句柄    上一个窗口句柄  字符串    ...
int WINAPI WinMain(HINSTANCE histance, HINSTANCE hprehisance, PSTR szcndline, int icmdshow)
{
    //MessageBoxA(0,"hello","huahua",0);
    static TCHAR szappname[100] = TEXT("鼠标");
    //窗口的设置
    WNDCLASS  wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpszClassName = szappname;//与CreateWindow 第一个参数一致
    wndclass.lpszMenuName = NULL;
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpfnWndProc = WndProc;//处理窗口
    wndclass.cbClsExtra = wndclass.cbWndExtra = 0;//扩展
    wndclass.hInstance = histance;//窗口的句柄
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//使用默认的图标
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);//载入鼠标

    if (!RegisterClass(&wndclass))//注册窗口到系统
    {
        MessageBoxA(0, "窗口注册失败", "huahua", 0);
        return 0;
    }
    HWND hwnd = CreateWindow(
        szappname,//窗口的类型
        TEXT("窗口"),//窗口的标题
        WS_OVERLAPPEDWINDOW,//窗口风格
        CW_USEDEFAULT,//默认窗口位置X
        CW_USEDEFAULT,//默认窗口位置y
        CW_USEDEFAULT,//默认窗口宽度
        CW_USEDEFAULT,//默认窗口高度
        NULL, NULL,//子窗口,子菜单
        histance,//窗口句柄
        NULL//
    );


    ShowWindow(hwnd, icmdshow);//显示
    UpdateWindow(hwnd);//更新

    MOUSE_MODE(hwnd);

    return NULL;
    
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)//处理消息
{
    //HDC hdc;//设备环境
    //PAINTSTRUCT ps;
    //RECT rect;
    switch (message)
    {
    case WM_LBUTTONDOWN:
        MessageBoxA(0, "按下左键", "提示", 0);
        break;
    case WM_RBUTTONDOWN:
        MessageBoxA(0, "按下右键", "提示", 0);
        break;
    case WM_DESTROY:
        MessageBoxA(0, "确定关闭", "提示", 0);
        break;
    case WM_CLOSE:
        MessageBoxA(0, "程序即将关闭", "提示", 0);
        _exit(0);
        break;
    }
    return DefWindowProc(hwnd, message, wparam, lparam);

}

 

环境 vs2017+win10

(10)验证

底层的鼠标钩子

(11)总结

可能有点问题,欢迎大家指正。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/35232.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注