窗口置顶,并且屏蔽系统按键
2011-05-10 09:04
267 查看
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sbfm As MsgboxForm
sbfm = New MsgboxForm()
sbfm.ShowDialog()
End Sub
End Class
'MsgboxForm警告窗体
Imports WinFromHook.FormHook
Imports Microsoft.Win32
Public Class MsgboxForm
Dim hhkLowLevelKybd As Long
Public KeyBoardHookProcedure As HookProc
Dim fh As FormHook
Private Sub MsgboxForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
Button1.Text = "退出"
Button2.Text = "可用"
Label1.Text = "最大化警告窗体屏蔽键盘(Ctrl+Alt+Delete)"
Destroy()
End Sub
Private Sub MsgboxForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If hhkLowLevelKybd <> 0 Then UnhookWindowsHookEx(hhkLowLevelKybd)
End Sub
Public Sub SetCtrlAltDeleteStatus(ByVal value As Integer)
'1不可用 0可用
Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim key1 As Microsoft.Win32.RegistryKey = key.CreateSubKey("Software//Microsoft//Windows//CurrentVersion//Policies//System")
key1.SetValue("DisableTaskMgr", 1, Microsoft.Win32.RegistryValueKind.DWord)
Dim r As RegistryKey
r = Registry.CurrentUser.OpenSubKey("Software//Microsoft//Windows//CurrentVersion//Policies//System", True)
If r Is Nothing Then
r = Registry.CurrentUser.CreateSubKey("Software//Microsoft//Windows//CurrentVersion//Policies//System")
End If
r.SetValue("DisableTaskMgr", value, RegistryValueKind.DWord)
End Sub
Private Sub Destroy()
fh = New FormHook()
fh.Hook_Start()
Dim handleTaskMask As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", Nothing)
ShowWindow(handleTaskMask, 0)
Dim handleDeskTop As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", Nothing)
ShowWindow(handleDeskTop, 0)
SetCtrlAltDeleteStatus(1)
Dim p As Boolean = True
SystemParametersInfo(spi_screensaverrunning, 1, p, 0) '关Ctrl+Alt+Del窗口,SystemParametersInfo(97, False, A, 0),uParam为布尔型
End Sub
Private Sub Recover()
fh = New FormHook()
fh.Hook_Clear()
Dim handle As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", Nothing)
ShowWindow(handle, 1)
Dim handleDeskTop As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", Nothing)
ShowWindow(handleDeskTop, 1)
SetCtrlAltDeleteStatus(0)
Dim p As Boolean = True
SystemParametersInfo(spi_screensaverrunning, 0, p, 0) '开Ctrl+Alt+Del窗口,SystemParametersInfo(97, False, A, 0),uParam为布尔型
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Recover()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
'类模块 FormHook.vb
Imports System.Runtime.InteropServices
Public Class FormHook
'委托
Public Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
Public hHook As Integer = 0
Public Const WH_KEYBOARD_LL = 13
'LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。
Public KeyBoardHookProcedure As HookProc
'键盘Hook结构函数
Public Structure KeyBoardHookStruct
Dim vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
'设置钩子
Public Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
'抽掉钩子
Public Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal idHook As Integer) As Boolean
'调用下一个钩子
Public Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
Public Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
Public Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal name As String) As IntPtr
Public Declare Function FindWindowEx Lib "User32.dll" Alias "FindWindowExA" (ByVal ph As IntPtr, ByVal ch As IntPtr, ByVal cn As String, ByVal wn As String) As IntPtr
Public Declare Function ShowWindow Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Long) As Boolean
Public Const spi_screensaverrunning As UInteger = 97
Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uiAction As UInteger, ByVal uiParam As UInteger, ByRef pvParam As Boolean, ByVal fWinIni As UInteger) As Boolean
'自定义事件
Public Sub Hook_Start()
'安装键盘钩子
If hHook = 0 Then
KeyBoardHookProcedure = New HookProc(AddressOf KeyBoardHookProc)
Dim hInstance As IntPtr
hInstance = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName)
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, hInstance, 0)
'如果设置钩子失败.
If hHook = 0 Then
Hook_Clear()
'throw new Exception("设置Hook失败!");
End If
End If
End Sub
'取消钩子事件
Public Sub Hook_Clear()
Dim retKeyboard As Boolean = True
If hHook <> 0 Then
retKeyboard = UnhookWindowsHookEx(hHook)
hHook = 0
End If
'如果去掉钩子失败.
If retKeyboard = False Then Throw New Exception("UnhookWindowsHookEx failed.")
End Sub
'这里可以添加自己想要的信息处理
Public Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
If nCode >= 0 Then
Dim kbh As KeyBoardHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(KeyBoardHookStruct)), KeyBoardHookStruct)
If kbh.vkCode = CInt(Keys.S) And CInt(Control.ModifierKeys) = CInt(Keys.Control) Then ' 截获Ctrl+S
Return 1
End If
If kbh.vkCode = CInt(Keys.Y) And CInt(Control.ModifierKeys) = CInt(Keys.Control) + CInt(Keys.Alt) Then '截获Ctrl+Alt+Y
Return 1
End If
If kbh.vkCode = CInt(Keys.LWin) Then '截获win
Return 1
End If
If kbh.vkCode = CInt(Keys.Tab) And CInt(Control.ModifierKeys) = CInt(Keys.Alt) Then '截获Alt+Tab
Return 1
End If
If kbh.vkCode = CInt(Keys.F4) And CInt(Control.ModifierKeys) = CInt(Keys.Alt) Then '截获Alt + F4
Return 1
End If
If kbh.vkCode = CInt(Keys.E) And CInt(Control.ModifierKeys) = CInt(Keys.LWin) Then '截获win + e
Return 1
End If
If kbh.vkCode = CInt(Keys.Escape) And CInt(Control.ModifierKeys) = CInt(Keys.Alt) Then '截获Alt + esc
Return 1
End If
If CType(Control.ModifierKeys, Integer) = CType(Keys.Control, Integer) + CType(Keys.Alt, Integer) + CType(Keys.Delete, Integer) Then '截获Ctrl+Alt+Delete 不成功
Return 1
End If
If kbh.vkCode = CType(Keys.Escape, Integer) And CType(Control.ModifierKeys, Integer) = CType(Keys.Control, Integer) Then '截获Ctrl + esc
Return 1
End If
End If
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sbfm As MsgboxForm
sbfm = New MsgboxForm()
sbfm.ShowDialog()
End Sub
End Class
'MsgboxForm警告窗体
Imports WinFromHook.FormHook
Imports Microsoft.Win32
Public Class MsgboxForm
Dim hhkLowLevelKybd As Long
Public KeyBoardHookProcedure As HookProc
Dim fh As FormHook
Private Sub MsgboxForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
Button1.Text = "退出"
Button2.Text = "可用"
Label1.Text = "最大化警告窗体屏蔽键盘(Ctrl+Alt+Delete)"
Destroy()
End Sub
Private Sub MsgboxForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If hhkLowLevelKybd <> 0 Then UnhookWindowsHookEx(hhkLowLevelKybd)
End Sub
Public Sub SetCtrlAltDeleteStatus(ByVal value As Integer)
'1不可用 0可用
Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim key1 As Microsoft.Win32.RegistryKey = key.CreateSubKey("Software//Microsoft//Windows//CurrentVersion//Policies//System")
key1.SetValue("DisableTaskMgr", 1, Microsoft.Win32.RegistryValueKind.DWord)
Dim r As RegistryKey
r = Registry.CurrentUser.OpenSubKey("Software//Microsoft//Windows//CurrentVersion//Policies//System", True)
If r Is Nothing Then
r = Registry.CurrentUser.CreateSubKey("Software//Microsoft//Windows//CurrentVersion//Policies//System")
End If
r.SetValue("DisableTaskMgr", value, RegistryValueKind.DWord)
End Sub
Private Sub Destroy()
fh = New FormHook()
fh.Hook_Start()
Dim handleTaskMask As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", Nothing)
ShowWindow(handleTaskMask, 0)
Dim handleDeskTop As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", Nothing)
ShowWindow(handleDeskTop, 0)
SetCtrlAltDeleteStatus(1)
Dim p As Boolean = True
SystemParametersInfo(spi_screensaverrunning, 1, p, 0) '关Ctrl+Alt+Del窗口,SystemParametersInfo(97, False, A, 0),uParam为布尔型
End Sub
Private Sub Recover()
fh = New FormHook()
fh.Hook_Clear()
Dim handle As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", Nothing)
ShowWindow(handle, 1)
Dim handleDeskTop As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", Nothing)
ShowWindow(handleDeskTop, 1)
SetCtrlAltDeleteStatus(0)
Dim p As Boolean = True
SystemParametersInfo(spi_screensaverrunning, 0, p, 0) '开Ctrl+Alt+Del窗口,SystemParametersInfo(97, False, A, 0),uParam为布尔型
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Recover()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
'类模块 FormHook.vb
Imports System.Runtime.InteropServices
Public Class FormHook
'委托
Public Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
Public hHook As Integer = 0
Public Const WH_KEYBOARD_LL = 13
'LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。
Public KeyBoardHookProcedure As HookProc
'键盘Hook结构函数
Public Structure KeyBoardHookStruct
Dim vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
'设置钩子
Public Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
'抽掉钩子
Public Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal idHook As Integer) As Boolean
'调用下一个钩子
Public Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
Public Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
Public Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal name As String) As IntPtr
Public Declare Function FindWindowEx Lib "User32.dll" Alias "FindWindowExA" (ByVal ph As IntPtr, ByVal ch As IntPtr, ByVal cn As String, ByVal wn As String) As IntPtr
Public Declare Function ShowWindow Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Long) As Boolean
Public Const spi_screensaverrunning As UInteger = 97
Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uiAction As UInteger, ByVal uiParam As UInteger, ByRef pvParam As Boolean, ByVal fWinIni As UInteger) As Boolean
'自定义事件
Public Sub Hook_Start()
'安装键盘钩子
If hHook = 0 Then
KeyBoardHookProcedure = New HookProc(AddressOf KeyBoardHookProc)
Dim hInstance As IntPtr
hInstance = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName)
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, hInstance, 0)
'如果设置钩子失败.
If hHook = 0 Then
Hook_Clear()
'throw new Exception("设置Hook失败!");
End If
End If
End Sub
'取消钩子事件
Public Sub Hook_Clear()
Dim retKeyboard As Boolean = True
If hHook <> 0 Then
retKeyboard = UnhookWindowsHookEx(hHook)
hHook = 0
End If
'如果去掉钩子失败.
If retKeyboard = False Then Throw New Exception("UnhookWindowsHookEx failed.")
End Sub
'这里可以添加自己想要的信息处理
Public Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
If nCode >= 0 Then
Dim kbh As KeyBoardHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(KeyBoardHookStruct)), KeyBoardHookStruct)
If kbh.vkCode = CInt(Keys.S) And CInt(Control.ModifierKeys) = CInt(Keys.Control) Then ' 截获Ctrl+S
Return 1
End If
If kbh.vkCode = CInt(Keys.Y) And CInt(Control.ModifierKeys) = CInt(Keys.Control) + CInt(Keys.Alt) Then '截获Ctrl+Alt+Y
Return 1
End If
If kbh.vkCode = CInt(Keys.LWin) Then '截获win
Return 1
End If
If kbh.vkCode = CInt(Keys.Tab) And CInt(Control.ModifierKeys) = CInt(Keys.Alt) Then '截获Alt+Tab
Return 1
End If
If kbh.vkCode = CInt(Keys.F4) And CInt(Control.ModifierKeys) = CInt(Keys.Alt) Then '截获Alt + F4
Return 1
End If
If kbh.vkCode = CInt(Keys.E) And CInt(Control.ModifierKeys) = CInt(Keys.LWin) Then '截获win + e
Return 1
End If
If kbh.vkCode = CInt(Keys.Escape) And CInt(Control.ModifierKeys) = CInt(Keys.Alt) Then '截获Alt + esc
Return 1
End If
If CType(Control.ModifierKeys, Integer) = CType(Keys.Control, Integer) + CType(Keys.Alt, Integer) + CType(Keys.Delete, Integer) Then '截获Ctrl+Alt+Delete 不成功
Return 1
End If
If kbh.vkCode = CType(Keys.Escape, Integer) And CType(Control.ModifierKeys, Integer) = CType(Keys.Control, Integer) Then '截获Ctrl + esc
Return 1
End If
End If
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
End Class
相关文章推荐
- 屏蔽系统鼠标按键
- Ring3下无驱动移除winlogon.exe进程ctrl+alt+del,win+u,win+l三个系统热键,非屏蔽热键(子类化SAS 窗口)
- JavaScript 获取按键,并屏蔽系统 Window 事件
- 屏幕锁定源程序(屏蔽一般的系统热键以及键盘上任何你想要屏蔽的按键)
- 基于Android6.0源码修改屏蔽系统下方的虚拟按键以及上方的状态栏
- 弹出层(Div)屏蔽父窗口并且让父窗口变暗
- 钩子编程(HOOK) 屏蔽全部按键、鼠标及系统功能键 (4)
- win8.1系统下,点击一个窗口,【当前活动窗口】该窗口无法置顶
- 弹出层(Div)屏蔽父窗口并且让父窗口变暗
- Qt 屏蔽系统按键、任务管理器、任务栏、鼠标,修改注册表
- wpf屏蔽窗口系统菜单
- 钩子编程(HOOK) 屏蔽所有按键、鼠标及系统功能键 (4)
- 弹出层(Div)屏蔽父窗口并且让父窗口变暗
- 如何不屏蔽Android系统的返回按键
- 弹出层(Div)屏蔽父窗口并且让父窗口变暗
- 屏蔽系统按键(最简单方法)
- 怎样不屏蔽Android系统的返回按键
- [置顶] Android N 多窗口的应用——画中画
- 点击弹出窗口,并且背景变暗渐变
- vc将窗口置顶的SetWindowPos()函数以及相关