阻止vb.net中的“打印屏幕”按钮

时间:2015-12-24 01:32:44

标签: vb.net printscreen

我使用vb.net创建了一个简单的Windows窗体应用程序,我的项目“Form1.vb”的第一个元素包含公共代码:

Public Class Form1
    Public Sub  Form1_Load
.....
End Class

我需要在我的应用程序中禁用“Print Screen”按钮,我使用google找到了以下代码:

    Option Explicit On

Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class HotKeyClass
    Inherits Control

    <DllImport("user32.dll")> _
    Private Shared Function RegisterHotKey(hWnd As IntPtr, id As Integer, fsModifiers As Integer, vk As Integer) As Boolean
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function UnregisterHotKey(hWnd As IntPtr, id As Integer) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As UInteger, dwExtraInfo As Integer)
    End Sub

    Public Event HotKeyPressed(Key As Keys, Modifer As HotKeyModifer)

    Private Const KEYEVENTF_KEYUP = &H2
    Private Const WM_HOTKEY = &H312

    Private m_Modifer As Integer
    Private m_Key As Integer
    Private m_Id As Integer

    'Конструктор
    Sub New()
        Me.BackColor = Color.Black
        Me.Visible = False
    End Sub

    'Обработка сообщений
    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_HOTKEY Then
            'Dim idHotKey As Integer = CInt(m.WParam) 'Получаем идентификатор комбинации
            RaiseEvent HotKeyPressed(m_Key, m_Modifer)
        End If

        MyBase.WndProc(m)
    End Sub

    'Переопределяем, получаем уникальный ID
    Public Overrides Function GetHashCode() As Integer
        Return m_Modifer ^ m_Key ^ Me.Handle.ToInt32()
    End Function

    'Переопределяем, снять регистрацию клавиш
    Protected Overrides Sub Dispose(disposing As Boolean)
        UnregisterHotKey(Me.Handle, Me.GetType().GetHashCode())
        MyBase.Dispose(disposing)
    End Sub

    'Регистрация клавиш
    Public Function Register(Key As Keys, Modifer As HotKeyModifer) As Boolean
        m_Id = Me.GetHashCode()
        m_Modifer = Modifer
        m_Key = Key

        Return RegisterHotKey(Me.Handle, m_Id, m_Modifer, m_Key)
    End Function

    'Снять регистрацию клавиш
    Public Function Unregiser() As Boolean
        Return UnregisterHotKey(Me.Handle, m_Id)
    End Function


    'Для эмуляции нажатия Ctrl + V
    Public Shared Sub EmulateControlV()
        keybd_event(Keys.ControlKey, 0, 0, 0)
        keybd_event(Keys.V, 0, 0, 0)
        keybd_event(Keys.V, 0, KEYEVENTF_KEYUP, 0)
        keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0)
    End Sub

End Class

<Flags> _
Public Enum HotKeyModifer As UInteger
    NO_MODIFICATION = 0
    ALT = 1
    CONTROL = 2
    SHIFT = 4
    WIN = 8
End Enum

现在如何使用此代码检测“打印屏幕”按钮按下以停止操作?

1 个答案:

答案 0 :(得分:-2)

我已在此处说过:https://stackoverflow.com/questions/34238442/how-to-disable-printscreen-functionality-on-32-bit-64-bit-machine/34238923#34238923但也许需要重复: - )

您可以监控按键,当用户按下打印屏幕(键代码44)时,执行Clipboard.Clear()。这是毫无意义的,因为您在应用程序中执行的任何操作都不会阻止用户使用移动电话或使用Windows内置的屏幕捕获软件(剪切工具)拍摄屏幕图片。