检测鼠标按钮&表单系统菜单上的事件。 [英] Detecting mouse button down & up events on the system menu of form.

查看:68
本文介绍了检测鼠标按钮&表单系统菜单上的事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在窗体左上角单击鼠标时出现的系统菜单的关闭选项中检测鼠标左键和向上事件?

How do I detect left mouse button down and up events on the 'Close' option of the system menu that appears when the mouse is clicked on the top left corner of a form?

推荐答案

您不能直接从Win Form的非客户区域获取事件;您可以使用Win API调用获取这些事件。



但是,如果在表单的关闭按钮上执行鼠标操作,并且在单击的情况下在TitleBar中的最大化和最小化按钮上,您可以通过为表单'关闭,'关闭和'调整事件大小来定义EventHandlers来获取这些事件。



如果你定义此表单结束EventHandler:
You cannot directly get Events from the non-Client area of a Win Form; you can get those Events using Win API calls.

However, in the case of a mouse-action on the Close Button of a Form, and in case of the clicks on Maximize, and Minimize Buttons in the TitleBar, you can get those Events by defining EventHandlers for the Form 'Closing, 'Close, and 'Resize Events.

If you define this Form Closing EventHandler:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (e.CloseReason == CloseReason.UserClosing)
     {
         // do stuff
         if(SomeConditionYouDefine) e.Cancel = true;
     }
 }

这将在表单关闭之前触发,您可以通过测试e.CloseReason来识别是否通过单击ControlBox来关闭表单。 />


如果你在Closing EventHandler中将e.Cancel设置为'true,那么表格将不会关闭。



对于最小化和最大化按钮单击,您可以为表单'调整大小事件定义一个EventHandler,并在每次调整大小后根据当前WindowState执行操作:

This will be triggered before the Form is closed, and you can recognize if the Form was closed by a click on the ControlBox by testing e.CloseReason.

If you set e.Cancel to 'true in the Closing EventHandler, then the Form will not close.

For Minimize and Maximize Button clicks, you can define an EventHandler for the Form 'Resize Event, and take action based on the current WindowState after each resize:

private void Form1_Resize(object sender, EventArgs e)
{
    switch (this.WindowState)
    {
        case FormWindowState.Minimized:
            // do stuff
            break;
        case FormWindowState.Maximized:
            // do stuff
            break;
        case FormWindowState.Normal:
            // do stuff
            break;
    }
}


这是我找到的。虽然它不是我最初发布的,但至少它缩小到我的要求。



而不是单独识别用户是否点击了右上角的关闭按钮表格标题栏的一角或系统菜单中的关闭选项,或者他/她按下Alt + F4键,我们可以将这些因素结合起来检查用户是否正在使用系统命令(即任何一个以上三种技术)关闭表格或以编程方式进行。如果使用系统命令,则相应地采取措施;否则我们会忽略。



一个按钮用于以编程方式关闭表单而不会中断。



Here’s what I found. Though it’s not exactly what I originally posted for but at least it narrows down to my requirement.

Instead of identifying separately if the user clicked the Close button at the top right corner of the title bar of the form or the ‘Close’ option in the system menu, or he/she pressed the Alt+F4 keys, we can combine these factors together to check if the user is consuming the system command (i.e. any of the above three techniques) to close the form or is doing so programmatically. If the system command is used, action is taken accordingly; else we ignore.

A button is used to close down the form programmatically without any interruption.

public partial class Form1 : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_CLOSE = 0xF060;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            // Check if the user is consuming the system command to close the form.
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_CLOSE))
            {
                MessageBox.Show("Form closing by system command. Take necessary actions here...");
            }

            base.WndProc(ref m);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }


这篇关于检测鼠标按钮&表单系统菜单上的事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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