当鼠标落在窗体边界上时获取MouseDown事件

本文关键字:获取 MouseDown 事件 边界 鼠标 窗体 | 更新日期: 2023-09-27 18:10:39

我无法弄清楚如何获得MouseDown事件时,鼠标向下/单击在Form的边界。很容易看到,当鼠标在表单的客户端区域(我认为它被称为)向下时,它会升起,但是当它在边界向下时,它永远不会升起。

下面是演示该问题的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;
    }
}

编辑:我已经解决了这个问题,但它产生了另一个。如果你将来访问这个问题,这里是我问的一个相关问题。它(希望)能帮助我/我们弄清楚鼠标何时释放。WM_NCLBUTTONUP消息不发送在拖动窗体结束,如何做到这一点?

当鼠标落在窗体边界上时获取MouseDown事件

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

使用此方法计算窗体的边框。没有办法通过"Form"来处理它,因为边框不是Form的一部分…

EDIT1:
正如Lars所评论的那样,使用WinForms是无法做到这一点的。你需要"下"一层并使用WinForms基础设施…