1

由于有人帮助我弄清楚如何在默认浏览器中开始打开 ​​URL,现在我遇到了一些问题,导致单击时在 2 个不同的选项卡中打开 URL。这可能是什么原因?

编辑:我认为最好指出我在详细模式下使用 ListView。因此,当用户单击带有 URL 的列时,它应该在浏览器中打开一个窗口。

lvWeb.MouseMove += new MouseEventHandler(lvWeb_MouseMove);
lvWeb.MouseUp +=new MouseEventHandler(lvWeb_MouseUp);

private void lvWeb_MouseMove(object sender, MouseEventArgs e)
{
    var hit = lvWeb.HitTest(e.Location);
    if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1]) 
        lvWeb.Cursor = Cursors.Hand;
    else lvWeb.Cursor = Cursors.Default;
}

private void lvWeb_MouseUp(object sender, MouseEventArgs e)
{
    var hit = lvWeb.HitTest(e.Location);
    if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
    {
        var url = new Uri(hit.SubItem.Text);
        System.Diagnostics.Process.Start(url.ToString());
    }
}
4

1 回答 1

3

正如一个人所建议的那样,我访问了一篇成功的文章,它的工作原理如下:

    private string getDefaultBrowser()
    {
        string browser = string.Empty;
        RegistryKey key = null;
        try
        {
            key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

            //trim off quotes
            browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
            if (!browser.EndsWith("exe"))
            {
                //get rid of everything after the ".exe"
                browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
            }
        }
        finally
        {
            if (key != null) key.Close();
        }
        return browser;
    }

    private void lvWeb_MouseUp(object sender, MouseEventArgs e)
    {
        var hit = lvWeb.HitTest(e.Location);
        if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
        {
            var url = new Uri(hit.SubItem.Text);
            //System.Diagnostics.Process.Start(url.ToString());

            Process p = new Process();
            p.StartInfo.FileName = getDefaultBrowser();
            p.StartInfo.Arguments = url.ToString();
            p.Start();
        }
    }
于 2011-12-20T21:24:26.077 回答