作为人帮助我弄清楚如何在默认浏览器中开始打开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());
}
}
根据一个人的建议,我访问了一篇做到这一点的文章,它的工作原理如下:
private string getDefaultBrowser()
{
string browser = string.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(@"HTTPshellopencommand", 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();
}
}