Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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?
Posted
Updated 16-Feb-14 4:36am
v2

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:
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (e.CloseReason == CloseReason.UserClosing)
     {
         // do stuff
         if(SomeConditionYouDefine) e.Cancel = true;
     }
 }
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:
C#
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;
    }
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Feb-14 19:40pm    
Correct, a 5.
—SA
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.

C#
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();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900