程序运行时用到TaskKeyHook.dll,编译时用到TaskKeyHook.lib,并把下面的类添加到应用程序中,调用方法为:
CTaskKeyMgr::Disable(CTaskKeyMgr::TASKMGR|CTaskKeyMgr::TASKKEYS|CTaskKeyMgr::TASKBAR,FALSE,FALSE);
//取消屏蔽
CTaskKeyMgr::Disable(CTaskKeyMgr::TASKMGR|CTaskKeyMgr::TASKKEYS|CTaskKeyMgr::TASKBAR,TRUE,FALSE);
//实施屏蔽
CTaskKeyMgr.h:
#pragma once
#include "TaskKeyHook.h"
class CTaskKeyMgr {
public:
enum {
TASKMGR = 0x01, // disable task manager (Ctrl+Alt+Del)
TASKKEYS = 0x02, // disable task keys (Alt-TAB, etc)
TASKBAR = 0x04, // disable task bar
ALL=0xFFFF // disable everything :(
};
static void Disable(DWORD dwItem,BOOL bDisable,BOOL bBeep=FALSE);
static BOOL IsTaskMgrDisabled();
static BOOL IsTaskBarDisabled();
static BOOL AreTaskKeysDisabled() {
return ::AreTaskKeysDisabled(); // call DLL
}
};
CTaskKeyMgr.cpp:
#include "StdAfx.h"
#include "TaskKeyMgr.h"
#define HKCU HKEY_CURRENT_USER
// Magic registry key/value for "Remove Task Manager" policy.
//
LPCTSTR KEY_DisableTaskMgr =
"Software//Microsoft//Windows//CurrentVersion//Policies//System";
LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr";
//////////////////
// Disable task-key related stuff.
//
// dwFlags = what to disable
// bDisable = disable (TRUE) or enable (FALSE)
// bBeep = whether to beep for illegal keys (TASKKEYS only)
//
void CTaskKeyMgr::Disable(DWORD dwFlags, BOOL bDisable, BOOL bBeep)
{
// task manager (Ctrl+Alt+Del)
if (dwFlags & TASKMGR) {
HKEY hk;
if (RegOpenKey(HKCU, KEY_DisableTaskMgr,&hk)!=ERROR_SUCCESS)
RegCreateKey(HKCU, KEY_DisableTaskMgr, &hk);
if (bDisable) { // disable TM: set policy = 1
DWORD val=1;
RegSetValueEx(hk, VAL_DisableTaskMgr, NULL,
REG_DWORD, (BYTE*)&val, sizeof(val));
} else { // enable TM: remove policy
RegDeleteValue(hk,VAL_DisableTaskMgr);
}
}
// task keys (Alt-TAB etc)
if (dwFlags & TASKKEYS)
::DisableTaskKeys(bDisable,bBeep); // install keyboard hook
// task bar
if (dwFlags & TASKBAR) {
HWND hwnd = FindWindow("Shell_traywnd", NULL);
EnableWindow(hwnd, !bDisable);
}
}
BOOL CTaskKeyMgr::IsTaskBarDisabled()
{
HWND hwnd = FindWindow("Shell_traywnd", NULL);
return IsWindow(hwnd) ? !IsWindowEnabled(hwnd) : TRUE;
}
BOOL CTaskKeyMgr::IsTaskMgrDisabled()
{
HKEY hk;
if (RegOpenKey(HKCU, KEY_DisableTaskMgr, &hk)!=ERROR_SUCCESS)
return FALSE; // no key ==> not disabled
DWORD val=0;
DWORD len=4;
return RegQueryValueEx(hk, VAL_DisableTaskMgr,
NULL, NULL, (BYTE*)&val, &len)==ERROR_SUCCESS && val==1;
}