Jump to content

Fading Computer Locker


FifthLobe
 Share

Recommended Posts

This uses windows' built-in transparency to transition your screen to black.

A giant window is used because I know no other way of doing fullscreen.

It'll fade slowly if your video card has to deal with a huge wallpaper or if your video card is weak in general.

The exe is meant to be named AutoFade.exe, which is necessary for the purpose of the KillClones() function.

Enjoy

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:       Fifthlobe <fifthlobeATspambobDOTnet>
;
; Script Function:
;   'Locks' your computer so that a lay person cannot take over your session.
;   It is still possible to logout or shutdown if necessary.
;   --------------------------------------------------------------------------
;   Default password is: password
;  
; Hotkeys:
;   Ctrl+Shift+~ ...... Lock/unlock 
;   Ctrl+Shift+P ...... Toggle preferences window
;
; ----------------------------------------------------------------------------

; Script Start

;options
Opt("RunErrorsFatal", 0); don't close on @error.
Opt("TrayIconHide", 1); hide system tray icon.


;includes
#include <guiconstants.au3>
#include <date.au3>


;hotkeys
;~ HotKeySet('{ESC}', 'Quit')
HotKeySet('^~', 'FadeIn')
HotKeySet('^P', 'Preferences')


;some declarations
Global $fade = 'in'; tells whether the program should fade into the screen or out, used in Fade()
Global $running = False; set to True when program is running to prevent interruption, used in Fade()
Global $locked = False; triggers whether to 'lock' the computer (kill task manager, etc)
Global $tries = 0; counts the number of attempts to enter correct password (when unlocking)
Global $lockdown = 0; stores the time to reactivate password dialog when too many attempts are made, equals 0 when not needed)
Global $pword = 'password'; default password before it's set by the user
Global $maxtries = 3; default maximum # of tries before 'lockdown'
Global $lockdelay = 5; default # of seconds a 'lockdown' is instigated
Global $autopower = 1; default to automatically power down the monitor (=1 when enabled)


;gui setup
$gui = GUICreate('Fullscreen', @DesktopWidth*1.2, @DesktopHeight*1.2, -1, -1, -1, $WS_EX_TOOLWINDOW+$WS_EX_TOPMOST); main fullscreen GUI
GUISetState(@SW_HIDE, $gui)
;
$dialog = GUICreate('Password', 250, 20, 200, 120, $WS_CHILD, -1, $gui); password dialog
$passbox = GUICtrlCreateInput('', 0, 0, 200, 20, $ES_PASSWORD)
$passbtn = GUICtrlCreateButton('Unlock', 200, 0, 50, 20, $BS_CENTER+$BS_VCENTER)
GUISetState(@SW_HIDE, $dialog)
;
$prefs = GUICreate('Preferences', 260, 200, -1, -1, $WS_EX_TOPMOST); preferences window
GUICtrlCreateLabel('Password: ', 5, 12, 50, 20, $SS_RIGHT)
$prefs_nowpass = GUICtrlCreateInput('', 60, 10, 180, 20, $ES_PASSWORD); ask for current password, used to prevent unauthorized changes in preferences
GUICtrlCreateLabel('New: ', 5, 37, 50, 20, $SS_RIGHT)
$prefs_pword = GUICtrlCreateInput('', 60, 35, 180, 20, $ES_PASSWORD)
GUICtrlCreateLabel('Verify:', 5, 62, 50, 20, $SS_RIGHT)
$prefs_pword_verify = GUICtrlCreateInput('', 60, 60, 180, 20, $ES_PASSWORD)
$prefs_maxtries = GUICtrlCreateInput($maxtries, 60, 85, 25, 20)
$prefs_lockdelay = GUICtrlCreateInput($lockdelay, 200, 85, 25, 20)
$prefs_autopower = GUICtrlCreateCheckbox('Auto power down monitor', 60, 110)
GUICtrlSetState($prefs_autopower, $GUI_CHECKED)
$resetprefs = GUICtrlCreateButton(' Reset ', 5, 145)
$cancelprefs = GUICtrlCreateButton(' Cancel ', 55, 145)
$quitbtn = GUICtrlCreateButton(' Quit ', 110, 145)
$saveprefs = GUICtrlCreateButton(' Save ', 205, 145)
GUISetState(@SW_HIDE, $prefs)


