gpt4 book ai didi

c# - 如何禁用应用程序的 alt+F4?

转载 作者:可可西里 更新时间:2023-11-01 07:58:38 27 4
gpt4 key购买 nike

如何在 C# 应用程序中禁用 ALT+F4 应用程序范围?

在我的应用程序中,我有很多 WinForms,我想禁用使用 ALT+F4 关闭窗体的功能。不过,用户应该能够使用表单的“X”关闭表单。

同样,这不仅仅是一种形式。我正在寻找一种方法,以便 ALT+F4 对整个应用程序禁用,并且不适用于任何表单。可能吗?

最佳答案

你可以在主启动方法中加入这样的东西:

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.AddMessageFilter(new AltF4Filter()); // Add a message filter
Application.Run(new Form1());
}
}

public class AltF4Filter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
const int WM_SYSKEYDOWN = 0x0104;
if (m.Msg == WM_SYSKEYDOWN)
{
bool alt = ((int)m.LParam & 0x20000000) != 0;
if (alt && (m.WParam == new IntPtr((int)Keys.F4)))
return true; // eat it!
}
return false;
}
}
}

关于c# - 如何禁用应用程序的 alt+F4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12898680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com