如何从 CEF 浏览器中获取当前 URL 地址,使用 C# WinForms。

huangapple go评论77阅读模式
英文:

How to Get the Current URL Adress From cef Browser c# winforms

问题

我可以翻译您的要求:

我想要从 C# WinForms 中的 CEF 浏览器获取当前 URL。每当浏览器加载一个 URL(访问一个网站)时,我希望该 URL 显示在 Form1 的 textBox1 中。

英文:

How can i get The current Url from cef browser c# winforms . i want URL to be displayed in textBox1 in Form1 Whenver browser loads a URL ( visit a website )

答案1

得分: 0

获取C# WinForms项目中CEF浏览器的当前URL,需要在您的代码中添加以下内容:

  1. 为浏览器创建一个地址更改事件。

  2. 将以下代码放入:

    private void chromiumWebBrowser1_AddressChanged(object sender, AddressChangedEventArgs e)
    
    string url = e.Address;
    textBox1.Invoke(new Action(() => textBox1.Text = url));
    

例如:

private void chromiumWebBrowser1_AddressChanged(object sender, AddressChangedEventArgs e)
{
    string url = e.Address;
    textBox1.Invoke(new Action(() => textBox1.Text = url));
}

在上述示例中,我们可以看到每当浏览器加载一个URL时,它会触发地址更改事件,并在事件中使用Invoke方法获取URL。

英文:

To get the current URL from the CEF browser in a C# WinForms project, you need to add these things in your code:

  1. Create an Address Changed Event for the browser.

  2. Put this code in the:

    private void chromiumWebBrowser1_AddressChanged(object sender, AddressChangedEventArgs e)
    
    string url = e.Address;
    textBox1.Invoke(new Action(() => textBox1.Text = url));
    

For Example:

private void chromiumWebBrowser1_AddressChanged(object sender, AddressChangedEventArgs e)
{
    string url = e.Address;
    textBox1.Invoke(new Action(() => textBox1.Text = url));
}

In the above example, we see that whenever the browser loads a URL, it triggers the address change event and, inside the event, we are getting the URL using the Invoke method.

if any futher questions reply to this ANSWER

Regards Adit

huangapple
  • 本文由 发表于 2023年2月18日 13:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定