// 程序调用部份代码
hMod = LoadLibrary("hookmessage.dll");

if(hMod)
{
STARTHOOK starthook = (STARTHOOK)GetProcAddress(hMod, _T("StartHook"));
if(starthook&&pWnd)
{
bool bok = (*starthook)(this->m_hWnd, pWnd->m_hWnd);
if(!bok)
{
MessageBox("启动失败!");
return;
}
}
else
{
MessageBox("未找到坐标获取模块.");
return;
}
}
else
{
MessageBox("未找到函数库. hookmessage.dll");
return;
}
// hookmessage.cpp : 定义 DLL 应用程序的入口点。
//#include "stdafx.h"
#include "stdio.h"HHOOK hk;
LRESULT CALLBACK CallWndProc(
  int nCode,     // hook code
  WPARAM wParam, // current-process flag
  LPARAM lParam  // message data
);
#define DLLEXPORT extern "C" __declspec(dllexport)DLLEXPORT bool StartHook(HWND hOwner, HWND hTarget);
DLLEXPORT void StopHook();HMODULE hMod;
HWND hWnd;
HWND hWndTarget;
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
 )
{
hMod = (HMODULE)hModule;
    return TRUE;
}LRESULT CALLBACK CallWndProc(
  int nCode,     // hook code
  WPARAM wParam, // current-process flag
  LPARAM lParam  // message data
)
{
if(nCode<0)
{
return CallNextHookEx(hk, nCode, wParam, lParam);
}
else if(nCode>=0)
{
if(nCode==HC_ACTION)
{
PMOUSEHOOKSTRUCT hook_param;
hook_param = (PMOUSEHOOKSTRUCT)lParam; char cmsg[255];
sprintf(cmsg, "0x%X, X:%d,Y:%d", hook_param->hwnd, hook_param->pt.x, hook_param->pt.y); SetWindowText(hWnd, cmsg);
if(hook_param->hwnd == hWndTarget)
{

if(wParam == WM_MOUSEMOVE)
{
SendMessage(hWnd, WM_USER + 3 , MAKEWPARAM(hook_param->pt.x,hook_param->pt.y), 0);
}
} }
return CallNextHookEx(hk, nCode, wParam, lParam);
}
return 1;
}bool StartHook(HWND hOwner, HWND hTarget)
{
hWnd = hOwner;
hWndTarget = hTarget;
bool bOk = (hWnd&&hWndTarget); if(bOk)
{
hk = SetWindowsHookEx(WH_MOUSE, CallWndProc, hMod, 0);
}
bOk = bOk && hk;
return bOk;
}void StopHook()
{
UnhookWindowsHookEx(hk);
}

