我的hook.dll源码如下,然后在程序中OnInitDialog就加了句 SetHook();
但运行时按一下F2,却会弹出两个窗口,为什么呢?
#include <windows.h>
HHOOK g_hKeyboard=NULL;
LRESULT CALLBACK KeyboardProc(
  int code,       // hook code
  WPARAM wParam,  // virtual-key code
  LPARAM lParam   // keystroke-message information
)
{
if(VK_F2==wParam)
{
HWND hwnd=::GetForegroundWindow();
int temp=::MessageBox(hwnd,"选是置顶,选否不置顶","置顶?",MB_YESNO);
switch( temp )
{
case IDYES: ::SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);break;
case IDNO:  ::SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);break;
default:break;
}
}
    return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
void SetHook()
{
   g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("hook"),0);
}

解决方案 »

  1.   

    KeyboardProc
    The KeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed. 收到2个消息,按下 和 弹起 按键都会收到消息。所以就有2 个对话框了
      

  2.   

    谢谢提点,
    上网又搜了下,
    已解决
    if(VK_F2==wParam)
    改为
    if(VK_F2==wParam && (lParam>>30 & 1)==1)
    就行了,
    30位好像是标志键之前是按下还是弹起状态的
    用31位应该也行.标志当前是按下还是弹起 ?