博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
鼠标钩子--- 悬浮窗口
阅读量:5905 次
发布时间:2019-06-19

本文共 4499 字,大约阅读时间需要 14 分钟。

MouseHook.dll

MouseHook.h

#pragma once/*global*/ extern DWORD g_dwThreadId; extern HINSTANCE g_Hinstance; extern HHOOK g_MouseHook; extern POINT g_ptOld;/*define*/#define WM_MOUSEMOVE_ALL WM_USER+1000/*export*/ extern "C" __declspec(dllexport) BOOL SetHook(DWORD dwThreadId); LRESULT CALLBACK MouseProc(     __in  int nCode,    __in  WPARAM wParam,    __in  LPARAM lParam    );

 

MouseHook.cpp

#include "stdafx.h"#include "MouseHook.h"#pragma data_seg("GlobalData")DWORD g_dwThreadId = 0;#pragma data_seg()#pragma comment(linker, "/SECTION:GlobalData,RWS")HINSTANCE g_Hinstance = NULL;HHOOK g_MouseHook = NULL;POINT g_ptOld;BOOL SetHook(DWORD dwThreadId){    if ( dwThreadId <= 0 )        return FALSE;    g_MouseHook = ::SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, g_Hinstance, 0);    if ( NULL == g_MouseHook )        return FALSE;        g_dwThreadId = dwThreadId;    return TRUE;}LRESULT CALLBACK MouseProc(    __in  int nCode,    __in  WPARAM wParam,    __in  LPARAM lParam    ){    LRESULT lResult = ::CallNextHookEx(g_MouseHook, nCode, wParam, lParam);    if ( g_dwThreadId > 0 )    {        POINT pt;        ::GetCursorPos(&pt);        if ( pt.x != g_ptOld.x && pt.y != g_ptOld.y )        {            ::PostThreadMessage(g_dwThreadId, WM_MOUSEMOVE_ALL, 0, MAKELPARAM(pt.x, pt.y));            g_ptOld.x = pt.x;            g_ptOld.y = pt.y;        }    }    return lResult;}

 

dllmain.cpp

#include "stdafx.h"#include "MouseHook.h"BOOL APIENTRY DllMain( HMODULE hModule,                       DWORD  ul_reason_for_call,                       LPVOID lpReserved                     ){    g_Hinstance = (HINSTANCE)hModule;    g_ptOld.x = 0;    g_ptOld.y = 0;    return TRUE;}

 

HangWnd

HangWnd.cpp(App)

BOOL CHangWndApp::InitInstance(){    // InitCommonControlsEx() is required on Windows XP if an application    // manifest specifies use of ComCtl32.dll version 6 or later to enable    // visual styles.  Otherwise, any window creation will fail.    INITCOMMONCONTROLSEX InitCtrls;    InitCtrls.dwSize = sizeof(InitCtrls);    // Set this to include all the common control classes you want to use    // in your application.    InitCtrls.dwICC = ICC_WIN95_CLASSES;    InitCommonControlsEx(&InitCtrls);    CWinApp::InitInstance();    AfxEnableControlContainer();    HMODULE hModule = ::LoadLibrary(_T("../Debug/MouseHook.dll"));    if ( NULL == hModule )        return FALSE;    typedef BOOL (*SETHOOK)(DWORD dwThreadId);    SETHOOK SetHook = (SETHOOK)::GetProcAddress(hModule, "SetHook");    if ( SetHook )    {        DWORD dwThreadId = ::GetCurrentThreadId();        SetHook(dwThreadId);    }    CHangWndDlg dlg;    m_pMainWnd = &dlg;    INT_PTR nResponse = dlg.DoModal();    ::FreeLibrary(hModule);    hModule = NULL;    // Since the dialog has been closed, return FALSE so that we exit the    //  application, rather than start the application's message pump.    return FALSE;}

 

HangWndDlg.h

class CHangWndDlg : public CDialogEx{// Constructionpublic:    CHangWndDlg(CWnd* pParent = NULL);    // standard constructorprivate:    BOOL m_bIsWindowVisible;};

 

HangWndDlg.cpp

#define WM_MOUSEMOVE_ALL WM_USER+1000 CHangWndDlg::CHangWndDlg(CWnd* pParent /*=NULL*/)    : CDialogEx(CHangWndDlg::IDD, pParent){    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);    m_bIsWindowVisible = TRUE;}BOOL CHangWndDlg::PreTranslateMessage(MSG* pMsg){    if ( pMsg->message == WM_MOUSEMOVE_ALL )    {        POINT pt;        pt.x = LOWORD(pMsg->lParam);        pt.y = HIWORD(pMsg->lParam);        CRect rcWnd;        GetWindowRect(&rcWnd);        int nWidth = rcWnd.Width();        int nHeight = rcWnd.Height();        if ( rcWnd.left <= 20 )        {            if ( m_bIsWindowVisible )            {                BOOL bPtInRect = rcWnd.PtInRect(pt);                if ( !bPtInRect )                {                    for ( int i = 0; i < nWidth; i++ )                    {                        SetWindowPos(&CWnd::wndNoTopMost, 0, rcWnd.top, nWidth-i, nHeight, SWP_SHOWWINDOW);                    }                    SetWindowPos(&CWnd::wndNoTopMost, 0, rcWnd.top, nWidth, nHeight, SWP_HIDEWINDOW);                    m_bIsWindowVisible = FALSE;                }            }        }        if ( !m_bIsWindowVisible && pt.x < 10 )        {            for ( int i = 0; i < nWidth; i++ )            {                SetWindowPos(&CWnd::wndNoTopMost, 0, rcWnd.top, 0+i, nHeight, SWP_SHOWWINDOW);            }            m_bIsWindowVisible = TRUE;        }    }    return CDialogEx::PreTranslateMessage(pMsg);}

 

 

转载地址:http://yhdpx.baihongyu.com/

你可能感兴趣的文章
微软超过苹果 成为全球第一大市值公司
查看>>
GitHub启用安全告警功能
查看>>
从 Google 的一道面试题说起·
查看>>
重构到更深层的模型
查看>>
服务应该去版本化,不管是微服务还是SOA
查看>>
Istio v1.0服务网格发布,各特性已生产就绪
查看>>
13岁女孩因发布JavaScript无限循环代码被捕
查看>>
知道大数据却不清楚工业大数据,知识架构“欠”在哪里?
查看>>
Docker周报:Windows Server将支持Mesos
查看>>
《码出高效:Java开发手册》背后的故事
查看>>
Oracle的Java模块化系统保卫战
查看>>
苹果开源Swift基准测试套件
查看>>
Java程序员:不要因未知而让云成本大涨
查看>>
QCon上海2015 架构实战热点前瞻
查看>>
python学习笔记-使用@property、setter、deleter
查看>>
EF Core数据库Provider一览
查看>>
Ceylon语言加入Eclipse基金会
查看>>
阿里云Redis开发规范
查看>>
滴滴自研分布式NoSQL数据库Fusion的演进之路
查看>>
软件安全访谈:ZipSlip、NodeJS安全性和BBS攻击
查看>>