C#、WinForms:什么会阻止 KeyDown 事件从焦点控件链接到主窗体?只有叶子控件 KeyDown 对我有用

发布于 2024-09-01 11:07:23 字数 364 浏览 6 评论 0原文

据我了解,当按下键盘按钮时,它应该为具有焦点的控件调用 KeyDown 事件。然后,父控件的 KeyDown,依此类推,直到到达主窗体。除非 - 沿着链中的一个事件处理程序做了:

e.SuppressKeyPress = true;
e.Handled = true;

在我的例子中,KeyDown 事件永远不会到达主窗体。 我有表格 ->面板->例如按钮。

Panel 不提供 KeyDown 事件,但它不应该阻止它到达主窗体,对吗?

现在,作为解决方案,我将每个控件设置为调用我编写的事件处理程序。我基本上是试图阻止 Alt-F4 关闭应用程序,而是将其最小化。

As i understand it, when a keyboard button is pressed it should invoke the KeyDown event for the control which has focus. Then, the KeyDown for the parent control, so on and so forth until it reaches main form. UNLESS - along the chain one of the EventHandlers did:

e.SuppressKeyPress = true;
e.Handled = true;

In my case, KeyDown events never get to the main form.
I have Form -> Panel -> button for example.

Panel doesn't offer a KeyDown Event, but it shouldn't stop it from reaching the main form right?

Right now as a work around I set every single control to call an event handler I wrote. I'm basically trying to prevent Alt-F4 from closing the application and instead minimize it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2024-09-08 11:07:23

[编辑]

如果您想捕获 Alt-F4,那么在控制级别尝试是没有意义的,因为击键是由应用程序处理的 - 请参阅 如何禁用 Alt + F4 关闭表单?

[Edit]

If you want to trap Alt-F4 then there's no point trying at the control level as that keystroke is handled by the application - see How to Disable Alt + F4 closing form?

白芷 2024-09-08 11:07:23

您可以使用应用程序消息过滤器:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.AddMessageFilter(new TestMessageFilter());
            Application.Run(new Form1());
        }
    }

    public class TestMessageFilter : IMessageFilter
    {
        private int WM_SYSKEYDOWN = 0x0104;
        private int F4 = 0x73;

        public bool PreFilterMessage(ref Message i_Message)
        {
            Console.WriteLine("Msg: {0} LParam: {1} WParam: {2}", i_Message.Msg, i_Message.LParam, i_Message.WParam);
            if (i_Message.Msg == WM_SYSKEYDOWN && i_Message.WParam == (IntPtr)F4)
                return (true); // Filter the message
            return (false);
        } // PreFilterMessage()

    } // class TestMessageFilter
}

You can use an application message filter:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.AddMessageFilter(new TestMessageFilter());
            Application.Run(new Form1());
        }
    }

    public class TestMessageFilter : IMessageFilter
    {
        private int WM_SYSKEYDOWN = 0x0104;
        private int F4 = 0x73;

        public bool PreFilterMessage(ref Message i_Message)
        {
            Console.WriteLine("Msg: {0} LParam: {1} WParam: {2}", i_Message.Msg, i_Message.LParam, i_Message.WParam);
            if (i_Message.Msg == WM_SYSKEYDOWN && i_Message.WParam == (IntPtr)F4)
                return (true); // Filter the message
            return (false);
        } // PreFilterMessage()

    } // class TestMessageFilter
}
玉环 2024-09-08 11:07:23

尝试创建一个观察者来捕获您的事件:

http://ondotnet .com/pub/a/dotnet/2002/04/15/events.html

Try creating an observer to capture your events:

http://ondotnet.com/pub/a/dotnet/2002/04/15/events.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文