1

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?

ali_m
  • 109
  • 1
  • 12
  • 1
    Possible duplicate of [Disabling Print Screen Command](http://stackoverflow.com/questions/23485438/disabling-print-screen-command). Also see http://stackoverflow.com/q/3889816/62576 – Ken White Dec 24 '15 at 01:34
  • For beginners this is not entry level stuff, do you know what that code does and why? Its best to know so you can find out **what is not working and why**. Also what if the user has a capturing program, this wouldnt stop a user at all from capturing what they want... Just a thought... – Trevor Dec 24 '15 at 01:47
  • I never new so many VB apps guarded **Top Secret** stuff that Print Screen, Windows Shut down, keyboards and App Exit had to be disabled. – Ňɏssa Pøngjǣrdenlarp Dec 24 '15 at 02:01

2 Answers2

0

Simply use the following code to achieve your purpose

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

      ' Prevent/Disable Print Screen
      If e.KeyData = Keys.PrintScreen Then
        Clipboard.Clear()
      End If

    End Sub
knoami
  • 75
  • 1
  • 2
  • 7
-3

I already said this here: https://stackoverflow.com/questions/34238442/how-to-disable-printscreen-functionality-on-32-bit-64-bit-machine/34238923#34238923 but maybe it needs repeating :-)

You can monitor key presses and when the user pushes print screen (key code 44) execute a Clipboard.Clear(). It's pretty pointless though as anything you do in your application doesn't prevent the user from taking a picture of the screen with a mobile phone or using the screen capture software that is built into Windows (snipping tool).

Community
  • 1
  • 1
Jeroen
  • 460
  • 6
  • 14