如何在计算机锁定时关闭C#应用程序(窗口L) [英] How to close the C# application when the computer is locked (window L)

查看:131
本文介绍了如何在计算机锁定时关闭C#应用程序(窗口L)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下窗口+ L键时,C#应用程序将自动关闭



我尝试过:



我尝试使用Lockworkstation方法,但我无法做到这一点

when the user press window+L key, C# application will close automatically

What I have tried:

I have try to use Lockworkstation method but I am unable to do that

推荐答案

看看WTSRegisterSessionNotification function(Windows) [ ^ ]。


// check below code it will automatically close the application whenever you locks the workstation by windows+L key, hope it solves your problem

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        private static SessionSwitchEventHandler eHandler;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            eHandler = new SessionSwitchEventHandler(EventCapture);
            SystemEvents.SessionSwitch += eHandler;
        }
        void EventCapture(object sender, SessionSwitchEventArgs e)
        {

            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLock: MessageBox.Show("Lock Encountered"); this.Close(); break;
                case SessionSwitchReason.SessionUnlock: MessageBox.Show("UnLock Encountered"); break;
            }
        }
    }
}


这篇关于如何在计算机锁定时关闭C#应用程序(窗口L)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