做了一个全局钩子在dll里,导出函数为getl(鼠标左键次数)与getr(鼠标右键次数)!测试程序是一个单文档程序,我想把鼠标左键与右键的次数实时在视图上显示出来!现在的问题是我只能拦截在视图view上鼠标的点击次数,不知道为什么!!!高手帮忙!!dll代码如下
#pragma data_seg("mydata")
static HHOOK hook;
static int lnum,rnum;
#pragma data_seg()
BEGIN_MESSAGE_MAP(CMousehookApp, CWinApp)
//{{AFX_MSG_MAP(CMousehookApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMousehookApp constructionCMousehookApp::CMousehookApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CMousehookApp object
int __declspec(dllexport)__stdcall getl()
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());
  return lnum;
}int __declspec(dllexport)__stdcall getr()
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());
  return rnum;
}LRESULT __declspec(dllexport)__stdcall CALLBACK MouseProc(
  int nCode,     
  WPARAM wParam,  
  LPARAM lParam   
)
{
if (nCode < 0)  // do not process the message 
{
return CallNextHookEx(NULL, nCode, wParam, lParam); 
} if(wParam == WM_RBUTTONDOWN) rnum++;
    else if(wParam == WM_LBUTTONDOWN) lnum++;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}BOOL __declspec(dllexport)__stdcall installhook()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
hook=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseProc,AfxGetInstanceHandle(),0);
if(!hook)
{
TRACE("SetWindowsHookEx() call failed");
return false;

return true;
}BOOL __declspec(dllexport)__stdcall unstallhook()
{
UnhookWindowsHookEx(hook);
return true;
}

解决方案 »

  1.   

    兄弟,判断一下LParam 参数,是否是左键还是右键
      

  2.   

    左键右键不是通过wparam判断的吗
      

  3.   

    LRESULT CALLBACK MouseProc(
      int nCode,      // hook code
      WPARAM wParam,  // message identifier
      LPARAM lParam   // mouse coordinates
    );
     
    Parameters
    nCode 
    Specifies a code the hook procedure uses to determine how to process the message. This parameter can be one of the following values: Value Meaning 
    HC_ACTION The wParam and lParam parameters contain information about a mouse message. 
    HC_NOREMOVE The wParam and lParam parameters contain information about a mouse message, and the mouse message has not been removed from the message queue. (An application called the PeekMessage function, specifying the PM_NOREMOVE flag.) 
    If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. wParam 
    Specifies the identifier of the mouse message. 
    lParam 
    Pointer to a MOUSEHOOKSTRUCT structure. 试一下,用SendMessage将获得的次数发送到主程序窗口。
      

  4.   

    在你的编译器选项中加入:    /SECTION:mydata,rws
      

  5.   

    给你参考一个回调函数:LRESULT WINAPI MouseHookProc(int nCode,WPARAM wParam ,LPARAM lParam)
    {
    if(nCode==HC_ACTION)
    {
    switch(wParam) 
    {
    case WM_LBUTTONDOWN:
    nLButtonDown++;
    PostMessage(hClientWnd,WM_MYNOTIFY,0,0);
    break;
    case WM_RBUTTONDOWN:
    nRButtonDown++;
    PostMessage(hClientWnd,WM_MYNOTIFY,0,0);
    break;
    default:break;
    }

        return CallNextHookEx(hMouse,nCode,wParam,lParam);
    }