您的位置:首页 > 其它

窗口置顶,并且屏蔽系统按键

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: