0

I cannot figure out how to get the MouseDown event when the mouse is down/clicked on the border of a Form. It is easy to see that it is raised when the mouse is down in the (I think its called) client area of the form, but it is never raised when it is down on the border.

Here is a SSCCE that demonstrates the issue. The label in the center of the form is only changed when the mouse is down on the client area and not the border.

Is there anyway to catch this event or have it be raised?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MouseEventTest
{
    public partial class Form1 : Form
    {
        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            label1.Text = rand.Next().ToString();
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(42, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(91, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "99999999999999";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(185, 75);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
    }
}

EDIT: I have solved this problem, but it created another. If you are visting this question in the future here is a related question I asked. It (hopefully) will help me/us figure out when the mouse has been released too. WM_NCLBUTTONUP message not sent at the end of dragging a form, how to do so?

Community
  • 1
  • 1
AnotherUser
  • 1,311
  • 1
  • 14
  • 35
  • Can you provide a use-case for this? Generally, you shouldn't care about that detail. Otherwise, you would have to override WndProc and examine the messages in the non-client area. – LarsTech Jun 27 '14 at 19:30
  • I have included the CodeProject project (link at the end) in my project, it is a form extender which will "snap" child forms in place. The problem is it creates "jitters" when moving the form. I am trying to stop the jitters by only "snapping" the form once the user has "let go" after moving. I need to catch the `MouseDown` events of the child forms, but they are not thrown for borders which is how the form is moved, none of the other events seem suitable to catch. // http://www.codeproject.com/Articles/6338/SnapFormExtender-a-magnet-for-your-MDI-child-forms – AnotherUser Jun 27 '14 at 19:37
  • Try overriding the `OnResizeEnd` method. It fires after the form is moved. – LarsTech Jun 27 '14 at 19:43

2 Answers2

1

Override the WndProc method:

const int WM_NCLBUTTONDWN = 0xA1;

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_NCLBUTTONDWN)
    {
        this.Text = "got it!";
    }

    base.WndProc(ref m);
}
David Crowell
  • 3,711
  • 21
  • 28
  • Is there anyway that I might subscribe to this event from another class? Or would I have to create my own custom event in a "base form" and derive all my forms from it to have this event evoked for all forms? – AnotherUser Jun 30 '14 at 13:08
  • You'll have to create an event. – David Crowell Jun 30 '14 at 13:12
  • I figured since you answered this you might want to go a little further down the rabbit hole, linked at the end is a related question I asked. But it has to do with catching the event when the mouse is released. // http://stackoverflow.com/questions/24493167/wm-nclbuttonup-message-not-sent-at-the-end-of-dragging-a-form-how-to-do-so – AnotherUser Jun 30 '14 at 15:06
0

Use this method and calculate for the border of your form. There's no way to handle it via "Form" because the border is not a part of the Form...

EDIT1:
As commented by Lars, there's no way to do this using WinForms. You will need to "go down" a floor and work with the WinForms infrastructure...

Leonardo
  • 10,737
  • 10
  • 62
  • 155