简体   繁体   English

如何使用C#锁定屏幕?

[英]How can I lock the screen using C#?

I just write my first C# application, which is a scheduler. 我只写了我的第一个C#应用程序,它是一个调度程序。 Once an hour I want to pop-up a dialog and lock the screen for two minutes in order to take a break. 每小时一次,我想弹出一个对话框并锁定屏幕两分钟,以便休息一下。

As of now, my application just shows a form on "TopMost" when its time to break and hides it two minutes later. 到目前为止,我的应用程序只是在“TopMost”上显示一个表单,它的时间是两分钟后打破并隐藏它。

How can I lock the screen as well? 我怎样才能锁定屏幕? Something similar to the UAC style in Vista. 类似于Vista中的UAC风格。

Eep, misread your question. Eep,误读了你的问题。 The UAC dialog of Vista creates a new desktop and shows itself on that. Vista的UAC对话框创建了一个新桌面并在其上显示。 To Create a desktop you can use the CreateDesktop function. 要创建桌面,您可以使用CreateDesktop功能。 You can then use SwitchDesktop to switch to it. 然后,您可以使用SwitchDesktop切换到它。

If you truly want to re-create the secure desktop's looks, then you need to take a screenshot first and display that on your form, darkened a little. 如果您真的想要重新创建安全桌面的外观,那么您需要先截取屏幕截图并在表单上显示,稍微变暗。


Original answer below, which might be of interest too: 下面的原始答案,也可能是有趣的:

Take a look at the LockWorkStation function. 看看LockWorkStation功能。

However, you can't programmatically unlock the work station again. 但是,您无法以编程方式再次解锁工作站。 That's something the user has to do himself. 这是用户必须自己做的事情。 So you can't easily enfore a break at least two minutes long unless you still resort to your top-level window technique to deny the user his workspace as long as the two minutes are not yet over. 因此,除非你仍然使用顶级窗口技术拒绝用户的工作空间,只要两分钟还没有结束,你就不能轻易地进行至少两分钟的休息。

As an alternative to taking a screen grab (which I've also used as an approach in the past under slightly different circumstances) you can also do this by creating a new window (WinForm) that is full screen and on top of all other windows. 作为屏幕抓取的替代方法(我在过去稍微不同的情况下也将其用作方法),您也可以通过创建一个全屏并在所有其他窗口之上的新窗口(WinForm)来实现此目的。 。

If you set the window background colour to solid black then set the opacity to 70-80%, you'll get something that looks like the UAC prompt. 如果您将窗口背景颜色设置为纯黑色,然后将不透明度设置为70-80%,您将获得类似于UAC提示的内容。

eg 例如

formName.WindowState = FormWindowState.Maximized; 
formName.FormBorderStyle = FormBorderStyle.None; 
formName.Opacity = 0.70; 
formName.TopMost = true;

Of course it would be sensible to draw a window on top of this informing the user why the screen has been dimmed, with a counter. 当然,在此基础上绘制一个窗口是明智的,通过计数器告知用户屏幕为何变暗。

Using something that looks enough like a UAC prompt (which people are accustomed to seeing) to not have people even think twice about it might be enough depending on the use case. 使用看起来像UAC提示(人们习惯于看到)的东西,根本没有让人们三思而后行,这取决于用例。

As with the screen grab approach, this on it's own does not prevent a user from bypassing it by using the Windows key, Alt - Tab or Ctrl - Esc to bring up the Start menu, or to switch to other tasks. 与屏幕抓取方法一样,它本身并不会阻止用户通过使用Windows键, Alt - TabCtrl - Esc调出“开始”菜单或切换到其他任务来绕过它。

Disabling built in key commands requires setting Registry Keys relating to Windows Hotkeys and is a bit more tricky (I think it requires a reboot and can't be toggled on the fly). 禁用内置键命令需要设置与Windows热键相关的注册表项,并且有点棘手(我认为它需要重新启动,无法动态切换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM