Comprobando el cambio de bloqueo / desbloqueo de la estación de trabajo con c #
-
03-07-2019 - |
Pregunta
DUPLICADO: ¿Cómo puedo determinar mediante programación si mi estación de trabajo está bloqueada?
¿Cómo puedo detectar (durante el tiempo de ejecución) cuando un usuario de Windows ha bloqueado su pantalla (Windows + L) y la ha desbloqueado de nuevo? Sé que podría realizar un seguimiento global de las entradas del teclado, pero ¿es posible verificar esto con las variables de entorno?
Solución
Puede recibir esta notificación a través de un mensaje WM_WTSSESSION_CHANGE. Debe notificar a Windows que desea recibir estos mensajes a través de WTSRegisterSessionNotification y anular el registro con WTSUnRegisterSessionNotification.
Estas publicaciones deberían ser útiles para una implementación de C #.
http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification
http://blogs.msdn.com/shawnfa /archive/2005/05/17/418891.aspx
http://bytes.com/groups/net -c / 276963-trapping-when-workstation-locked
Otros consejos
Puedes usar ComponentDispatcher
como una forma alternativa de obtener esos eventos.
Aquí hay una clase de ejemplo para envolver eso.
public class Win32Session
{
private const int NOTIFY_FOR_THIS_SESSION = 0;
private const int WM_WTSSESSION_CHANGE = 0x2b1;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8;
public event EventHandler MachineLocked;
public event EventHandler MachineUnlocked;
public Win32Session()
{
ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;
}
void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled)
{
if (msg.message == WM_WTSSESSION_CHANGE)
{
int value = msg.wParam.ToInt32();
if (value == WTS_SESSION_LOCK)
{
OnMachineLocked(EventArgs.Empty);
}
else if (value == WTS_SESSION_UNLOCK)
{
OnMachineUnlocked(EventArgs.Empty);
}
}
}
protected virtual void OnMachineLocked(EventArgs e)
{
EventHandler temp = MachineLocked;
if (temp != null)
{
temp(this, e);
}
}
protected virtual void OnMachineUnlocked(EventArgs e)
{
EventHandler temp = MachineUnlocked;
if (temp != null)
{
temp(this, e);
}
}
}
Absolutamente no necesitas WM_WTSSESSION_CHANGE Solo use las apis internas de WTTS.