代碼如下:
CLaunchDLLApp theApp;
HHOOK Hook; 
LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam); 
void SaveLog(char* c); 
DllExport void WINAPI InstallLaunchEv() 

  Hook=(HHOOK)SetWindowsHookEx(WH_MOUSE, 
  (HOOKPROC)LauncherHook, 
  theApp.m_hInstance, 
  0); 
} LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam) 
{
  LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam); 
  if(nCode==HC_ACTION) 
  { 
if(wParam==WM_RBUTTONDOWN)
 AfxMessageBox("WM_RBUTTONDOWN");
  } 
  return Result; 
}
在單文檔應用程式中加入InstallLaunchEv(),程式運行時,多點擊mouse右鍵幾次,會引起程式羿常或系統當機,大家幫看看,問題出在哪里呢?

解决方案 »

  1.   

    http://www.copathway.com/vchelp/type.asp?class_id=1&type_id=25
      

  2.   

    MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*) lParam;
    在mhs中检测一下鼠标点到哪里了,比如
    if(mhs->wHitTestCode!=HTCLIENT)
    {...........}
    还有
    Hook=(HHOOK)SetWindowsHookEx(WH_MOUSE, 
      (HOOKPROC)LauncherHook, 
      theApp.m_hInstance, 
      0); 
    改成
    Hook=(HHOOK)SetWindowsHookEx(WH_MOUSE, 
      (HOOKPROC)LauncherHook, 
      NULL,
      ::GetCurrentThreadId()); 
    试试
      

  3.   

    謝謝!
    我已經改了,但是程式對MOUSE毫無反映,如下:LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(
                                int nCode, 
                               WPARAM wParam, 
                                LPARAM lParam)
    {
    MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*) lParam; if(HC_ACTION==nCode)
    {
    if(wParam==WM_RBUTTONUP)
    if(mhs->wHitTestCode!=HTCLIENT)
    MessageBox(NULL,"hello","ok",MB_OK);
    }
    }
    BOOL __declspec(dllexport)__stdcall installhook()
    {//hkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)KeyboardProc,hins,0);
    hkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)KeyboardProc,NULL,::GetCurrentThreadId());
    return TRUE;
    }
      

  4.   

    当 nCode==-1时必须要调用CallNextHook
    还有KeyboardProc中没有反回值
    如果都改对了还不行,再把这句去掉试试if(HC_ACTION==nCode)
      

  5.   

    thanks!
    我是這樣做的,但是會當機,如下:
    LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(
                                int nCode, 
                               WPARAM wParam, 
                                LPARAM lParam)
    {
        if(nCode == HC_ACTION) 
        { 
            // get a pointer to the mouse hook struct. 
            PMOUSEHOOKSTRUCT mhs = (PMOUSEHOOKSTRUCT) lParam; 
            
            // intercept messages for left button down and up
            switch(wParam) 
            { 
                case WM_NCLBUTTONDOWN: 
                    {
                        // get the pointer to the main window 
                        CWnd *pWnd =  AfxGetMainWnd(); 

                        if((mhs->hwnd == pWnd->GetSafeHwnd()) 
    && (mhs->wHitTestCode == HTCAPTION)) 
                        { 
                            MessageBox(NULL,"NcRButtonDown","ok",0);
    Sleep(100);
                          
                        }
                    }
                    break;            case WM_NCLBUTTONUP: 
                     {
    bNcLButtonDown = FALSE; 
    MessageBox(NULL,"NcLButtonUp","ok",0);
    }
                    break;             case WM_LBUTTONUP: 
                    {
                        // get the pointer to the main window 
                        CWnd *pWnd =  AfxGetMainWnd();                 
                        if((mhs->hwnd == pWnd->GetSafeHwnd()) 
    && (bNcLButtonDown == TRUE))
                        {
                     pWnd->PostMessage(WM_NCLBUTTONUP, HTCAPTION, MAKELONG(mhs->pt.x, mhs->pt.y)); 
    MessageBox(NULL,"LButtonUp","ok",0);
                            // reset non-client left button down 
                            bNcLButtonDown = FALSE;
                        }
                    } 
                    break;             default: 
                    break; 
            } 
     
    }
    LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam ); return  RetVal;
    }
    謝謝!
      

  6.   

    死机可能是这句造成的CWnd *pWnd =  AfxGetMainWnd();