How to Make Screen Lock in VB.NET


The project today is Screen lock, Screen lock is used to lock the screen of your computer. and not just to access or tamper with your work. yet you will be able to access that requiring the user to perform a certain action in order to receive access, like log-in or click the button. here are one example of the lock screen.

First open Microsoft Visual Studio 2008/2010 and Create New Project and name it and then add three Form, first form the main form, second the background or the block screen and third form the close or unblock form. Then add  three Module. first Lock Screen Module, second Lock System Parameters and Native module.

Lock Screen Module
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 scr As Screen

        scr = Screen.PrimaryScreen

        Dim background As Bitmap = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
        Using g As Graphics = Graphics.FromImage(background)

            g.CopyFromScreen(0, 0, 0, 0, scr.Bounds.Size)
            Using br As Brush = New SolidBrush(Color.FromArgb(192, Color.Black))
                g.FillRectangle(br, scr.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 BlockScreen(v.Background)
            f.Show()
            myForm.ShowDialog()
            f.BackgroundImage = Nothing
            Application.DoEvents()
            Thread.Sleep(250)
        End Using
    End Sub

#End Region
End Class

Lock System Parameters
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 module
Imports System.Runtime.InteropServices

 _
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,  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  Boolean
    Public Declare Auto Function SwitchDesktop Lib "user32.dll" (ByVal desktop As IntPtr) As  Boolean
    Public Declare Auto Function CloseDesktop Lib "user32.dll" (ByVal desktop As IntPtr) As  Boolean

End Module

In main form there have a 2 button The LOCK and EXIT.
Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New Point(10, 10)
    End Sub

    Private Sub btnlock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlock.Click
        Dim myObj As LockScreen = New LockScreen
        myObj.LockSystemAndShow(CloseBotton)
    End Sub

    Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
        End
    End Sub

End Class

The background or the block screen.
Public Class BlockScreen
    Inherits Form

    Private background As Bitmap

    Public Sub New(ByVal background As Bitmap)
        Me.BackColor = Color.Black
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Location = Point.Empty
        Me.Size = Screen.PrimaryScreen.Bounds.Size
        Me.StartPosition = FormStartPosition.Manual
        Me.Visible = True
        Me.background = background
    End Sub

    Protected Overrides Sub OnShown(ByVal e As System.EventArgs)
        Me.BackgroundImage = background
        Me.DoubleBuffered = True
        MyBase.OnShown(e)
    End Sub
End Class


The Close or Unblock form
Public Class CloseBotton

    Private Sub btnunlock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnunlock.Click
        Me.Close()
    End Sub

    Private Sub CloseBotton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New Point(10, 10)
    End Sub
End Class

Follow the code and you... create your own lock screen with login.
If you want the copy of this project. Here's the link.  
http://j.gs/6IHD

This Project Design and Program By: lyo

0 comments:

Post a Comment