从Windows服务禁用打印屏幕

时间:2012-10-24 17:49:48

标签: .net windows-services printscreen

是否有任何方法可以阻止用户登录计算机以使用printscreen键。我看着整个网络,无法找到任何方法让它工作。我看到的唯一方法是使用Windows窗体,这不是一个真正的选择。

vb的c#对我来说都没问题。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

好的,就这样......

来自http://go4answers.webhost4life.com/Example/monitor-keys-53505.aspx

    Module Keyboard
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Public Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
Public Declare Function UnhookWindowsHookEx Lib "user32" _
  (ByVal hHook As Integer) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" _
  Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
  ByVal lpfn As KeyboardHookDelegate, ByVal hmod As IntPtr, _
  ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _
  (ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" _
  (ByVal hHook As Integer, _
  ByVal nCode As Integer, _
  ByVal wParam As Integer, _
  ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Public Structure KBDLLHOOKSTRUCT
    Public vkCode As Integer
    Public scanCode As Integer
    Public flags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer
End Structure
' Low-Level Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const LLKHF_EXTENDED As Integer = &H1
Private Const LLKHF_INJECTED As Integer = &H10
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const LLKHF_UP As Integer = &H80
' Virtual Keys
Public Const VK_TAB As Integer = &H9
Public Const VK_CONTROL As Integer = &H11
Public Const VK_ESCAPE As Integer = &H1B
Public Const VK_DELETE As Integer = &H2E
Private Const WH_KEYBOARD_LL As Integer = 13
Public KeyboardHandle As Integer
' Implement this function to block as many
' key combinations as you'd like
Public Function IsHooked( _
  ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
    Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
    Debug.WriteLine(Hookstruct.vkCode = VK_TAB)
    'MessageBox.Show(Hookstruct.vkCode.ToString)
    If (Hookstruct.vkCode = VK_ESCAPE) And _
      CBool(GetAsyncKeyState(VK_CONTROL) _
      And &H8000) Then
        Call HookedState("Ctrl + Esc blocked")
        Return True
    End If
    If (Hookstruct.vkCode = VK_TAB) And _
      CBool(Hookstruct.flags And _
      LLKHF_ALTDOWN) Then
        Call HookedState("Alt + Tab blockd")
        Return True
    End If
    If (Hookstruct.vkCode = VK_ESCAPE) And _
      CBool(Hookstruct.flags And _
        LLKHF_ALTDOWN) Then
        Call HookedState("Alt + Escape blocked")
        Return True
    End If
    '' disable PrintScreen here
    If (Hookstruct.vkCode = 44) Then
        Call HookedState("Print blocked")
        Return True
    End If
    Return False
End Function
Private Sub HookedState(ByVal Text As String)
    Debug.WriteLine(Text)
End Sub
Public Function KeyboardCallback(ByVal Code As Integer, _
  ByVal wParam As Integer, _
  ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    If (Code = HC_ACTION) Then
        Debug.WriteLine("Calling IsHooked")
        If (IsHooked(lParam)) Then
            Return 1
        End If
    End If
    Return CallNextHookEx(KeyboardHandle, _
      Code, wParam, lParam)
End Function
Public Delegate Function KeyboardHookDelegate( _
  ByVal Code As Integer, _
  ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
               As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callback As KeyboardHookDelegate
Public Sub HookKeyboard(ByRef f As Form)
    callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0)
    MessageBox.Show(KeyboardHandle.ToString)
    Call CheckHooked()
End Sub
Public Sub CheckHooked()
    If (Hooked()) Then
        Debug.WriteLine("Keyboard hooked")
    Else
        Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
    End If
End Sub
Private Function Hooked() As Boolean
    Hooked = KeyboardHandle <> 0
End Function
Public Sub UnhookKeyboard()
    If (Hooked()) Then
        Call UnhookWindowsHookEx(KeyboardHandle)
    End If
End Sub

结束模块

另外,我看了这个和其他几个,似乎是你正在寻找的东西

.NET中的全球系统挂钩  http://www.codeproject.com/KB/system/globalsystemhook.aspx