激活与C#应用程序指定的URL现有的浏览器窗口(不触发重新加载) [英] Activate existing browser window with given URL from C# application (without triggering reload)

查看:144
本文介绍了激活与C#应用程序指定的URL现有的浏览器窗口(不触发重新加载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C#应用程序,我想激活(brint到前)的网站,已经在用户的默认浏览器打开。我想这样做在浏览器无关的方式。

From a C# application I want to activate (brint to front) a site that is already open in the users default browser. I want to do this in a browser agnostic way.

困难的部分(至少对我来说)是我做的不可以希望浏览器打开一个新的标签页和浏览器的不得重新加载的页面,如果它是已经打开。

The difficult part (for me at least) is that I do not want the browser to open a new tab and the browser must not reload the page if it is already open.

我可以在默认浏览器中打开URI

I can open the URI in the default browser with

System.Diagnostics.Process.Start("http://foo.example");

但具体行为取决于用户的默认浏览器(IE6似乎重复使用当前最上面的浏览器窗口中,谷歌Chrome总是会打开一个新标签等)

But the exact behaviour depends on the users default browser (IE6 seems to reuse the current topmost browser window, Google Chrome will always open a new tab and so on)

我试过另一种方法是枚举所有打开的窗口,并找到一个我基于窗口的标题想(做假​​设大多数浏览器设置窗口标题到当前打开页面的标题)

Another approach I tried was to enumerate all open Windows and find the one I want based on the window title (making the assumption that most browsers set the window title to the title of the currently open page)

public delegate bool EnumThreadWindowsCallback(int hWnd, int lParam);

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

[DllImport("user32.dll")]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);

private bool FindWindowByRx(int hWnd, int lParam)
{
    Regex pattern = new Regex("Example Title of Page", RegexOptions.IgnoreCase);
    StringBuilder windowTitle = new StringBuilder(256);
    GetWindowText(hWnd, windowTitle, 255);

    if (pattern.IsMatch(windowTitle.ToString()))
    {
        SetForegroundWindow(new IntPtr(hWnd));
        return false; // abort search
    }
    else
    {
        return true; // keep on searching
    }
}

使用它:

EnumWindows(new EnumThreadWindowsCallback(FindWindowByRx), new IntPtr());

这做什么,我需要做的,但感觉很脆,hackish的,并可能是缓慢的。

This does what I need it to do, but feels very brittle and hackish, and is probably slow.

有没有更好的,更优雅的方式?

Is there a better, more elegant way?

推荐答案

这将工作,如果用户拥有该网站活跃在自己的浏览器。如果他们使用一个标签式浏览器,该网站是开放的,但不是在选定的选项卡,我不相信你会找到浏览器窗口。 (EnumWindows的只枚举顶层窗口)。

This will work, IF the user has that site active in their browser. If they're using a tabbed browser, and the site is open, but not in the selected tab, I don't believe you will ever find the browser window. (EnumWindows only enumerates top-level windows).

如果公司开始了新的选项卡中的用户,你最终没有找到它,(可能)在启动现场的第二个副本在新标签页。

If the user's started a new tab, you'll end up not finding it, and (likely) starting a second copy of the site in a new tab.

个人而言,如果你真的需要限制单个浏览器实例,我会考虑其是否可以嵌入浏览器在您的应用程序,而不是使用默认的浏览器。如果这可以由使用浏览器控制应用程序中的GUI元素,你有更多的控制,并能够避免这种类型的解决办法的。

Personally, if you really need to restrict to a single browser instance, I would consider whether its possible to embed the browser in your application instead of using the default browser. If this can be made a GUI element in your application using a browser control, you'd have much more control and be able to avoid this type of workaround.

这篇关于激活与C#应用程序指定的URL现有的浏览器窗口(不触发重新加载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