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.
I have figured out how to enumerate a list of open tabs using Interop.ShDocVW, but 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.
So I've been messing with EnumWindows
and EnumChildWindows
but I can't get anything to work.
Any suggestions on how to best approach this?
This code enumerates window handles that correspond to IE thumbnails and can be used as the
hwndSource
parameter of the DwmRegisterThumbnail functionUpdate
While specified in the question indeed, I hadn't actually looked into the DWM Thumbnail API and the requirements of the DwmRegisterThumbnail function specifically:
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.
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):
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.:
In order to retrieve all desired tabs, you need to iterate over the results by means of the 2nd parameter
hwndChildAfter
of FindWindowEx():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!
The solution I went with was using
EnumWindows
andGetWindowText
from the Win32 API. I enumerate through Internet Explorer windows usingshdocvw.dll
and pass the tab's caption to a method that parses the results ofGetWindowText
to find the hwnd of the window with that caption.This works for all IE windows, not just tabs.