Hook(鉤子)就是對Windows系統的一些消息或是API函數進行攔截和監控的處理平臺,讓能夠根據程序員的設置獲取其感興趣的信息。程序員
這裏主要是介紹一下Hook攔截鼠標消息和鍵盤消息。app
下面是CALLBACK Proc 回調函數 和 CallNextHookEx函數less
LRESULT CALLBACK HookProc
(
int nCode, //指定是否須要處理該消息
WPARAM wParam,
LPARAM lParam //包含該消息的附加消息 ,
);
這個回調函數的名字能夠隨你取,但形式可必定要知足以上要求,其實鉤子的回調函數和Windows的差很少一個德行。看看鉤子函數的返回值,如果返回非0值,表示咱們已經本身處理了該消息,則消息就不被傳遞到目標窗口過程。
看看LRESULT CallNextHookEx
(HHOOK hhk;
int nCode;
WPARAM wParam;
ide
LPARAM lParam;)這個函數把鉤子信息傳遞給下一個鉤子函數,也就是能夠理解成把車輛放行到下一個檢查站,這個能夠根據本身的須要進行調用。如果咱們只設定了一個鉤子函數,那麼咱們假設把鉤子消息用CallNextHookEx傳給下個鉤子函數,由於不存在因此就傳遞迴了目標窗口函數。函數
先建立一個MFC BaseDialog 工程佈局
XXXDlg.cpp代碼this
// MbHookDlg.cpp : implementation file // #include "stdafx.h" #include "MbHook.h" #include "MbHookDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CAboutDlg dialog used for App About class CAboutDlg : public CDialogEx { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation 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() // CMbHookDlg dialog CMbHookDlg::CMbHookDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CMbHookDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMbHookDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CMbHookDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() END_MESSAGE_MAP() ////////////////////////////////////////////////////////// //Hook的過程函數 HHOOK hook; HWND hWnd; LRESULT CALLBACK MessageBoxProc( INT nCode,WPARAM wParam,LPARAM lParam ) { if(nCode<0) return CallNextHookEx(hook,nCode,wParam,lParam); if (wParam == VK_F3)//當按「F3」鍵時卸載Hook { EndDialog(hWnd,NULL);//關閉窗口 UnhookWindowsHookEx(hook);//卸載HOOk函數 }else { return 1; } } // CMbHookDlg message handlers BOOL CMbHookDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. 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); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here hWnd= m_hWnd; hook=SetWindowsHookEx(//WH_MOUSE//設置鼠標鉤子 WH_KEYBOARD//設置鍵盤鉤子 ,MessageBoxProc,NULL,GetCurrentThreadId()); return TRUE; // return TRUE unless you set the focus to a control } void CMbHookDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CMbHookDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CMbHookDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); }
HHOOK SetWindowsHookEx(spa
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。線程