Vérification du changement de locking / délocking du poste de travail avec c #

DUPLICATE: Comment puis-je déterminer par programme si mon poste de travail est verrouillé?

Comment puis-je détecter (pendant l’exécution) lorsqu’un utilisateur Windows a verrouillé son écran (Windows + L) et le déverrouiller à nouveau? Je sais que je pourrais suivre globalement la saisie au clavier, mais est-il possible de vérifier une telle chose avec des variables d’environnement?

Vous pouvez obtenir cette notification via un message WM_WTSSESSION_CHANGE. Vous devez notifier Windows que vous souhaitez recevoir ces messages via WTSRegisterSessionNotification et annuler l’inscription avec WTSUnRegisterSessionNotification.

Ces publications devraient être utiles pour une implémentation 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

Un événement SessionSwitch peut être votre meilleur choix pour cela. Vérifiez que SessionSwitchReason est passé par SessionSwitchEventArgs pour savoir quel type de commutateur il est et réagir de manière appropriée.

Vous pouvez utiliser ComponentDispatcher comme un moyen alternatif pour obtenir ces événements.

Voici un exemple de classe pour envelopper cela.

 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); } } } 

Vous n’avez absolument pas besoin de WM_WTSSESSION_CHANGE Utilisez simplement les apis WTTS internes.