Lock workstation
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyCode_put_in_This_module.Initialze()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MyCode_put_in_This_module.CloseModule()
End Sub
End Class
Module MyCode_put_in_This_module
Dim Soundplayer As New System.Media.SoundPlayer
Dim Button1 As NativeWindow_Button
Public Const edge As Int32 = 5
Public Class NativeWindow_Button
Inherits NativeWindow
Implements IDisposable
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_WTSSESSION_CHANGE
If m.WParam.ToInt32 = WTS_SESSION_UNLOCK Then
Soundplayer.Play()
ElseIf m.WParam.ToInt32 = WTS_SESSION_LOCK Then
System.Media.SystemSounds.Asterisk.Play()
End If
Case WM_LBUTTONDOWN
Process.Start("RunDLL32.exe", "User32.dll,LockWorkStation")
End Select
MyBase.WndProc(m)
End Sub
Public Sub New(Parent_hWnd As IntPtr)
Dim cp As New CreateParams With {
.Parent = Parent_hWnd,
.Caption = "Click here to lock PC",
.ClassName = "BUTTON",
.Style = WS_CHILD Or WS_VISIBLE,
.X = edge, .Y = edge, .Width = 180, .Height = 30
}
CreateHandle(cp)
WTSRegisterSessionNotification(Me.Handle, NOTIFY_FOR_THIS_SESSION)
End Sub
Private disposedValue As Boolean
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
End If
WTSUnRegisterSessionNotification(Me.Handle)
ReleaseHandle()
disposedValue = True
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(disposing:=True)
GC.SuppressFinalize(Me)
End Sub
End Class
Sub Initialze()
Soundplayer = New System.Media.SoundPlayer(
$"{Environment.GetEnvironmentVariable("windir")}\Media\Windows Unlock.wav")
Button1 = New NativeWindow_Button(Form1.Handle)
Call Initialize2()
End Sub
Partial Private Sub Initialize2()
End Sub
Sub CloseModule()
Soundplayer.Dispose()
Button1.Dispose()
Call CloseModule2()
End Sub
Partial Private Sub CloseModule2()
End Sub
Public Const WS_CHILD As Int32 = &H40000000
Public Const WS_VISIBLE As Int32 = &H10000000
Public Const WTS_SESSION_LOCK As UInt32 = &H7
Public Const WTS_SESSION_UNLOCK As UInt32 = &H8
Public Const WM_LBUTTONDOWN As UInt32 = &H201
Public Const WM_WTSSESSION_CHANGE As UInt32 = &H2B1
Public Const NOTIFY_FOR_THIS_SESSION As UInt32 = 0
Declare Unicode Function WTSRegisterSessionNotification Lib "WTSApi32.dll" (
hWnd As IntPtr, dwFlags As UInt32) As Boolean
Declare Unicode Function WTSUnRegisterSessionNotification Lib "WTSApi32.dll" (
hWnd As IntPtr) As Boolean
End Module














留言
張貼留言