;main loop
While 1
    
;~  Sleep(100)
    
    If $fade = 'out' And $running = False And $locked = True Then
        WinKill('Windows'); kill Windows Task Manager and the like
    EndIf
    
    KillClones(); Kill duplicate processes
    
    $guimsg = GUIGetMsg(1)
    
    If $guimsg[0] <> 0 Then
        $guiwinhandle = $guimsg[1]
        $guictrlhandle = $guimsg[2]
        $guimsg = $guimsg[0] 
        
        Select
            Case $guimsg = $passbtn
                
                $pbox = GUICtrlRead($passbox); temporarily stores value entered into $passbox
                GUICtrlSetData($passbox, '')
                
                If $pbox = $pword Then
                    $tries = 0
                    GUISetState(@SW_HIDE, $dialog)
                    FadeOut(); fade out on correct pass
                    
                ElseIf $pbox <> '' Then
                    $tries += 1
                    Sleep(200)
                    GUISetBkColor(0xFF0000, $gui)
                    Sleep(300)
                    GUISetBkColor(0x000000, $gui)
                    
                    If $tries >= $maxtries Then 
                        GUISetState(@SW_DISABLE, $dialog)
                        GUICtrlSetState($passbox, $GUI_DISABLE)
                        GUICtrlSetState($passbtn, $GUI_DISABLE)             
                        $lockdown = _TimeToTicks(@HOUR, @MIN, @SEC+$lockdelay); the time to exit lockdown ($lockdelay secs)
                    EndIf
                    
                    
                EndIf
                
            Case $guimsg = $saveprefs
                
                If GUICtrlRead($prefs_nowpass) = $pword Then
                    GUISetState(@SW_HIDE, $prefs)
                    If IsInt(GUICtrlRead($prefs_lockdelay)) Then $lockdelay = GUICtrlRead($prefs_lockdelay)
                    If IsInt(GUICtrlRead($prefs_maxtries)) Then $maxtries = GUICtrlRead($prefs_maxtries)
                    If StringLen(GUICtrlRead($prefs_pword)) > 0 And GUICtrlRead($prefs_pword) = GUICtrlRead($prefs_pword_verify) Then $pword = GUICtrlRead($prefs_pword)
                    If GUICtrlRead($prefs_autopower) = $GUI_CHECKED Then 
                        $autopower = 1 
                    Else 
                        $autopower = 0
                    EndIf
                ElseIf GUICtrlRead($prefs_nowpass) = '' Then
                    MsgBox(64, 'Error', 'You did not enter your password.')
                Else
                    MsgBox(16, 'Error', 'Incorrect Password,')
                EndIf
                
            Case $guimsg = $resetprefs
                GUICtrlSetData($prefs_pword, '')
                GUICtrlSetData($prefs_pword_verify, '')
                GUICtrlSetData($prefs_maxtries, $maxtries)
                GUICtrlSetData($prefs_lockdelay, $lockdelay)
                If $autopower = 1 Then 
                    GUICtrlSetState($prefs_autopower, $GUI_CHECKED) 
                Else
                    GUICtrlSetState($prefs_autopower, $GUI_UNCHECKED)
                EndIf
                
            Case $guimsg = $cancelprefs
                GUISetState(@SW_HIDE, $prefs)
                GUICtrlSetData($prefs_pword, '')
                GUICtrlSetData($prefs_pword_verify, '')
                GUICtrlSetData($prefs_maxtries, $maxtries)
                GUICtrlSetData($prefs_lockdelay, $lockdelay)
                If $autopower = 1 Then 
                    GUICtrlSetState($prefs_autopower, $GUI_CHECKED) 
                Else 
                    GUICtrlSetState($prefs_autopower, $GUI_UNCHECKED)
                EndIf
                
            Case $guimsg = $quitbtn
                Quit()
                
                
        EndSelect
        
    EndIf
    
    If $lockdown <> 0 And _TimeToTicks(@HOUR,@MIN,@SEC) >= $lockdown Then
        GUICtrlSetState($passbox, $GUI_ENABLE)
        GUICtrlSetState($passbtn, $GUI_ENABLE)  
        GUISetState(@SW_ENABLE, $dialog)
        $lockdown = 0; reset the time to exit lockdown
    EndIf
    
    
    
