Domanda

DUPLICATO: Come posso determinare a livello di programmazione se la mia workstation è bloccata?

Come posso rilevare (durante il runtime) quando un utente Windows ha bloccato il suo schermo (Windows + L) e lo ha sbloccato di nuovo. So che potrei monitorare globalmente l'input da tastiera, ma è possibile verificare tale cosa con le variabili di ambiente?

È stato utile?

Soluzione

Puoi ricevere questa notifica tramite un messaggio WM_WTSSESSION_CHANGE. È necessario informare Windows che si desidera ricevere questi messaggi tramite WTSRegisterSessionNotification e annullare la registrazione con WTSUnRegisterSessionNotification.

Questi post dovrebbero essere utili per un'implementazione in 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 / 276.963-trapping-quando-workstation-locked

Altri suggerimenti

Un evento SessionSwitch potrebbe essere la soluzione migliore per questo. Controlla SessionSwitchReason passato attraverso SessionSwitchEventArgs per scoprire che tipo di interruttore è e reagire in modo appropriato.

Puoi usare ComponentDispatcher come un modo alternativo per ottenere quegli eventi.

Ecco una classe di esempio per concludere.

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

Non hai assolutamente bisogno di WM_WTSSESSION_CHANGE Usa le API WTTS interne.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top