2010-06-16 74 views

回答

3

只是想通了!

var presentationSource = (HwndSource)PresentationSource.FromVisual(child); 
var parentHandle = presentationSource.Handle; 
1
[DllImport("user32.dll")] 
     public static extern int GetParent(int hwnd); 

     public int GetParentWindowHandle(Visual child) 
     { 
      HwndSource presentationSource = (HwndSource)PresentationSource.FromVisual(child); 

      int parentHandle = presentationSource.Handle.ToInt32(); 
      int handle = parentHandle; 

      while (parentHandle != 0) 
      { 
       handle = parentHandle; 
       parentHandle = ApplicationHelperInterop.GetParent(parentHandle); 
      } 

      return handle; 
     } 

然后,您可以遍历集合System.Windows.Forms.Application.OpenForms找到对应于上述GetParentWindowHandle方法的返回值一个WinForm。

Alex D.

相关问题