一个简单的mouse消息勾子实现 Topic is solved

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
smallwolf
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sun Aug 26, 2007 11:02 pm

一个简单的mouse消息勾子实现

Post by smallwolf » Wed Mar 19, 2008 7:32 pm

本人前一个星期开始学消息勾子,发现很多文章都是代码不全。
一步一步实验,慢慢才扁译成功。过程是痛苦的。
读懂以下代码需要一定的基础。学一点基本概念后,再来看才行。
我是菜鸟一名,请高手指点。
我有不少的wxWidgets笔记,整理后会发放。只是现在忙着学API HOOK,才没时间。


wxDevC++环境,用C开发的全局勾子.

extern "C" 是用c++开发才加上的。因为我用C写,不需要加上。

Code: Select all

dll.h



 

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

#include <windows.h>


DLLIMPORT void HelloWorld (void);       //出口函数
LRESULT CALLBACK HookProc(int,WPARAM,LPARAM);   //消息勾子
DLLIMPORT BOOL InstallHook(void);    //出口函数
DLLIMPORT BOOL UnHook(void);    //出口函数


#endif /* _DLL_H_ */


 

 

---------------------------

 

dllmain.c

 

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#pragma data_seg("share")   
 HINSTANCE hinst=NULL;     //DLL句柄
 HHOOK hhk=NULL;          //勾子句柄
#pragma data_seg()
#pragma comments(linker,"/section:share,rws")


DLLIMPORT void HelloWorld ()
{
    MessageBox (0, "Hello World from DLL! ", "Hi", MB_ICONINFORMATION);
}

LRESULT CALLBACK HookProc(int nCode,WPARAM wParam, LPARAM lParam)
{
    if(nCode<0)
    return (CallNextHookEx(0,nCode,wParam,lParam));
    switch(wParam)
    {
        case WM_RBUTTONDOWN:
            MessageBox(0,"Right button down! ","hi",MB_ICONINFORMATION);
            break;
        case WM_LBUTTONDOWN:
            MessageBox(0,"Left button down! ","hi",MB_ICONINFORMATION);
            break;
        default:
            
            break;
    }
    return (CallNextHookEx(0,nCode,wParam,lParam));
}

DLLIMPORT BOOL InstallHook()
{
    hhk=SetWindowsHookEx(WH_MOUSE,HookProc,hinst,0);//将回传值放在hhk,hhk是勾子句柄。
    if(hhk)
     {
            MessageBox(0,"Hook Success! ","hi",MB_ICONINFORMATION);
            return TRUE;
     }
    else
      {
            MessageBox(0,"Hook Fail! ","hi",MB_ICONINFORMATION);
            return FALSE;
    }

 

}


DLLIMPORT BOOL UnHook()
{
    if (hhk)
    {
        UnhookWindowsHookEx(hhk);
        return TRUE;
    }
    else
     return FALSE;
}

 

 


BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
            hinst=hInst;      //得到DLL句柄,hinst在共享段。

        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}


Loaden
I live to help wx-kind
I live to help wx-kind
Posts: 177
Joined: Tue Feb 19, 2008 10:21 am
Location: China

Post by Loaden » Wed Mar 19, 2008 11:12 pm

这是完全的Windows API的实现,貌似无法实现跨平台...
期待楼主能将wxWidgets资料打包共享,先谢谢了!

swordfish862
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 07, 2008 12:06 pm
Location: Yunnan,China
Contact:

Post by swordfish862 » Mon Apr 07, 2008 12:45 pm

感谢楼主、感谢天、感谢地,感谢CCTV、感谢MTV(都流行说这个,不是吗?)!
我在搜遍全球无结果的情况下,对我有很大的启示!!!

spritez
Earned a small fee
Earned a small fee
Posts: 21
Joined: Sat Apr 19, 2008 2:16 pm
Location: china

Post by spritez » Mon Oct 06, 2008 7:51 am

谢谢楼主,对我也很有参考意义。
*** china ***

Post Reply