Escolar Documentos
Profissional Documentos
Cultura Documentos
Form 2
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
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