1、建钩子;
m_hhook=SetWindowsHookEx(WH_CALLWNDPROC,(HOOKPROC)HookProc,AfxGetApp()->m_hInstance,0);
2、建钩子处理函数:
LRESULT CALLBACK HookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
CWPSTRUCT *p=(CWPSTRUCT*)lParam;
if(p->message =WM_CLOSE) 
{
这里应该怎么做??????????????????
}
  return CallNextHookEx(m_hhook , nCode, wParam, lParam );
}我的疑问就是,钩子处理函数,中如果取得了WM_CLOSE,应该怎么办????
最好能写出代码来,还有,我的这个钩子,没有作为DLL,

解决方案 »

  1.   

    直接返回就可以了
    不过好像hook那个WM_CLOSE不行吧?
    应该hook那个WM_SYSCOMMAND,然后判断wParam为SC_CLOSE时,直接返回就行了
      

  2.   

    我知道,必须用DLL来做,我现在改为MFC 标准DLL来做,并添加了一个CHOOK,类.
    其他怎么做,指点一下好吗.
      

  3.   

    去 MSDN 查 WM_SYSCOMMAND 和 SC_CLOSE
      

  4.   

    帖给你看看CWnd::OnSysCommand  
    afx_msg void OnSysCommand( UINT nID, LPARAM lParam );ParametersnIDSpecifies the type of system command requested. This parameter can be any one of the following values: 指定了请求的系统命令的类型。这个参数可以是下列值之一: SC_CLOSE   Close the CWnd object.SC_CLOSE 关闭CWnd对象。 
    SC_HOTKEY   Activate the CWnd object associated with the application-specified hot key. The low-order word of lParam identifies the HWND of the window to activate. SC_HOTKEY 激活与应用程序指定的热键相关的CWnd对象。lParam的低位字标识了要激活的窗口的HWND句柄 
    SC_HSCROLL   Scroll horizontally.SC_HSCROLL 水平滚动。 
    SC_KEYMENU   Retrieve a menu through a keystroke.SC_KEYMENU 通过击键获得菜单。 
    SC_MAXIMIZE (or SC_ZOOM)   Maximize the CWnd object.SC_MAXIMIZE (或SC_ZOOM) 最大化CWnd对象 
    SC_MINIMIZE (or SC_ICON)   Minimize the CWnd object.SC_MINIMIZE (或SC_ICON) 最小化CWnd对象。 
    SC_MOUSEMENU   Retrieve a menu through a mouse click.SC_MOUSEMENU 通过鼠标点击获得菜单。 
    SC_MOVE   Move the CWnd object.SC_MOVE 移动CWnd对象 
    SC_NEXTWINDOW   Move to the next window.SC_NEXTWINDOW 移动到下一个窗口。 
    SC_PREVWINDOW   Move to the previous window.SC_PREVWINDOW 移动到前一个窗口。 
    SC_RESTORE   Restore window to normal position and size.SC_RESTORE 将窗口恢复为普通的位置和大小 
    SC_SCREENSAVE   Executes the screen-saver application specified in the [boot] section of the SYSTEM.INI file.SC_SCREENSAVE 执行SYSTEM.INI文件中[boot]部分指定的屏幕保护应用程序。 
    SC_SIZE   Size the CWnd object.SC_SIZE 调整CWnd对象的大小。 
    SC_TASKLIST   Execute or activate the Windows Task Manager application.SC_TASKLIST 执行或激活Windows的任务管理器应用程序 
    SC_VSCROLL   Scroll vertically.SC_VSCROLL 垂直滚动。 
    lParamIf a Control-menu command is chosen with the mouse, lParam contains the cursor coordinates. The low-order word contains the x coordinate, and the high-order word contains the y coordinate. Otherwise this parameter is not used. 如果控制菜单是通过鼠标选择的,则lParam中包含了光标的位置。低位字包含了x轴坐标,高位字包含了y轴坐标;否则这个参数没有使用。 SC_HOTKEY   Activate the window associated with the application-specified hot key. The low-order word of lParam identifies the window to activate.SC_HOTKEY 激活与应用程序指定的热键相关的窗口。lParam的低位字标识了要激活的窗口。 
    SC_SCREENSAVE   Execute the screen-save application specified in the Desktop section of Control Panel.SC_HOTKEY 激活与应用程序指定的热键相关的窗口。lParam的低位字标识了要激活的窗口。 
    ResThe framework calls this member function when the user selects a command from the Control menu, or when the user selects the Maximize or the Minimize button. By default, OnSysCommand carries out the Control-menu request for the predefined actions specified in the preceding table. In WM_SYSCOMMAND messages, the four low-order bits of the nID parameter are used internally by Windows. When an application tests the value of nID, it must combine the value 0xFFF0 with the nID value by using the bitwise-AND operator to obtain the correct result.The menu items in a Control menu can be modified with the GetSystemMenu, AppendMenu, InsertMenu, and ModifyMenu member functions. Applications that modify the Control menu must process WM_SYSCOMMAND messages, and any WM_SYSCOMMAND messages not handled by the application must be passed on to OnSysCommand. Any command values added by an application must be processed by the application and cannot be passed to OnSysCommand.An application can carry out any system command at any time by passing a WM_SYSCOMMAND message to OnSysCommand. Accelerator (shortcut) keystrokes that are defined to select items from the Control menu are translated into OnSysCommand calls; all other accelerator keystrokes are translated into WM_COMMAND messages.当用户从控制菜单选择了一个命令,或者用户选择了最大化户最小化按钮时,框架调用这个函数。在缺省情况下,OnSysCommand执行控制菜单对前面表格中描述的预定义动作的请求。在WM_SYSCOMMAND消息中,nID参数的低四位被Windows内部使用。当应用程序测试nID的值时,它必须用位与操作符AND将值0xFFF0与nID的值组合在一起以获得正确的结果。 控制菜单中的菜单项可以用GetSystemMenu,AppendMenu,InsertMenu和ModifyMenu成员函数来修改。修改了控制菜单的应用程序必须处理WM_SYSCOMMAND消息,并且应用程序没有处理的任何WM_SYSCOMMAND消息都必须被发送给OnSysCommand。应用程序加入的任何命令值必须由应用程序处理,并且不能被传递给OnSysCommand。应用程序可以在任何时候通过向OnSysCommand发送WM_SYSCOMMAND消息来执行任何系统命令。为选择控制菜单中的项的加速键(快捷方式)被转换为OnSysCommand调用;所有其它的加速键被转换为WM_COMMAND消息 Note   This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.注意 框架调用这个成员函数以允许你的应用程序处理一个Windows消息。传递给你的成员函数的参数反映了接收到消息时框架接收到的参数。如果你调用了这个函数的基类实现,则该实现将使用最初传递给消息的参数(而不是你提供给这个函数的参数)。 
      

  5.   

    LRESULT CALLBACK HookProc( int nCode, WPARAM wParam, LPARAM lParam )
    {
    CWPSTRUCT *p=(CWPSTRUCT*)lParam;
    if(p->message =WM_SYSCOMMAND) 
    {
      if(p->wParam == SC_CLOSE)
        return 0;
    }
      return CallNextHookEx(m_hhook , nCode, wParam, lParam );
    //如果不行,用GetMsgProc钩子,这个可以屏蔽消息