代码之家  ›  专栏  ›  技术社区  ›  John Weldon user3678248

这是我的孩子吗?

  •  1
  • John Weldon user3678248  · 技术社区  · 16 年前

    我如何判断硬件是否属于我的一个孩子控件?

    我想做如下的事情:

    if(this.Controls.Find(hWnd) != null) return false;
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Tim Robinson    16 年前

    这有一个win32函数: IsChild

        2
  •  2
  •   Andrew Garrison    16 年前

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

      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);