WEnd


;---------------
;functions below
;---------------

Func FadeIn()

    If $running = False Then; $running prevents interruption from hotkey
        
        If $fade = 'in' Then
            
            $running = True
            GUISetBkColor(0xFFFFFF, $gui)
            WinSetTrans('Fullscreen', '', 0)
            GUISetState(@SW_SHOW, $gui) 
            For $i=25 To 100 Step 25; Fade to 100% opacity
                WinSetTrans('Fullscreen', '', ($i/100)*255)
                If @error = 1 Then ExitLoop; If OS doesn't support transparency
;~              Sleep(1)
            Next
            For $i=0 To 255 Step 5; Fade to black
                $j = Hex(255-$i,2)
                GUISetBkColor('0x'&$j&$j&$j, $gui)
                Sleep(10)
            Next
            _CurVisInv(0); Cursor invisible
            If $autopower = 1 Then MonitorPower(0); Monitor power off
            $locked = True
            $fade = 'out'
            $running = False
            
        ElseIf $fade = 'out' Then
            
            $running = True
            If $autopower = 1 Then MonitorPower(1); Monitor power on
            _CurVisInv(1); Cursor Visible
            GUISetState(@SW_SHOW, $dialog); show password box
            $running = False
            
        EndIf
        
    EndIf
    
    Return 1
EndFunc;==> Fade


Func FadeOut()
    If $fade = 'out' Then
        
        $running = True
        $locked = False
        _CurVisInv(1); Cursor visible
        MonitorPower(1); Monitor power on
;~      Sleep(2500)
        For $i=0 To 255 Step 5; Fade to white
            $j = Hex($i,2)
            GUISetBkColor('0x'&$j&$j&$j, $gui)
            Sleep(10)
        Next
        GUISetState(@SW_HIDE, $gui)
        $fade = 'in'
        $running = False
        
    EndIf
    
    Return 1
EndFunc;==> FadeOut



Func Quit()
    Exit
EndFunc;==> Quit



Func MonitorPower($toggle)
    
    Opt("WinTitleMatchMode", 4)
    
    $WM_SYSCommand = 274
    $SC_MonitorPower = 61808
    $Power_Off = 2
    $Power_On = -1
    
    $HWND = WinGetHandle("classname=Progman")

    If $toggle = 0 Then
        
        DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand, "int", _
        $SC_MonitorPower, "int", $Power_Off)
        
    ElseIf $toggle = 1 Then
        
        DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand, "int", _
        $SC_MonitorPower, "int", $Power_On)
        
    EndIf
    
    Return 1
    
EndFunc;==> MonitorPower



Func _CurVisInv($bV)
  Local $cursorinternalc
 
  Select
     Case $bV = 1
        Do
           $cursorinternalc = DllCall("User32.dll", "long", "ShowCursor", "long", $bV)
           Sleep(1)
        Until $cursorinternalc[0] > 0
     Case $bV = 0
        Do
           $cursorinternalc = DllCall("User32.dll", "long", "ShowCursor", "long", $bV)
           Sleep(1)
        Until $cursorinternalc[0] < 0
     Case Else
        Return 0
  EndSelect

  Return 1
EndFunc;==>_CurVisInv1



Func KillClones()
    $processes = ProcessList('AutoFade.exe')
    
    If $processes[0][0] > 1 Then
        For $i=2 To $processes[0][0]
            ProcessClose($processes[$i][1])
        Next
    EndIf
    
    Return 1
EndFunc


Func Preferences()
    
    If $fade = 'in' And $running = False Then
        GUISetState(@SW_SHOW, $prefs); show preferences window
    EndIf
    
    Return 1
EndFunc
Edited by FifthLobe
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...