C# 当鼠标落在窗体边框上时获取MouseDown事件?

C# 当鼠标落在窗体边框上时获取MouseDown事件?,c#,winforms,C#,Winforms,当鼠标在窗体的边框上按下/单击时,我不知道如何获取MouseDown事件。很容易看出,当鼠标落在表单的(我认为它被称为)客户区时,它会升起,但当鼠标落在边框上时,它永远不会升起 下面是一个SSCCE,它演示了这个问题。只有当鼠标落在客户区而不是边框上时,表单中心的标签才会更改 是否有任何方式可以捕捉到这一事件,或者将其引发 using System; using System.Collections.Generic; using System.ComponentModel; using Syst

当鼠标在
窗体
的边框上按下/单击时,我不知道如何获取
MouseDown
事件。很容易看出,当鼠标落在表单的(我认为它被称为)客户区时,它会升起,但当鼠标落在边框上时,它永远不会升起

下面是一个SSCCE,它演示了这个问题。只有当鼠标落在客户区而不是边框上时,表单中心的标签才会更改

是否有任何方式可以捕捉到这一事件,或者将其引发

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;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间MouseEventTest
{
公共部分类Form1:Form
{
Random rand=新的Random();
公共表格1()
{
初始化组件();
}
私有void Form1\u MouseDown(对象发送方,MouseEventArgs e)
{
label1.Text=rand.Next().ToString();
}
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.label1=new System.Windows.Forms.Label();
这个.SuspendLayout();
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(42,30);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(91,13);
this.label1.TabIndex=0;
this.label1.Text=“9999999999”;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图纸尺寸(185,75);
this.Controls.Add(this.label1);
this.Name=“Form1”;
this.Text=“Form1”;
this.MouseDown+=new System.Windows.Forms.MouseEventHandler(this.Form1\u MouseDown);
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.label1;
}
}
编辑:我已经解决了这个问题,但它又造成了另一个问题。如果你将来要看这个问题,这里有一个我问的相关问题。它(希望)将帮助我/我们确定鼠标何时也被释放

使用并计算窗体的边框。没有办法通过“表单”处理它,因为边框不是表单的一部分

EDIT1:

正如Lars所评论的,使用WinForms是无法做到这一点的。您需要“下一层楼”使用WinForms基础架构…

覆盖WndProc方法:

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);
}

Post被标记为WinForms。您能提供一个用于此的用例吗?一般来说,你不应该在意那个细节。否则,您将不得不重写WndProc并检查非客户端区域中的消息。我已将CodeProject项目(末尾的链接)包括在我的项目中,它是一个表单扩展程序,将“捕捉”子表单。问题是移动表单时会产生“抖动”。我试图通过在用户移动后“放开”表单后“抓拍”表单来停止这种不安。我需要捕获子窗体的
MouseDown
事件,但它们不会被抛出,因为边框是窗体移动的方式,其他事件似乎都不适合捕获尝试重写
OnResizeEnd
方法。它会在移动表单后激发。我是否可以从其他类订阅此事件?或者我必须在“基本形式”中创建我自己的自定义事件,并从中派生出我的所有形式,以便为所有形式调用此事件?你必须创建一个事件。我想既然你回答了这个问题,你可能想进一步深入兔子洞,最后链接是我问的一个相关问题。但这与释放鼠标时捕捉事件有关//