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?

Not sure if you can actually track that mouse event but you could handle the FormClosing event instead.

Are you trying to detect these events on your form? If so, here's an example:

To use the example:

  • Add a MenuStrip named: menuStrip1
  • Add a ToolStripMenuItem named: File
  • Add a ToolStripMenuItem named: Close

  • Add "MouseDown" and "MouseUp" events

        private void closeToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Console.WriteLine("Left mouse down.");
            }//if
        }
    
        private void closeToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Console.WriteLine("Left mouse up.");
            }//if
        }
    

If you simply try to detect when the app is closing, use the advice darkagn gave you.

Otherwise you have to use Windows API calls. I used this search engine and found this code. It should be a good starting point.

HTH

If you simply try to detect when the app is closing, use the advice darkagn gave you.

Otherwise you have to use Windows API calls. I used this search engine and found this code. It should be a good starting point.

HTH

Thanks @Teme64. Please check my following post. I came up with something.

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();
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.