从 Web 浏览器控件中检索选定的文本
这就是我想要做的:
从使用网络浏览器控件打开的网页中选择文本。在该文本仍处于选中状态时单击按钮后,我希望弹出一个消息框,显示由用户。 如何在我的 wpf 应用程序中使用此功能?
我认为我使用 mshtml 的方式是正确的,但我收到一条错误消息:
调用 COM 组件返回了错误 HRESULT E_FAIL。
即使我在文档上尝试一些小操作(例如更改标题),也会发生此错误。
代码如下:
IHTMLDocument2 doc = (IHTMLDocument2)this.webBookText.Document;
doc.title = "l";
Here's what I am trying to do:
Select text from a webpage I pulled up using my web browser control.After clicking a button while this text is still selected I would like a message box to pop-up displaying the text that was highlighted by the user. How do I get this functionality to work in my wpf application?
I think I'm on the right track using mshtml but I get an error that says:
Error HRESULT E_FAIL has been returned from a call to a COM component.
This error will happen even when I try something small on the document like changing the title.
The code is below:
IHTMLDocument2 doc = (IHTMLDocument2)this.webBookText.Document;
doc.title = "l";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,对于初学者来说,使用 会简单得多
WebBrowser
比 mshtml (请注意,您仍然可以在 WPF 中托管WebBrowser
) - 这肯定会让您更轻松地完成简单的事情:但是,我可以'在那里看不到任何可以让您轻松处理选择的内容...
您可以使用
.Document.ActiveElement
获取选定的元素,但这是整个元素 - 不是所选部分。Well, for starters it would be a lot simpler to use
WebBrowser
than mshtml (note that you can still hostWebBrowser
in WPF) - this will certainly let you do simple things a lot easier:However, I can't see anything there that would let you work with selections very easily...
You can get the selected element with
.Document.ActiveElement
, but this is the entire element - not the selected portion.发现错误是因为这不在我的表单类中
Figured it out that error was because this wasn't in my form class