在的WinForms窗口的检测X按钮点击 [英] Detecting X button click of a window in winforms

查看:153
本文介绍了在的WinForms窗口的检测X按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检测关闭(X)按钮的Click事件在窗体/窗口的控制盒的右上角?请注意,我也不想知道CloseReason,的FormClosing,FormClosed或东西像这样的,除非它们是不可避免的。我究竟
要检测如果用户点击窗体的X按钮。谢谢你。

How can I detect the click event of the close (X) button at the top right corner of the control box of a form/window? Please note, I don't want to know about CloseReason, FormClosing, FormClosed or stuffs like these, unless they are inevitable. I exactly want to detect if the user clicked the X button of the form. Thanks.

推荐答案

如果您真的有一个很好的理由不使用的FormClosing,CloseReason,... ,你可以重写窗口过程,写是这样的:

If you really have a good reason not to use FormClosing, CloseReason, ..., you can override the window procedure and write something like this:

    protected override void WndProc(ref Message m)
    {
        const int WM_NCLBUTTONDOWN = 0x00A1;
        const int HTCLOSE = 20;

        if (m.Msg == WM_NCLBUTTONDOWN)
        {
            switch ((int)m.WParam)
            {
                case HTCLOSE:
                    Trace.WriteLine("Close Button clicked");
                    break;
            }
        }

        base.WndProc(ref m);
    }



的细节可以发现的here 并的这里

这篇关于在的WinForms窗口的检测X按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