I made a simple windows form application using vb.net, the first element of my project "Form1.vb"contains common code such :
Public Class Form1
Public Sub Form1_Load
.....
End Class
I needed to disable "Print Screen" button in my application and I found the following code using 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
Now how to use this code to detect "print screen" button press in order to stop the action?