Question

DUPLICATE: Comment 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 l'a déverrouillé à nouveau. Je sais que je peux suivre globalement la saisie au clavier, mais est-il possible de vérifier cela avec des variables d’environnement?

Était-ce utile?

La solution

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'enregistrement 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-piégeage-lorsque-verrouillé-station

Autres conseils

Un événement SessionSwitch peut être votre meilleur pari pour ça. Consultez le SessionSwitchReason transmis via le SessionSwitchEventArgs pour savoir quel type de commutateur il s'agit et réagir de manière appropriée.

Vous pouvez utiliser ComponentDispatcher comme autre moyen d'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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top