c# - wpf 使用 C# 停止 ctrl+Alt+del 和 windows 按钮

标签 c# wpf

如何使用 C# 和 wpf 禁用 ctrl+Alt+del 和 Windows 按钮??

我尝试使用一些事件形式的事件,例如按键但失败。

最佳答案

无法专门禁用 Ctrl-Alt-Del 的快捷方式, 这是因为 Ctrl-Alt-Del 组合是一个深度烘焙的系统调用。

但是可以单独过滤它们,因此您可以使用这些键阻止其他快捷键。 为此,您需要 Hook 操作系统事件:


这与系统事件 Hook 。

private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);

如果将 id 设置为 13,它将连接到键盘输入。


在回调中你需要几件事:

[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
    public readonly Keys key;
    private readonly int scanCode;
    private readonly int flags;
    private readonly int time;
    private readonly IntPtr extra;
}

需要此结构来读取 C# 中的实际键。

可以通过为委托(delegate)提供如下函数来使用它:

private static IntPtr CaptureKey(int nCode, IntPtr wp, IntPtr lp)
{
    if (nCode < 0) return (IntPtr) 1; //CallNextHookEx(_ptrHook, nCode, wp, lp);
    KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
    if(objKeyInfo.key == /*some key*/){
        // do something
    }
}

使用此功能时,您可以从objKeyInfo.key获取 key

有关 Ctrl-Alt-Del 组合的更多背景信息: Is there any method to disable logoff,lock and taskmanager in ctrl+alt+del in C#

关于c# - wpf 使用 C# 停止 ctrl+Alt+del 和 windows 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351697/

相关文章:

c# - 在同一个项目中同时使用 AddDbContextFactory() 和 AddDbContext() 扩展方法

wpf DataGrid 更改单元格换行

WPF 触发器未按预期工作

wpf - 基本viewmodel不包含属性?在Wpf MVVM中

WPF - 如何让某些 ListView 项目跨越列?

wpf - 如何确定单击了哪个节点。 Silverlight TreeView

c# - 在 Crystal 报表中以大写形式显示字段值

c# - C# 中的默认类可访问性

c# - 是否可以在 iOS 应用程序的导航 Controller 中实现 Split View Controller ?

c# - 如何停止闪烁 C# winforms