c# winform 获得 鼠标 选择 容器中 控件 对象

解决方案 »

  1.   

    GetChildAtPoint方法不行。难到就只能递归。C#2005中就没提供方法?
      

  2.   

    不知道,要干嘛。控件的Enter事件不能满足要求?
      

  3.   

    你自己也说了,用递归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 WindowsFormsApplication7
    {
        public partial class Form1 : Form, IMessageFilter
        {
            public Form1()
            {
                InitializeComponent();            Application.AddMessageFilter(this);            Panel P1 = new Panel();
                P1.Parent = this;            Panel P2 = new Panel();
                P2.Parent = P1;            new TextBox().Parent = P2;        }        public bool PreFilterMessage(ref Message m)
            {
                int WM_LBUTONDOWN = 0x201;            if (m.Msg == WM_LBUTONDOWN)
                {
                    Control C = GetChild(this.Controls);                if (C != null)
                        MessageBox.Show(C.GetType().Name);
                }
                return false;
            }        Control GetChild(Control.ControlCollection CC)
            {
                foreach (Control C in CC)
                    if (C.Bounds.Contains(C.Parent.PointToClient(Control.MousePosition)))
                    {
                        Control TC = GetChild(C.Controls);                    if (TC == null)
                            return C;
                        else
                            return TC;
                    }            return null;
            }
        }
    }
      

  4.   

    后记:
    很多页,想把这个程序封装。
    但是IMessageFilter不让继承?