zoukankan      html  css  js  c++  java
  • 鼠标钩子--- 悬浮窗口

    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
    {
    // Construction
    public:
        CHangWndDlg(CWnd* pParent = NULL);    // standard constructor
    
    private:
        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); }
  • 相关阅读:
    WPS客户端更新日志留着备用
    C#设置电脑时间帮助类
    分布式数据库拆表拆库的常用策略
    程序员如何对待自己的工作
    java对接申通下单接口示例代码
    电子面单开发流程
    2016年工作总结和计划
    【价值观】大牛们的经典语录
    大话 程序猿 眼里的 接口
    【C#公共帮助类】DateTimeHelper设置电脑本地时间,实际开发很需要
  • 原文地址:https://www.cnblogs.com/qintangtao/p/3259995.html
Copyright © 2011-2022 走看看