鼠标在表格边框上按下时获取MouseDown事件?

时间:2014-06-27 19:21:18

标签: c# winforms

当鼠标在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 message not sent at the end of dragging a form, how to do so?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

<强> EDIT1:
正如Lars评论的那样,使用WinForms无法做到这一点。您需要“下载”一个楼层并使用WinForms基础架构...