在编程过程中,有时候需要捕获一些消息,来进行操作,钩子一共有14种,
// HookTest1Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "HookTest1.h"
#include "HookTest1Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
CString str,str1;
int num=0;
HWND hwnd;
CDC *cdc;
HHOOK hmouse,hkeyboard;
LRESULT CALLBACK MouseProc(int ncode, WPARAM wParam,LPARAM lParam)
{
str=_T("鼠标操作次数: ");
num++;
str1.Format(_T("%d"),num);
str=str+str1;
int s=str.GetLength();
cdc->TextOut(50,50,str,s);
return CallNextHookEx(hmouse, ncode, wParam, lParam);
}
LRESULT CALLBACK KeyProc(int ncode,WPARAM wParam,LPARAM lParam)
{
int s=0;
if(wParam==VK_F10)
{
::SendMessage(hwnd,WM_CLOSE,0,0);
::UnhookWindowsHookEx(hmouse);
::UnhookWindowsHookEx(hkeyboard);
}
else
{
switch(wParam)
{
case VK_F2:
str=_T("按键F2");
s=str.GetLength();
cdc->TextOut(50,150,str,s);
break;
case VK_F3:
str=_T("按键F3");
s=str.GetLength();
cdc->TextOut(50,150,str,s);
break;
break;
case 0x41:
str=_T("按键A");
s=str.GetLength();
cdc->TextOut(50,150,str,s);
break;
case 0x42:
str=_T("按键B");
s=str.GetLength();
cdc->TextOut(50,150,str,s);
break;
}
return CallNextHookEx(hkeyboard, ncode, wParam, lParam);
}
}
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CHookTest1Dlg 对话框
CHookTest1Dlg::CHookTest1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CHookTest1Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHookTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CHookTest1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CHookTest1Dlg::OnBnClickedOk)
END_MESSAGE_MAP()
// CHookTest1Dlg 消息处理程序
BOOL CHookTest1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
cdc=GetDC();
hwnd=this->m_hWnd;
hmouse=SetWindowsHookEx(WH_MOUSE,MouseProc,NULL,GetCurrentThreadId());
hkeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyProc,NULL,GetCurrentThreadId());
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CHookTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CHookTest1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CHookTest1Dlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CHookTest1Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnOK();
}
以上方法即可获得当前运行程序内的所有鼠标和键盘的消息,程序中只显示了部分按键消息,并未全部处理,然后介绍如何获取全部线程的鼠标和键盘消息。
hook.dll代码
// hook.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include
HHOOK hmouse=NULL;
HHOOK hkeyboard=NULL;
HWND hwnd =NULL;
LRESULT CALLBACK MouseProc(int ncode, WPARAM wParam,LPARAM lParam)
{
return 1;
}
LRESULT CALLBACK KeyProc(int ncode,WPARAM wParam,LPARAM lParam)
{
int s=0;
if(wParam==VK_F10)
{
::SendMessage(hwnd,WM_CLOSE,0,0);
::UnhookWindowsHookEx(hmouse);
::UnhookWindowsHookEx(hkeyboard);
}
else
{
switch(wParam)
{
}
return CallNextHookEx(hkeyboard, ncode, wParam, lParam);
}
}
void SetHookEx(HWND hWnd)
{
hwnd=hWnd;
hmouse= SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle(L"hook"),0);
hkeyboard = SetWindowsHookEx(WH_KEYBOARD,KeyProc,GetModuleHandle(L"hook"),0);
}
(2)然后在测试客户端内增加钩子函数
// hook.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include
HHOOK hmouse=NULL;
HHOOK hkeyboard=NULL;
#pragma data_seg("MySeg")
HWND hwnd =NULL;
#pragma data_seg()
#pragma comment (linker,"/section:MySec,RWS")
//将句柄设置成共享
LRESULT CALLBACK MouseProc(int ncode, WPARAM wParam,LPARAM lParam)
{
return 1;
}
LRESULT CALLBACK KeyProc(int ncode,WPARAM wParam,LPARAM lParam)
{
if(wParam==VK_F1)
{
::SendMessage(hwnd,WM_CLOSE,0,0);
::UnhookWindowsHookEx(hmouse);
::UnhookWindowsHookEx(hkeyboard);
}
return 1;
}
void SetHookEx(HWND hWnd)
{
hwnd=hWnd;
hmouse= SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle(L"hook"),0);
hkeyboard = SetWindowsHookEx(WH_KEYBOARD,KeyProc,GetModuleHandle(L"hook"),0);
}