2011-10-27 27 views
0

如何以编程方式在运行IE的网页上找到用户控件的句柄?在IE中查找ActiveX用户控件的句柄

我能够使用Spy ++找到它,但自从手柄不断变化我卡住了。

我一直在使用FindWindow函数(),但没有运气试图:(我也想知道如果我做错了什么,或者干脆只适用于Windows工作...

由于提前,

朱波罗卡

回答

1

我也有类似的问题,找到一个PDF ActiveX控件在WPF中IE控件内部。

为了克服我用EnumChildWindows API找到正确的子窗口,从而获得其处理问题。

我将包含尽可能多的代码。

private static IntPtr FindPdfControlWindow(IntPtr parentHandle) 
{ 
    IntPtr result = IntPtr.Zero; 
    IntPtr matchPointer = IntPtr.Zero; 
    try 
    { 
     //allocate unmanaged memory for the result of the callback delegate 
     matchPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); 
     Marshal.WriteIntPtr(matchPointer, IntPtr.Zero) 

     //instantiate the delegate and pass to the API 
     NativeMethods.EnumWindowProc windowChecker = CheckForPdfControlWindow; 
     if (!NativeMethods.EnumChildWindows(parentHandle, 
               windowChecker, 
               matchPointer)) 
    } 
    finally 
    { 
     if (matchPointer != IntPtr.Zero) Marshal.FreeHGlobal(matchPointer); 
    } 
    return result; 
} 

private static bool CheckForPdfControlWindow(IntPtr handle, 
               IntPtr matchPointer) 
{ 
    int captionLength = NativeMehtods.GetWindowTextLength(handle); 
    if (captionLength > 0) 
    { 
     StringBuilder buffer = new StringBuilder(captionLength + 1); 
     NativeMethods.GetWindowText(handle, buffer, buffer.Capacity); 
     if (buffer.ToString().Contains("Adobe")) 
     { 
      Marhsal.WriteIntPtr(matchPointer, handle) 
      return false; 
     } 
    } 
    return true; 
} 

private static class NativeMethods 
{ 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool EnumChildWindows(IntPtr window, 
                EnumWindowProc callback, 
                IntPtr i); 

    internal delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); 

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSer.Auto)] 
    internal static extern int GetWindowTextLength(IntPtr hWnd); 

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    internal static extern int GetWindowText(IntPtr hWnd, 
               StringBuilder lpString, 
               int nMaxCount); 
} 

在急于转录,所以我希望它是有用的和准确的。

+0

谢谢,我不得不工作一下,但几分钟后我做了工作! –

1

如果ActiveX控件是窗口化的,那么你可以查询它的IOleWindow接口来获得窗口句柄。

query interfaces from the ActiveX之前,您需要查看页面的HTML以找到识别文档中activex的方法,例如元素id。