在 C# 中调用 ShellWindows FindWindowSW 方法



我正在编写一个小应用程序,它将启动Internet Explorer并打开一个未知的列表或URL,作为新窗口或现有窗口中的新选项卡(取决于该特定站点的设置(。正在启动的网站可以位于任何互联网区域。我能够使用 SHDocVw 方法来打开新的窗口和选项卡。

我正在尝试找出一种方法来跟踪上次打开的Internet Explorer引用,以便我可以使用该引用打开选项卡。

我遇到了这样的情况:由于"松散耦合的Internet Explorer"(LCIE(和IE保护模式,我启动的IE实例被关闭,另一个实例自动启动(IE虚拟选项卡切换(。这导致我丢失了对原始IE的引用,并且当我尝试打开选项卡时,它失败了。

我想使用 ShellWindows FindWindowSW 方法来获取特定的 Window(基于 ShellWindows cookie 值(,但我无法让它工作。 有人可以指出我正确的方向吗?

private InternetExplorer GetLastExplorer(int cookie)
{
object _m = Type.Missing;
const int SWC_BROWSER = 0x00000001;
const int SWFO_COOKIEPASSED = 4;
int pHWND;           
_shellWindows.FindWindowSW(cookie, ref _m, SWC_BROWSER, out pHWND, 5);
foreach (InternetExplorer window in _shellWindows)
{
if (window.HWND == pHWND)
return window;
}
return null;
}

我无法让它工作,不得不采取不同的方法。我最终执行以下操作来获取上次打开的IE实例:

private InternetExplorer _lastInternetExplorer;
private List<InternetExplorer> _existingInternetExplorers = new List<InternetExplorer>();
private static ShellWindows _shellWindows = new ShellWindows();
_shellWindows.WindowRegistered += OnShellWindowRegistered;
private void OnShellWindowRegistered(int lCookie)
{
foreach (InternetExplorer window in _shellWindows)
{
if (!_existingInternetExplorers.Contains(window))
{
_lastInternetExplorer = window;
_existingInternetExplorers.Add(window);
}
}        
}

相关内容

最新更新