Você está na página 1de 5

Form1

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
'~~~ Calling it and passing the name of the form to be displayed
Dim myObj As LockScreen = New LockScreen
myObj.LockSystemAndShow(Form2)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Close()
End Sub
Private Sub LockScreenNowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles LockScreenNowToolStripMenuItem.Click
Dim myObj As LockScreen = New LockScreen
myObj.LockSystemAndShow(Form2)
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ExitToolStripMenuItem.Click
Close()
End Sub
End Class

Form 2

Public Class Form2


Dim bolExit As Boolean = False
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If bolExit = False Then
e.Cancel = True
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If TextBox1.Text.Trim.ToLower = "kaizer" Then
bolExit = True
MessageBox.Show("Screen terbuka", "Success")
Me.Close()
Else
MessageBox.Show("Nah nah nah wkwkwk password salah", "Password Salah")
End If
End Sub
End Class

LockScreen.vb
Imports System.Threading
Public NotInheritable Class LockScreen
Private myForm As Form
#Region " System Locking Methods "
Public Function LockSystemAndShow(ByVal myDForm As Form) As Boolean
myForm = myDForm
Dim locker As Screen
locker = Screen.PrimaryScreen
Dim background As Bitmap = New Bitmap(locker.Bounds.Width, locker.Bounds.Height)
Using g As Graphics = Graphics.FromImage(background)
g.CopyFromScreen(0, 0, 0, 0, locker.Bounds.Size)
Using br As Brush = New SolidBrush(Color.FromArgb(192, Color.Black))
g.FillRectangle(br, locker.Bounds)
End Using
End Using
Dim originalThread As IntPtr
Dim originalInput As IntPtr
Dim newDesktop As IntPtr
originalThread = GetThreadDesktop(Thread.CurrentThread.ManagedThreadId)
originalInput = OpenInputDesktop(0, False, DESKTOP_SWITCHDESKTOP)
newDesktop = CreateDesktop("Desktop" & Guid.NewGuid().ToString(), Nothing,
Nothing, 0, GENERIC_ALL, Nothing)

SetThreadDesktop(newDesktop)
SwitchDesktop(newDesktop)
Dim newThread As Thread = New Thread(AddressOf NewThreadMethod)
newThread.CurrentCulture = Thread.CurrentThread.CurrentCulture
newThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture
newThread.Start(New LockSystemParameters(newDesktop, background))
newThread.Join()
SwitchDesktop(originalInput)
SetThreadDesktop(originalThread)
CloseDesktop(newDesktop)
CloseDesktop(originalInput)
Return True
End Function
Private Sub NewThreadMethod(ByVal params As Object)
Dim v As LockSystemParameters = DirectCast(params, LockSystemParameters)
SetThreadDesktop(v.NewDesktop)
Using f As Form = New BackgroundForm
f.Show()
myForm.ShowDialog()
f.BackgroundImage = Nothing
Application.DoEvents()
Thread.Sleep(250)
End Using
End Sub
#End Region
End Class

LockSystemParameters.vb

Friend Class LockSystemParameters


Public NewDesktop As IntPtr
Public Background As Bitmap
Public Sub New(ByVal newDesktop As IntPtr, ByVal background As Bitmap)
Me.NewDesktop = newDesktop
Me.Background = background
End Sub
End Class

Native.vb
Imports System.Runtime.InteropServices
<HideModuleName()> _
Friend Module Native
Public Const GENERIC_ALL As Integer = &H10000000
Public Const DESKTOP_SWITCHDESKTOP As Integer = &H100L
Public Declare Auto Function GetThreadDesktop Lib "user32.dll" (ByVal threadId As
Integer) As IntPtr
Public Declare Auto Function OpenInputDesktop Lib "user32.dll" (ByVal flags As
Integer, <MarshalAs(UnmanagedType.Bool)> ByVal inherit As Boolean, ByVal desiredAccess As
Integer) As IntPtr
Public Declare Auto Function CreateDesktop Lib "user32.dll" (ByVal desktop As String,
ByVal device As String, ByVal devmode As IntPtr, ByVal flags As Integer, ByVal
desiredAccess As Integer, ByVal lpsa As IntPtr) As IntPtr
Public Declare Auto Function SetThreadDesktop Lib "user32.dll" (ByVal desktop As
IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
Public Declare Auto Function SwitchDesktop Lib "user32.dll" (ByVal desktop As IntPtr)
As <MarshalAs(UnmanagedType.Bool)> Boolean
Public Declare Auto Function CloseDesktop Lib "user32.dll" (ByVal desktop As IntPtr)
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Module

Você também pode gostar