解决方案 »

  1.   

    要将hHook保存至共享数据段// hookmessage.cpp : 定义 DLL 应用程序的入口点。
    //#include "stdafx.h"
    #include "stdio.h"#pragma data_seg("Shared")
    HHOOK hk = NULL; // Hook handle for systemwide shell hook
    HINSTANCE g_hInst = NULL;
    #pragma data_seg()
    #pragma comment(linker,"/SECTION:Shared,RWS")
    LRESULT CALLBACK CallWndProc(
      int nCode,     // hook code
      WPARAM wParam, // current-process flag
      LPARAM lParam  // message data
    );
    #define DLLEXPORT extern "C" __declspec(dllexport)DLLEXPORT bool StartHook(HWND hOwner, HWND hTarget);
    DLLEXPORT void StopHook();HMODULE hMod;
    HWND hWnd;
    HWND hWndTarget;
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
    g_hInst = (HINSTANCE)hModule;
        return TRUE;
    }LRESULT CALLBACK CallWndProc(
      int nCode,     // hook code
      WPARAM wParam, // current-process flag
      LPARAM lParam  // message data
    )
    {
    if(nCode<0)
    {
    return CallNextHookEx(hk, nCode, wParam, lParam);
    }
    else if(nCode>=0)
    {
    if(nCode==HC_ACTION)
    {
    PMOUSEHOOKSTRUCT hook_param;
    hook_param = (PMOUSEHOOKSTRUCT)lParam; char cmsg[255];
    sprintf(cmsg, "0x%X, X:%d,Y:%d", hook_param->hwnd, hook_param->pt.x, hook_param->pt.y); SetWindowText(hWnd, cmsg);
    if(hook_param->hwnd == hWndTarget)
    {

    if(wParam == WM_MOUSEMOVE)
    {
    SendMessage(hWnd, WM_USER + 3 , MAKEWPARAM(hook_param->pt.x,hook_param->pt.y), 0);
    }
    } }
    return CallNextHookEx(hk, nCode, wParam, lParam);
    }
    return 1;
    }bool StartHook(HWND hOwner, HWND hTarget)
    {
    hWnd = hOwner;
    hWndTarget = hTarget;
    bool bOk = (hWnd&&hWndTarget); if(bOk)
    {
    hk = SetWindowsHookEx(WH_MOUSE, CallWndProc, g_hInst , 0);
    }
    bOk = bOk && hk;
    return bOk;
    }void StopHook()
    {
    UnhookWindowsHookEx(hk);
    }
      

  2.   

    何必这么麻烦?其实不用HOOK也可以取得鼠标在屏幕的位置啊。
      

  3.   

    // myhook.cpp : Defines the initialization routines for the DLL.
    //#include "stdafx.h"
    #include <afxdllx.h>
    //#include <windows.h>
    //#include <windowsx.h>
    #include "myhook.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif#ifndef HOOKLIBAPI
    #define HOOKLIBAPI  __declspec(dllimport)
    #endif#pragma data_seg("mydata")
    HWND glhPrevTarWnd=NULL;//上次鼠标所指的窗口句柄
    HWND glhDisplayWnd=NULL;//显示目标窗口标题编辑框的句柄
    HHOOK glhHook=NULL;//安装的鼠标勾子句柄
    HINSTANCE glhInstance=NULL;//DLL实例句柄
    #pragma data_seg()
    #pragma comment(linker,"/SECTION:mydata,RWS")static AFX_EXTENSION_MODULE MyhookDLL = { NULL, NULL };extern "C" __declspec(dllexport) BOOL starthook(HWND hWnd);
    extern "C" __declspec(dllexport) BOOL stophook();LRESULT WINAPI MouseProc(int nCode,WPARAM wparam,LPARAM lparam)
    {
       LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lparam;
       if (nCode>=0)
       {
          //取目标窗口句柄
          HWND glhTargetWnd=pMouseHook->hwnd;
     
          //char szClassName[38]="\0";
      //::GetClassName(glhTargetWnd, szClassName, 36);
      CString s;
      s.Format("X:%d  Y:%d",pMouseHook->pt.x,pMouseHook->pt.y);
          SendMessage(glhDisplayWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)s);
       }
         return CallNextHookEx(glhHook,nCode,wparam,lparam);
          //继续传递消息
    }extern "C" int APIENTRY
    DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
    {
    // Remove this if you use lpReserved
    UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH)
    {
    TRACE0("MYHOOK.DLL Initializing!\n");

    // Extension DLL one-time initialization
    if (!AfxInitExtensionModule(MyhookDLL, hInstance))
    return 0; new CDynLinkLibrary(MyhookDLL);//把DLL加入动态MFC类库中
            glhInstance=hInstance;//插入保存DLL实例句柄
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
    TRACE0("MYHOOK.DLL Terminating!\n");
    // Terminate the library before destructors are called
    AfxTermExtensionModule(MyhookDLL);
    }
    return 1;   // ok
    }BOOL starthook(HWND hWnd)
    {
        //安装钩子并设定接收显示窗口句柄
    BOOL bResult=FALSE;
        glhHook=SetWindowsHookEx(WH_MOUSE,MouseProc,glhInstance,0);
        if(glhHook!=NULL)
    bResult=TRUE;
    glhDisplayWnd=hWnd;
        //设置显示目标窗口标题编辑框的句柄
    return bResult;
    }BOOL stophook()
    {
       //卸载钩子
       BOOL bResult=FALSE;
       if(glhHook)
       {
        bResult= UnhookWindowsHookEx(glhHook);
        if(bResult)
    {
           glhPrevTarWnd=NULL;
           glhDisplayWnd=NULL;//清变量
           glhHook=NULL;
    }
       }
       return bResult;
    }
    调用程序://隐式加载dll
    BOOL CMouseDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    CWnd * pwnd=GetDlgItem(IDC_EDIT1);
      starthook(pwnd->GetSafeHwnd());//加载钩子 return TRUE;  // return TRUE  unless you set the focus to a control
    }
      

  4.   

    响应WM_MOUSEMOVE消息,生成消息响应函数
    void CMy1Dlg::OnMouseMove(UINT nFlags, CPoint point) point.x point.y就是鼠标的客户区坐标位置
      

  5.   

    To Papaya:
    编译没有通过,在链接时出了问题
      

  6.   

    我帮你解决获得坐标的问题,很简单。程序启动后添加一个SetTimer(1,70,0);然后在OnTimer()函数里处理          if (1 == nIDEvent)
    {
    CString CurPnt;
    POINT spoint;
    ::GetCursorPos(&spoint); CurPnt.Format("X:%d Y:%d",spoint.x,spoint.y); // 这里就得到了。
    GetDlgItem(IDC_STATIC)->SetWindowText(CurPnt);
    }