第九章 keyhook windows程序设计王艳平版
// MainDir.h文件 // 07MainDir
#include <afxwin.h>
class CMyApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CMainDialog : public CDialog
{
public:
CMainDialog(CWnd* pParentWnd = NULL);
protected:
virtual BOOL OnInitDialog();
virtual void OnCancel();
afx_msg long OnHookKey(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
};
// KeyHookApp.cpp文件
#include "resource.h"
#include "KeyHookApp.h"
#include "../09KeyHookLib/KeyHookLib.h"
#pragma comment(lib, "09KeyHookLib")
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
CMainDialog dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
return FALSE;
}
CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAIN, pParentWnd)
{
}
BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
ON_MESSAGE(HM_KEY, OnHookKey)
END_MESSAGE_MAP()
BOOL CMainDialog::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(theApp.LoadIcon(IDI_MAIN), FALSE);
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0,?
0, 0, SWP_NOSIZE|SWP_NOREDRAW|SWP_NOMOVE);
// 安裝鉤子
if(!SetKeyHook(TRUE, 0, m_hWnd))
MessageBox("安裝鉤子失敗!");
return TRUE;
}
void CMainDialog::OnCancel()
{
// 卸載鉤子
SetKeyHook(FALSE);
CDialog::OnCancel();
return;
}
long CMainDialog::OnHookKey(WPARAM wParam, LPARAM lParam)
{
// 此時參數wParam為用戶按鍵的虛擬鍵碼,
// lParam參數包含按鍵的重復次數、掃描碼、前一個按鍵狀態等信息
char szKey[80];
::GetKeyNameText(lParam, szKey, 80);
CString strItem;
strItem.Format(" 用戶按鍵:%s \r\n", szKey);
// 添加到編輯框中
CString strEdit;
GetDlgItem(IDC_KEYMSG)->GetWindowText(strEdit);
GetDlgItem(IDC_KEYMSG)->SetWindowText(strItem + strEdit);
::MessageBeep(MB_OK);
return 0;
}
總結
以上是生活随笔為你收集整理的第九章 keyhook windows程序设计王艳平版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中科点击:大数据应用的六种商业模式
- 下一篇: 学士后博客开通啦