C# 在启动时锁定 Windows 计算机
我正在尝试为我的弟弟编写一个简单的程序。他很多时候都在电脑后面,但他应该在学校学习简单的算术:D
我想编写以下程序:
- 他启动他的电脑
- 他需要做一些简单的练习 并完成
- 如果他正确完成了 x 项,他就可以继续使用他的计算机。
有没有一种简单的(不需要非常干净的xD)方法来锁定他的计算机,直到我的程序说它可以这样做。
PS(我不是指Windows中标准机制中的锁定)
PPS 它不需要超高科技,只是普通计算机用户无法通过关闭软件来绕过它:P
I'm trying to write a simple program for my little brother. He's behind his computer a lot of times but he should be learning simple arithmetics for school :D
I want to make the following program:
- He starts up his computer
- He needs to do a few simple exercises
and complete - If he did an x amount correct, he can continue to use his computer.
Is there a simple (it doesn't need to be very clean xD) way of locking his computer untill my program says it can do so.
P.S. (I don't mean locking as in the standard mechanism in windows)
P.P.S. It does't need to be super high tech, just that an average computer user cannot bypass it by closing the software :P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要某种系统模式对话框(自 win nt 4 以来并未正式支持(出于某些充分的原因)。但是,有 此博客向您展示如何仍然可以完成同样的事情。
You need some kind of system modal dialog (which isn't formally supported since win nt 4 (for some good reasons). However there is this blog that shows you how you can still accomplish the same thing.
如果这仍然相关,那么在不使用任何挂钩的情况下永久锁定计算机的简单方法是执行以下操作:
首先,创建 Form_LostFocus 事件处理程序。这不在“属性”->“事件”窗口中,因此您必须以编程方式添加它:
当表单失去焦点时调用此方法,因此在这里我们需要将焦点返回给表单:
此方法在以下情况下效果最佳:然后将焦点赋予窗体上的控件:
接下来,添加一个计时器,间隔为 100 毫秒,并启用它。该计时器将检查表单是否具有焦点,如果没有,则为其提供焦点:
这将有效地确保无论用户尝试做什么,程序总是会窃取焦点 - 即使他打开任务管理器。
然后让程序全屏显示:
将“TopMost”属性设置为 true。
将“窗口状态”设置为“最大化”。
将“FormBorderStyle”设置为“无”。
最后,处理 Form_Closing 事件处理程序。
请注意:一般来说,窃取焦点是非常糟糕的做法。
If this is stil relevant, then a simple way of locking down the computer for good without using any hooks is to do as follows:
First, create the Form_LostFocus Eventhandler. This isn't in the properties->events window, so you'll have to add it programmatically:
This is called when the from loses focus, so in here we need to give focus back to the form:
This method works best if focus is then given to a control on the form:
Next, add a timer, with an interval of 100ms, and enable it. This timer will check if the form has focus, and if not, will give it focus:
This will effectively ensure that no matter what your user attemps to do, the program will always steal focus - even if he opens the task manager.
Then just make the program fullscreen:
Set 'TopMost' property to true.
Set 'WindowState' to 'maximised'.
Set 'FormBorderStyle' to 'None'.
Finally, handle the Form_Closing Eventhandler.
Please note: In general, stealing focus is extremely bad practice.
在 babysmash 的代码中寻找灵感,特别是 此处 和 此处。这是一款由 Scott Hanselman 编写的针对婴儿/幼儿的应用程序,可以很好地锁定桌面。您也许可以重用其中的一些想法。
Look for inspiration at the code for babysmash, specifically here and here. It's an app for babies/toddlers written by Scott Hanselman that locks down the desktop reasonably well. You can probably reuse some of the ideas in there.
我为我儿子提供的解决方案是创建一个巨大的全屏 wpf 应用程序,删除所有工具栏等,并在启动时加载它。由于没有明显的方法可以关闭它,他不知道如何关闭它,所以只能回到床上。他最终学会了如何解决这个问题,但这对他有用了一段时间。
组合起来并不难,我可以实施更多策略来获取他的屏幕输入,并在他试图关闭它时阻止它,但当他想出如何解决它时,这已经不需要了。
如果您愿意,使用全屏 wpf 应用程序,您应该能够以有趣的格式输出数学问题。
My solution for my son was to create a giant fullscreen wpf app with all the toolbars removed etc and load it on startup. Since there was no visible way to close it he had no idea how to close it, so would just go back to bed. He eventually learned how to get around it but it worked for him for a fair while.
It wasn't very hard to put together, and I could have implemented some more tactics to grab his screen input and stop it if he tried to close it but that wasn't needed by the time he figured out how to get around it.
With a fullscreen wpf app you should be able to output your math questions in an interesting format if you wish.
如果你想要一些快速的东西(尽管我认为 Eddy 和 jeroenh 有高质量的答案),你可以在没有 WinAPI/互操作的情况下完成 - 只需使用窗口技巧。
首先在您的
Form
上创建一个属性HasCompletedTasks
,当他完成测验后将其设置为 true:现在阻止窗口关闭:
接下来在表单设计器中设置
FormBorderStyle
为None
,WindowState
为Maximized
和TopMost
为True,或者只是粘贴此代码位于
InitializeComponent();
下方:如果您想在他知道如何关闭它时收到警报 (CTRL+ALT+DEL),您可以在系统上留下您可以查找的文件(并相应地责骂他)。取消注释示例中的行以启用此功能。
If you want something quick (although I think Eddy and jeroenh have quality answers) you can do it with no WinAPI/interop - just use window tricks.
First create a property
HasCompletedTasks
on yourForm
, set it to true when he has done his quizzes:Now prevent the window from closing:
Next in the form designer set the
FormBorderStyle
toNone
, theWindowState
toMaximized
andTopMost
toTrue
, or just paste this code right underInitializeComponent();
:If you want to be alerted if he figures out how to close it (CTRL+ALT+DEL) you can leave files on the system that you can look for (and scold him accordingly). Uncomment the lines in samples to enable this.