【问题标题】:Is this hWnd a child of mine?这个 hWnd 是我的孩子吗?
【发布时间】:2009-05-07 15:44:14
【问题描述】:

如何判断 hWnd 是否属于我的子控件之一?

我想做这样的事情:

if(this.Controls.Find(hWnd) != null) return false;

【问题讨论】:

    标签: c# .net winforms hwnd


    【解决方案1】:

    为此有一个 Win32 函数:IsChild

    【讨论】:

      【解决方案2】:

      听起来是使用递归的好机会。将此函数添加到您的父类:

        private bool IsChild(System.Windows.Forms.Control control, System.IntPtr hWnd)
        {
           if(control.Handle == hWnd)
              return(true);
      
           foreach (System.Windows.Forms.Control child in control.Controls)
           {
              if (IsChild(child, hWnd))
                 return (true);
           }
           return (false);
        }
      

      然后您可以使用此函数在此父类中搜索具有指定 hWnd 的任何子控件:

      this.IsChild(this, hWnd);
      

      【讨论】:

      • 谢谢!我希望有一种比迭代子控件更快的方法(特别是如果 hWnd 属于孩子的孩子:))
      猜你喜欢
      • 1970-01-01
      • 2012-05-22
      • 2013-08-11
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多