How do I detect a Lock This Computer command from a WPF application?

Here is something from CodeProject.

http://www.codeproject.com/KB/vb/DetectWindowslockunlock.aspx


You need to P/Invoke WTSRegisterSessionNotification.


When you handle the Microsoft.Win32.SystemEvents.SessionSwitch event (which it sounds like you're already doing to detect logout), check to see if the Reason is SessionSwitchReason.SessionLock:

 using Microsoft.Win32;
 // ...
 // Somewhere in your startup, add your event handler:
    SystemEvents.SessionSwitch += 
       new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
 // ...

 void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
 {
     switch(e.Reason)
     {
         // ...
         case SessionSwitchReason.SessionLock:
            // Do whatever you need to do for a lock
            // ...
         break;
         case SessionSwitchReason.SessionUnlock:
            // Do whatever you need to do for an unlock
            // ...
         break;
         // ...
     }
 }