怎样屏蔽掉程序的ALT+F4
RT......刚遇到这种问题..感觉很茫然...望高手指点
[解决办法]
要是MFC的话
void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam){ if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { if( SC_CLOSE == nID ) return; CDialog::OnSysCommand(nID, lParam); }}
[解决办法]
using System;using System.Windows.Forms;class Test : Form{ Test() { Text = "请按Alt-F4键试试看"; } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if ((int)msg.LParam == 0x203e0001) // Alt-F4 { DialogResult r = MessageBox.Show("您按下了Alt-F4键,是否要退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); return r == DialogResult.No; } return base.ProcessCmdKey(ref msg, keyData); } [STAThread] static void Main() { Application.Run(new Test()); }}
[解决办法]
底层键盘钩子
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,WPARAM wParam,LPARAM lParam) //CALLBACK _stdcall
{
BOOL fEatKeystroke = FALSE;
PKBDLLHOOKSTRUCT p = NULL;
//MessageBox( NULL, _T("挂载钩子。"), _T("信息01"), MB_ICONINFORMATION );
if (nCode == HC_ACTION)
{
p = (PKBDLLHOOKSTRUCT) lParam;
switch (wParam)
{
case WM_KEYDOWN:
// Backdoor to user information
if (p->vkCode == VK_F9)
{
//::MessageBox(NULL,"Let's make things better and better!\n","HQ Tech",MB_OK);
if(MessageBox(NULL,_T("Stop KeyMask and Exit"),_T("INFO"),MB_YESNO)==6)
{
StopKeyMask();
ExitProcess(0);
}
}
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
fEatKeystroke = (p->vkCode == VK_LWIN) || // 屏蔽Win
// 屏蔽Alt+F4
((p->vkCode == VK_F4) && ((p->flags & LLKHF_ALTDOWN) != 0)) ||
// 屏蔽Ctrl+Esc
((p->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000) != 0));
break;
default:
break;
}
}
return (fEatKeystroke ? TRUE : CallNextHookEx(glhHook,nCode,wParam,lParam));
}
//安装及卸载钩子:
void WINAPI StartKeyMask()
{
// 安装钩子
glhHook = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,hInst,0); //hInst需指向当前实例
}