gpt4 book ai didi

WPF 浏览器控件与 winforms

转载 作者:行者123 更新时间:2023-12-03 18:06:14 25 4
gpt4 key购买 nike

我正在创建一个 wpf 应用程序,我在其中使用了 webbrowser 控件。无论如何,有时我需要查找 html 元素、调用点击和其他基本功能。

在 winforms webbrowser 控件中,我可以通过执行以下操作来实现:

 webBrowser1.Document.GetElementById("someId").SetAttribute("value", "I change the value");

在 wpf webbrowser 控件中,我设法通过执行以下操作来实现相同的目的:
  dynamic d = webBrowser1.Document;  
var el = d.GetElementById("someId").SetAttribute("value", "I change the value");

我还设法通过使用动态类型调用 wpf webbrowser 控件中的单击。有时我会得到异常(exception)。

我怎样才能找到 html 元素、设置属性和调用点击 在 wpf webbrowser 控件中,而不必使用我经常遇到异常的动态类型?我想用 wpf webbrowser 控件替换 wpf 应用程序中的 winforms webbrowser 控件。

最佳答案

使用以下命名空间,您可以访问所有元素属性和事件处理程序属性:

    using mshtml;

private mshtml.HTMLDocumentEvents2_Event documentEvents;
private mshtml.IHTMLDocument2 documentText;

在构造函数或 xaml 中设置您的 LoadComplete 事件:
    webBrowser.LoadCompleted += webBrowser_LoadCompleted;

然后在该方法中创建新的 webbrowser 文档对象并查看可用属性并创建新事件,如下所示:
    private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
documentText = (IHTMLDocument2)webBrowserChat.Document; //this will access the document properties as needed
documentEvents = (HTMLDocumentEvents2_Event)webBrowserChat.Document; // this will access the events properties as needed
documentEvents.onkeydown += webBrowserChat_MouseDown;
documentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;
}

private void webBrowser_MouseDown(IHTMLEventObj pEvtObj)
{
pEvtObj.returnValue = false; // Stops key down
pEvtObj.returnValue = true; // Return value as pressed to be true;
}

private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
{
return false; // ContextMenu wont open
// return true; ContextMenu will open
// Here you can create your custom contextmenu or whatever you want
}

关于WPF 浏览器控件与 winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8871130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com