用钩子截去ALT+F4 ===========================================================================
以下在stdafx.cpp 中定义的全局变量和函数 应该在.h中声明
HHOOK g_hKeyboard = NULL; //钩子型全局变量
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam) //定义鼠标钩子过程
{
if(VK_F4 == wParam && ( 1 == (lParam>>29 &1))) //截掉ALT+F4 lParam除以29次2 lParam是一个32位整数
{
return 1;
}
else
{
return ::CallNextHookEx(::g_hKeyboard,nCode,wParam,lParam);
}
//lParam 各位的含义
// 0~15位 连续接键重复次数
// 16~23位 指示扫描码(依赖于厂家)
// 24位 是否是功能键或者小键盘区
// 25~28位 无
// 29位 ALT是否按下
// 30位 当前键是按下的
// 31位 如果此键正在被按下 值为0
// 右移29位后 之前的第29位就变成了最右边的位
// 对于 1 来说 除了个位其余位都是0
// 右移29位后 和 1 进行 与运算 就知道最右边是1是0了
}
Dlg.cpp
BOOL CMy0712ADlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 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
::g_hKeyboard=
::SetWindowsHookExA //最后设置的钩子会最先捞消息
(
WH_KEYBOARD, //键盘钩子
::KeyboardProc,
NULL,
::GetCurrentThreadId()
);
return TRUE; // return TRUE unless you set the focus to a control
}
以下在stdafx.cpp 中定义的全局变量和函数 应该在.h中声明
HWND g_hwnd=NULL; //窗口句柄型全局变量
HHOOK g_hKeyboard = NULL; //钩子型全局变量
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam) //定义鼠标钩子过程
{
if(VK_F2 == wParam)
{
::SendMessageA(g_hwnd,WM_CLOSE,0,0);
//发关闭消息后,最好将已安装的钩子移除
::UnhookWindowsHookEx(::g_hKeyboard);
::UnhookWindowsHookEx(::g_hMouse);
}
return 1;
}
Dlg.cpp InitDlg()中
添加
::g_hwnd=this->m_hWnd;