How do I display open IE tabs as DWM thumbnails?

前端 未结 3 786
面向向阳花
面向向阳花 2021-02-20 06:46

I am building a WPF application in C# and I want to display thumbnails of open IE tabs in a listbox. I\'m essentially trying to duplicate the DWM functionality in Windows 7.

3条回答
  •  有刺的猬
    2021-02-20 07:11

    Update

    While specified in the question indeed, I hadn't actually looked into the DWM Thumbnail API and the requirements of the DwmRegisterThumbnail function specifically:

    hwndSource

    The handle to the window to use as the thumbnail source. Setting the source window handle to anything other than a top-level window type will result in a return value of E_INVALIDARG. [emphasis mine]

    The emphasized requirement renders my approach with child windows retrieved via FindWindowEx() outlined below invalid, i.e. only FindWindow() might be used to retrieve a handle to a top-level window instead (thanks Simon for pointing this out) - Simon's answer provides an appropriate solution based on the class name of the top-level IE window apparently rendered specifically for this purpose.


    [...] in order to use the DWM API calls, I have to pass in an hwnd, and the tabs all share the same handle as Internet Explorer.

    How have you inspected the window hierarchy? If I inspect an IE 9 window with e.g. Spy++, it exposes the following hierarchy of Window Classes (abbreviated):

    • IEFrame
      • [...]
      • Frame Tab
        • [...]
      • Frame Tab
        • [...]
        • TabWindowClass
          • Shell DocObject View
            • Internet Explorer_Server

    The child windows have separate handles, so (from the top of my head) you should be able to retrieve the desired ones via appropriate calls to the FindWindowEx function, e.g.:

    HWND hwndIeTab = ::FindWindowEx(hwndIeFrame, NULL, "Internet Explorer_Server", NULL);
    

    In order to retrieve all desired tabs, you need to iterate over the results by means of the 2nd parameter hwndChildAfter of FindWindowEx():

    A handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.

    So you'd need to iterate via class "Frame Tab" first and retrieve each "Internet Explorer_Server" child window with a second call to FindWindowEx() in turn (though you might want to experiment, whether passing a child higher up via the 3rd parameter lpszClass produces identical or better results).

    Good luck!

提交回复
热议问题