捕获鼠标按钮

本文关键字:按钮 鼠标 | 更新日期: 2023-09-27 18:28:50

我有一个包含GroupBox1的表单,它还包含Label1、TextBox1和Button1。GroupBox1.Enables为false!我可以在TextBox1上捕捉或模拟鼠标单击吗?

捕获鼠标按钮

我最好发布一个答案而不是评论。由于您禁用了TextBox的父级,因此不会获得任何事件。从技术上讲,您可以实现IMessageFilter接口来解决这个问题。但非常重要的是,你不要试图绕过这一点,这一切都是经过设计的,你永远不应该对点击禁用的控件感兴趣。

你的用户永远不会猜到一百万年后这样的点击会有什么用处。禁用的控件使其非常显然,单击它是毫无意义的。应该如此。如果有任何点击,那么这可能是一场意外。您既不想实现意外,也不想暴露代码中完全无法发现的功能。

我做了一个解决方案

using System;
using System.Windows.Forms;
using System.Drawing;
//$/t:winexe
//& RunInOwnWindow
namespace PowerAPP
{
    public class MainForm : Form
    {
        #region  Initialization
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label2;
        private static void Main(string [] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        MainForm()
        {
            groupBox1 = new System.Windows.Forms.GroupBox();
            button1 = new System.Windows.Forms.Button();
            textBox1 = new System.Windows.Forms.TextBox();
            label1 = new System.Windows.Forms.Label();
            textBox2 = new System.Windows.Forms.TextBox();
            label2 = new System.Windows.Forms.Label();
            // button1
            button1.Name = "Button1";
            button1.Location = new System.Drawing.Point(101, 90);
            button1.Size = new System.Drawing.Size(75, 24);
            button1.Text = "button1";
            // textBox1
            textBox1.Name = "TextBox1";
            textBox1.Location = new System.Drawing.Point(76, 25);
            textBox1.Size = new System.Drawing.Size(100, 20);
            // label1
            label1.Name = "Label1";
            label1.Bounds = new Rectangle(35, 22, 146, 28);
            label1.BackColor = System.Drawing.Color.Yellow;
            label1.Text = "label1";
            // textBox2
            textBox2.Name = "TextBox2";
            textBox2.Location = new System.Drawing.Point(76, 55);
            textBox2.Size = new System.Drawing.Size(100, 20);
            // label2
            label2.Name = "Label2";
            label2.Bounds = new Rectangle(35, 52, 146, 28);
            label2.BackColor = System.Drawing.Color.Yellow;
            label2.Text = "label2";
            groupBox1.Name = "GroupBox1";
            groupBox1.Controls.Add(button1);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(label1);
            groupBox1.Controls.Add(textBox2);
            groupBox1.Controls.Add(label2);
            groupBox1.Location = new System.Drawing.Point(35, 34);
            groupBox1.Size = new System.Drawing.Size(200, 128);
            groupBox1.Text = "groupBox1";
            // MainFORm
            Name = "MainFOrm";
            ClientSize = new System.Drawing.Size(292, 266);
            Controls.Add(groupBox1);
            Text = "Click Fields to include";
            MouseClick += new System.Windows.Forms.MouseEventHandler(MainForm_MouseClick);
            groupBox1.Enabled = false;
        }
        #endregion
        private void MainForm_MouseClick(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            Locate_Point_in_Control_Bounds(this, pt);
        }
        private void Locate_Point_in_Control_Bounds(Control ctl, Point pt)
        {
            Rectangle r;
            if (ctl is Form || ctl.HasChildren)
            {
                foreach (Control c in ctl.Controls)
                {
                    if (c.HasChildren) Locate_Point_in_Control_Bounds(c, pt);
                    r = c.Bounds;
                    r.Offset(ctl.Left, ctl.Top);
                    if (r.Contains(pt))
                        MessageBox.Show(c.Name);
                }
            }
            else
            {
                r = ctl.Bounds;
                r.Offset(ctl.Left, ctl.Top);
                if (r.Contains(pt))
                    MessageBox.Show(ctl.Name);
            }
        }
    }
}

是的,您可以通过以下代码行轻松完成此操作:

    private void button1_Click(object sender, EventArgs e)
    {
        //Disable the groupBox with textBox1 in it
        groupBox1.Enabled = false;
        //Simulate the Click on textBox1
        textBox1_MouseClick(this, new MouseEventArgs(MouseButtons.Left, 1,0,0,0));
    }
    private void textBox1_MouseClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("Test");
    }

上面显示的代码也可以在另一个控件中,例如我使用了button1。

更新1:组框是否启用并不重要,如果您直接通过textBox1_MouseClick(…)调用事件,它总是会捕获事件。

问候