How to capture mousemove events beneath child controls

后端 未结 5 866
粉色の甜心
粉色の甜心 2021-01-18 09:42

I am trying to handle a mouseclick event on a particular form that should fire if the mouse cursor falls between a set of coordinates - lets say a square.

I understa

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 10:08

    try this:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AddMouseMoveHandler(this);
        }
    
        private void AddMouseMoveHandler(Control c)
        {
            c.MouseMove += MouseMoveHandler;
            if(c.Controls.Count>0)
            {
                foreach (Control ct in c.Controls)
                    AddMouseMoveHandler(ct);
            }
        }
    
        private void MouseMoveHandler(object sender, MouseEventArgs e)
        {
            lblXY.Text = string.Format("X: {0}, Y:{1}", e.X, e.Y);
        }
    }
    

提交回复
热议问题