2010-06-16 46 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.

相關問題